C++ Friend Question:
Download Questions PDF

Explain friend function?

Answer:

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;
}

Download C++ Friend Interview Questions And Answers PDF

Previous QuestionNext Question
List the advantages of using friend classes?What are friend classes?