Friend Class and Friend Function in C++

Friend Class:

A friend class can access the private and protected members of other classes that have declared themselves friends. It can be useful to allow one class to access private members of another class.

  • [sourcecode language="plain"]your code here[/sourcecode]
Example 1:
Programmer's Academy
Example 2:
Programmer's Academy

Friend Function:

Like the friend class, this role can access private and protected members of another class. You can also declare a global function as a friend, as shown in the following example.

Example 1:
Programmer's Academy
Example 2:
Programmer's Academy

Important points about Friend class and function:

  1. Friends should be used for limited purposes only. Too many functions or outer classes declared as friends of a class with protected or private data reduce the value of encapsulating individual classes in object-oriented programming.
  2. Friendship is not mutual. If class A is friends with B, B will not automatically become friends with A.
  3. Friendship is not inherited.
  4. The concept of friends is not in Java.

Key Differences Between Friend Class and Function:

  1. The friend role is a role that can access private and protected members of a class. In contrast, a friends class is a class that helps access private members of the class.
  2. The friend function is declared including its prototype in the class and preceded by the friend keyword. Similarly, the friend class is defined by the friend keyword.
  3. The advance statement is used for friend functions, but does not need to be used in friend classes.

Comparison between Friend Class and Function:

BasicThis is the class used with the friend keyword to access private members of another class.This is a function that is used with the friend keyword to allow access of non-member functions to private members of the class.
Forward declarationnot mandatory.I must be used.
UseFriend classes can be used when creating a class on top of another class.Friend functions can be used in situations of operator overload.

Advantages and disadvantages of friend class and function:

One of the benefits of a friend class or role is that you have access to the class’s private and protected data. For example, if you are implementing a linked list, you can create a linked list class as a friend of the node class and access that data because the linked list is made up of nodes.

Therefore, in certain situations, you may find it advantageous to use a friend class or role. However, it also has some drawbacks.

One of the hallmarks of C ++ is encapsulation. That is, it groups the data with the functions that manipulate it so that external functions and classes cannot access the data. However, allowing one friend class or function to access private members of another class compromises the encapsulation function.

To avoid this, you need to be careful about using friend classes or functions. Your program should avoid using too many friendly classes and functions that would completely compromise encapsulation.


If your role needs access to two or more independent classes, inner members, you need a friend role. On the other hand, if a class needs access to members of another class, it needs a friendly class. If a multi-member function needs to be friends with that function, we recommend using the friend class.


(Visited 89 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Would love your thoughts, please comment.x