C/C++

Friend Function And Friend Class In C++ With Examples

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 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 the function prototype. When a friend function is defined outside the class, no resolution operator (::) or name of the 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 as an ordinary function without the scope resolution operator (::). The function “xy” is called in the main() function without the reference of the class object but the address of class object “obj” is passed as an argument to the function.

In the 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 the 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 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, a syntax error will arise.

In the main() function, two objects ‘aa’ and ‘bb’ is declared. The ‘aa’ and ‘bb’ are the objects of classes A and B respectively. These objects 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 a 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 a 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.

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’.

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 objects ‘aa’ and ‘bb’ is 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 an argument in members of class A are used for calculating the sum of values of data members of class A.



Related Article:

https://programmingdigest.com/c-operator-overloading-with-programming-examples/

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button