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

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.