Friend Classes

Friend Class can access private or protected members and functions of other class that specifies it as a friend.

Syntax for specifying friend class:

Here's an example:

Output:

Explanation:

  • Atline 24 we declare a friend class. Inside the class Store any private / protected member or function of the class Phone can be accessed.
  • At line 31 an array of phones is declared. It's filled with elements at lines 66 - 70.
  • At lines 42 and 43 private members of the Phone class are accessed. This is possible only due to the declaration of a friend class at line 24.