Friend function Can access private, protected and public members of a class 

Let’s begin with a programming example in which I will show you how a friend function Can access private, protected, and public members of a class.

Example of Friend function Can access private and protected data members in C++
Example of Friend function Can access private and protected data members in C++

C++ Example: Friend function Can access private data members of a class

 

Output

Sum: 2

C++ Example: Friend function Can access protected data members of a class

Output

Sum: 2

 C++ Example: Friend function Can access public data members of a class

Output

Sum: 2