Mutual friendship of Classes in C++ with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate friend class
#include <iostream>
using namespace std;
  
// Class A
class 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 B
class 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;
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate mutual
// friendship of classes
#include <iostream>
using namespace std;
  
// Forward Declaration
class B;
  
// Class A
class A {
  
    // Member of class A
    int data;
  
public:
    // Make B as a friend of class A
    friend class B;
  
    // Contructor 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 B
class B {
  
    // Member of class B
    int dataB;
  
public:
    // Making A a friend of class B
    friend class A;
  
    // Contructor 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 object
void A::get_data_B(B objb)
{
    cout << "Data of B is: "
         << objb.dataB;
}
  
// Driver Code
int 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;
}

chevron_right


Output:

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.