A friend function of class defined outside that class scope but it has a right to access all private and protected member of class. Even through prototype of a function appears in the class definition. Friend function is not member function.
A friend function can be a function, Function template or member function or a class or class template. In this case the entire class & all its members are friends. To declare a function as a friend of class, preceded function prototype in the class definition with keyword friend as follows:
e.g:#include<iostream.h> #include<conio.h> class Sample{ int x,y; public: Sample(int i,int j){ x=i; y=j; } void show(){ cout<<"\nx = "<<x<<"\t"<<"y = "<<y; } friend float avg(Sample &s); }; float avg(Sample &s){ return((s.x+s.y)/2.0); } void main() { Sample s(10,4); clrscr(); s.show(); float a=avg(s); cout<<"\nAverage = "<<a; getch(); }O/P
X=10 Y=4 Average=12