Friend Functions

Friend functions are declared outside the class body. They have access to all private and protected members of the class.

Syntax:

Example:

Output:

Explanation:

  • At line 13 a function is marked as "friend". It has no body inside the class.
  • At lines 23 - 26 the friend function is declared. The body of a friend function is declared outside the class.
  • At line 32 the friend function is called.