A friend function cannot be used to overload the assignment operator=. Explain why? Write a C++ program to calculate sum of distance and display the result using Friend function?
Answers
Answer:
n this article, you'll learn to create friend function and class in C++, and use them efficiently in your program. One of the important concepts .
Answer:
One of the important concepts of OOP is data hiding, i.e., a nonmember function cannot access an object's private or protected data.
But, sometimes this restriction may force programmer to write long and complex codes. So, there is mechanism built in C++ programming to access private or protected data from non-member functions.
This is done using a friend function or/and a friend class.
friend Function in C++
If a function is defined as a friend function then, the private and protected data of a class can be accessed using the function.
The complier knows a given function is a friend function by the use of the keyword friend.
For accessing the data, the declaration of a friend function should be made inside the body of the class (can be anywhere inside class either in private or public section) starting with keyword