// demonstation of a friend function // // you should get compilation errors (that's the whole point)! #include // definition of the class foo, which has some private members // and methods class foo { private: int private_int; public: int public_int; // constructor foo() { cout << "I am foo!" << endl; private_int=10; public_int=20; } // destructor ~foo() { cout << "I am no more!" << endl; } int pubval(void) { return(public_int); } private: int prival(void) { return(private_int); } friend void foofriend(void); }; // ------------------------------------- // here is the friend function foofriend void foofriend(void) { // local variable of type foo foo f; // first go for the public stuff cout << "member public_int is " << f.public_int << endl; cout << "method pubval is " << f.pubval() << endl; // now go for the private stuff cout << "private_int is " << f.private_int << endl; cout << "method prival is " << f.prival() << endl; } // this one is not a friend of foo! void foofoe(void) { // local variable of type foo foo f; // first go for the public stuff cout << "member public_int is " << f.public_int << endl; cout << "method pubval is " << f.pubval() << endl; // now go for the private stuff cout << "private_int is " << f.private_int << endl; cout << "method prival is " << f.prival() << endl; } int main(void) { foofriend(); foofoe(); }