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:

Practice Your Code Here

Example 2:

Practice Your Code Here

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:

Practice Your Code Here

Example 2:

Practice Your Code Here

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:

BASIS FOR COMPARISON FRIEND CLASS FRIEND FUNCTION
Basic This 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 declaration not mandatory. I must be used.
Use Friend 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.

Conclusion:

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.

Recommended Articles:

  1. What is Copy Constructor in C++
  2. What is Constructor and Destructor in C++
  3. What is static data members in C++
  4. What is Data Abstraction in C++
  5. What is Encapsulation in C++

References:

 

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 114 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Friend Class and Friend Function in C++ […]

trackback

[…] Friend Class and Friend Function in C++ […]

trackback

[…] Friend Class and Friend Function in C++ […]

You cannot copy content of this page
Scroll Up
3
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.