Guideline that should be followed while using friend function.
- When the application is needed to access a private member of another class, the only way is to utilize the friend functions.
- The following are the guidelines to handle friend functions :
1. Declare the function with the keyword ‘friend’ that is followed by return type and followed by the function name.
2. Specify the class whose private members are to be accessed in the friend function within parenthesis of the friend function.
3. Write the code of the friend function in the class.
- The following code snippet illustrates the use of friend function :
friend void display(car); //Friend of the class 'car'
void display(car myCar)
cout<<"\nThe color of my car is : "<<myCar.color;
cout<<"\nThe speed of my car is : "<<myCar.speed;