#Previous question #Index of C++ Questions #Next question

The concept of encapsulation and data hiding indicate that nonmember functions should not be able to access an objectís private and protected data. The policy is if you are not a member, you canít get it. But there is a certain situation wherein you need to share your private or protected data with nonmembers. ĎFriendsí come here as a rescue.

A friend function is a non-member function that grants access to classís private and protected members.

Pointers for friend functions:

  • A friend function may be declared friend of more than one class.
  • It does not have the class scope as it depends on functionís original definition and declaration.
  • It does not require an object (of the class that declares it a friend) for invoking it. It can be invoked like a normal function.
  • Since it is not a member function, it cannot access the members of the class directly and has to use an object name and membership operator (.) with each member name.
  • It can be declared anywhere in the class without affecting its meaning.
  • A member function of the class operates upon the members of the object used to invoke it, while a friend function operates upon the object passed to it as argument.

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students