Similarly like a friend function, class can be made a friend of another class using friend keyword.
A friend class in C++ can access the "private" and "protected" members of the class in which it is declared as a friend.
e.g:class A{ friend class B; } Class B{ }
When a class is made a friend class all the member function of that class becomes friend function. In this program all the member function of class B will be friend function of class A.
Thus any member function of class B can access the private and protected data of class A.
If B is declare friend class of A then all member function of class B can access private and protected data of class A, but member function of class A can not private & protected data of class B.
#include<conio.h> #include<iostream.h> class Number{ int x,y; public: void setNumber(int i,int j){ x=i; y=j; } void show() { cout<<"\nx = "<<x<<"\t"<<"y = "<<y; } friend class min; }; class min{ public: int minof(Number &n); }; int min::minof(Number &n) { if(n.x>n.y) return n.x; else return n.y; } void main() { Number n; min m; clrscr(); n.setNumber(5,6); n.show(); int a=m.minof(n); cout<<"\n Maximum = "<<a; getch(); }O/P
X=5 Y=6 Maximum= 6