#include using namespace std; class Friendly{ public: friend class Buddy; friend void GainAccess(Friendly & myFriend); int publicInt = 0; void PublicFunction() { cout << "I am the public function" << endl; return; } void TellAll() { cout << "In Tell All Friendly" << endl; cout << "\tpublicInt = " << publicInt << endl; cout << "\tprotectedInt = " << protectedInt << endl; cout << "\tprivateInt = " << privateInt << endl; cout << endl; return; } protected: int protectedInt = 0; void ProtectedFunction() { cout << "I am the protected function" << endl; return; } private: int privateInt = 0; void PrivateFunction() { cout << "I am the private function" << endl; return; } }; class DerivedFriend : public Friendly { public: void TellAll() { cout << "In Tell All DerivedFriend " << endl; cout << "\tpublicInt = " << publicInt << endl; cout << "\tprotectedInt = " << protectedInt << endl; //cout << "\tprivateInt = " << privateInt << endl; cout << endl; return; } private: int derivedInt = 7; }; class Buddy { public: void MessWithFriend( Friendly & myFriend) { myFriend.publicInt = 9; myFriend.protectedInt = 9; myFriend.privateInt = 9; myFriend.PublicFunction(); myFriend.ProtectedFunction(); myFriend.PrivateFunction(); return; } /* void MessWithDerived(DerivedFriend & d) { d.derivedInt = 9; return; } */ }; void GainAccess(Friendly & myFriend); int main() { Friendly f; DerivedFriend d; Buddy b; cout << " The base class tries to mess with me " << endl; f.TellAll(); cout << endl; cout << " The derived class tries to mess with me " << endl; d.TellAll(); cout << endl; cout << "My buddy is messing with me " << endl; b.MessWithFriend(f); f.TellAll(); cout << endl; cout << "GainAccess is messing with me " << endl; GainAccess(f); f.TellAll(); cout << endl; cout << "Messing wtih the derived class " << endl; b.MessWithFriend(d); return 0; } void GainAccess(Friendly & myFriend){ myFriend.publicInt = 99; myFriend.protectedInt = 99; myFriend.privateInt = 99; myFriend.PublicFunction(); myFriend.ProtectedFunction(); myFriend.PrivateFunction(); }