Friend function: – It is a non-member function. The use of friend function is to access the private member function of class or classes.

Note: – As we know that a non-member function (Which is not a part of the class) cannot access the private member of class but by using friend function we can access the private members of class. There are following rules to be followed.

  1. Friend function must be declare inside the class with friend keyword.
  2. In the formal argument of friend function the object should be their of the class.
  3. Friend function is not mutual.
  4. At the time of friend function definition friend keyword will not be use.
  5. A friend function can also be declared either in one class or more than one classes.
  6. Inside friend function. The member variables will be accessed with object name.

Note: – The use of friend function is very rare now a days because it exploit the data security.

Advertisements