Mutual friendship of Classes in C++ with Examples - GeeksforGeeks

16 Apr, 2021
Prerequisite: Friend Class in C++A friend class can access private and protected members of other classes in which it is declared as a friend. It is sometimes useful to allow a particular class to access private members of another class. Below is the program to illustrate the friend class:  
CPP
// C++ program to illustrate friend class#include <iostream>using namespace std; // Class Aclass A {private:    // Private member of Class A    int a; public:    // Constructor to initialise private    // member of class A    A() { a = 0; }     // Make friend class B using a friend    // keyword    friend class B;}; // Class Bclass B {private:    // Private members of class B    int b; public:    // Members function of class B    void showA(A& x)    {        // Since B is friend of A,        // it can access private members        // of class A        cout << "Accessed Private "             << "Member of class A\n";        cout << "A::a = " << x.a;    }}; int main(){    // Object of class A    A a;     // Object of class B    B b;     // Member function of friend class B    b.showA(a);    return 0;}
Accessed Private Member of class A
A::a = 0
 
Mutual Friendship of Classes: For class A and B is said to be in mutual friendship if class A is a friend class of class B and class B is a friend class of class A. Due to the establishment of friendship between these two classes, both the classes can access each other protected and private members using their own member functions.Proceeding normally to declare and define functions within both of these classes, there will be a common syntax error is encountered, which signals that the class declared first cannot access the data members of the succeeding class even when made a friend. Therefore Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of class A automatically.But we can make the mutual friendship of classes by avoiding error occurred above, the functions in the first class which is accessing the data members of the second class are defined after the second class using a scope resolution operator.Let us assume class A was defined first in the program and class B was defined after class A. The member functions in class A accessing the data of class B should be defined after class B. This is done to make sure that the compiler first reads class B which in turn will provide the information to the compiler that class A is a friend of class B, and hence is allowed to access its members.Below is the program to illustrate mutual friendship of classes: 
CPP
// C++ program to illustrate mutual// friendship of classes#include <iostream>using namespace std; // Forward Declarationclass B; // Class Aclass A {     // Member of class A    int data; public:    // Make B as a friend of class A    friend class B;     // Constructor to initialise member    // of class A    A(int d) { data = d; }     // Function to get data of friend    // class B    void get_data_B(B objb);}; // Class Bclass B {     // Member of class B    int dataB; public:    // Making A a friend of class B    friend class A;     // Constructor to initialise member of    // class B    B(int d) { dataB = d; }     // Function to get the data of    // friend class A    void get_data_A(A obja)    {        cout << "Data of A is: "             << obja.data;    }}; // Function for accessing friend class// B's objectvoid A::get_data_B(B objb){    cout << "Data of B is: "         << objb.dataB;} // Driver Codeint main(){     // Object of class A    A a(10);     // Object of class B    B b(20);     // Print data of class A    b.get_data_A(a);    cout << endl;     // Print data of class B    a.get_data_B(b);    return 0;}
Data of A is: 10
Data of B is: 20
 
In the above program as class A and class B are mutual friends. Therefore, for accessing the private member of class A can also be done from member function in class B and for accessing the private member of class B can also be done from member function in class A. 
arorakashish0911
C++-friend keyword
cpp-friend
Friend function and class
C++
C++ Programs
How To
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Sorting a vector in C++
Friend class and function in C++
Polymorphism in C++
List in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
C++ Program for QuickSort
How to return multiple values from a function in C or C++?
Program to print ASCII Value of a character
C++ program for hashing with chaining