// C++ program to create a member function of another class // as a friend function #include using namespace std; class base; // forward definition needed // another class in which function is declared class anotherClass { public: void memberFunction(base& obj); /* void memberFunction(base& obj) { cout << "Private Variable: " << obj.private_variable << endl; cout << "Protected Variable: " << obj.protected_variable; } */ }; // base class for which friend is declared class base { private: int private_variable; protected: int protected_variable; public: base() { private_variable = 10; protected_variable = 99; } // friend function declaration friend void anotherClass::memberFunction(base&); }; // friend function definition void anotherClass::memberFunction(base& obj) { cout << "Private Variable: " << obj.private_variable << endl; cout << "Protected Variable: " << obj.protected_variable; } int main() { base baseObject1 ; anotherClass object2; object2.memberFunction( baseObject1 ); return 0; }