Answers

Question and Answer:

  Home  C++ Friend

⟩ Explain 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.

Declare the function with the keyword ‘friend’ that is followed by return type and followed by the function name.

Specify the class whose private members are to be accessed in the friend function within parenthesis of the friend function.

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;

}

 199 views

More Questions for you: