InterviewSolution
Saved Bookmarks
| 1. |
What do you know about friend class and friend function? |
|
Answer» A friend class can access private, protected, and PUBLIC members of other classes in which it is DECLARED as friends. Like friend class, friend function can also access private, protected, and public members. But, Friend functions are not member functions. For example - class A{ private: INT data_a; public: A(int X){ data_a=x; } friend int fun(A, B);}class B{ private: int data_b; public: A(int x){ data_b=x; } friend int fun(A, B);}int fun(A a, B b){ return a.data_a+b.data_b;}int main(){ A a(10); B b(20); cout<<fun(a,b)<<endl; return 0;}Here we can access the private data of class A and class B. |
|