Friend Function And Friend Class In C++ With Examples

(Last Updated On: August 17, 2020)

Friend Function:

Friend Function –The private and protected members of any class cannot be accessed from outside the class. Sometime, it may require to access private and protected members.

A Friend function is basically a non-member function of the class. A friend function is used to for accessing the private and other protected members of the class from outside of the class. A friend function can be used in basic and  in the most advanced programs.

A function is declared to be a friend of a class by using the keyword “friend” before the name of the function in function prototype. When a friend function is defined outside the class, no resolution operator (::) or name of class is mentioned along with the function header.


Write a program that explains the concept of friend function:

Friend Function

In this program, the function “xy” is a friend function. Even though the prototype of this function is given in the definition of class “abc” but it is not a member function of this class. Only the friendship is created with the class just to access its private and protected members.

The friend function is defined outside the class like an ordinary function without the scope resolution operator (::). The function “xy” is called in the main() function without the reference of class object but the address of class object “obj” is passed as argument to the function.

In definition of function “xy”, the private data members a & b of class “abc” are accessed (i.e. values are assigned to ‘a’ and ‘b’) with the reference of address of class object ‘c’.



Write a program by using classes A and B that initializes the data to private data member ‘a’ of class A and private data member ‘b’ of class B using constructors:

Friend Function

In this program, the function “sum” is declared as friend function in both classes (such as “friend int sum(A, B);”. This statement tells the compiler that the function is friend function of classes A & B.

The prototype of class B is also specified before the class A (such as “class B;”). It is because, class B is referenced in the prototype of friend function “sum” in class A (and also in class B). we know that is a class cannot be referenced before it has been declared. So the prototype of class B is necessary before using its name as a reference. Otherwise syntax error will be arise.

In the main() function, two object ‘aa’ and ‘bb’ are declared. The ‘aa’ and ‘bb’ are the objects of classes A and B respectively. These object are passed to the friend function “sum” to calculate the sum of private data members ‘a’ and ‘b’ of classes A and B respectively. The values are assigned to data members ‘a’ and ‘b’ by constructors of the classes.


Friend Class:

A class can also be declared as friend of another class. A type of class that allows for accessing the private and protected members of a particular class is called friend class. the keyword ‘friend’ is used before the class name to make it the friend of another class.

For example, to declare class b as friend of class a, the following statement is written in the definition of class a.

friend class B;


write a program that explains the concept of friend class:

Friend Function

in this program, two classes A and B are declared. In the definition of class A class B is declared as friend class A. so the members of class B can access the private and protected members of class A.

the class A contains two data members (one private and second protected ) and one member function. The member function contains the statements to assign values to data members ‘m’ and ‘n’.

the class B contains one member function ‘sum’. It has one argument of object type of class A. the member function ‘sum’ accesses the data members ‘m’ and ‘n’ through object ‘obj’ of class A and computes the sum of values. The result is returned to the calling function.

In the main() function, two object ‘aa’ and ‘bb’ are declared of class A and class B respectively. The data values in data members ‘m’ and ‘n’ of class A are assigned through member function ‘assign()’ of class A. the member function of class B is called for execution by passing object ‘aa’. The object ‘aa’ is passed by values and assigns to the object ‘obj’ used as argument in members of class A are used for calculating the sum of values of data members of class A

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply