Explain friend function?
Submitted by: AdministratorWhen 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;
}
Submitted by: Administrator
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;
}
Submitted by: Administrator
Read Online C++ Friend Job Interview Questions And Answers
Top C++ Friend Questions
☺ | List the characteristics of friend functions? |
☺ | Explain the characteristics of friend functions? |
☺ | What is a friend function? |
☺ | What are friend classes? |
☺ | Explain advantages of using friend classes? |
Top C Plus Plus Language Categories
☺ | C++ Pointers & Functions Interview Questions. |
☺ | C++ Operator Overloading Interview Questions. |
☺ | C++ Exception Handling Interview Questions. |
☺ | C++ Virtual Functions Interview Questions. |
☺ | C++ Template Interview Questions. |