1.

Explain friend functions and their characteristics.

Answer»

A friend function is a non-member function and is a friend of a class. It is declared inside a class with the prefix friend and defined outside the class like any other normal function without the prefix friend. This friend function can access private and protected data members if it is a friend function of that class.
The characteristics of friend function are:

  • The friend function should not be defined inside the class.
  • It can be declared inside a class under public, private or protected access specifier since it is a non member function and make no difference.
  • A friend function should be defined outside the class without prefix friend
  • The scope resolution operator should not be used while defining the friends function outside the class since it is not member function of any class.
  • It should be called like any other normal function and not to be called using an object with dot operator.
  • The data members of class are accessed through object and dot operator with a data members.


Discussion

No Comment Found