Friend Class and Function in C++

Last Updated : 31 Aug, 2026

Friend functions and friend classes in C++ allow specific functions or classes to access the private and protected members of another class.

  • They provide controlled access to class internals when normal public member functions are not suitable.
  • Friendship is explicitly granted and is not mutual, inherited, or automatically extended to other functions or classes.

Example: The following example demonstrates how a friend function and a friend class can access the private members of a class.

C++
#include <iostream>
using namespace std;

class GFG {
private:
    int value = 100;

    friend void display(GFG obj);
    friend class FriendClass;
};

void display(GFG obj) {
    cout << "Friend Function: " << obj.value << endl;
}

class FriendClass {
public:
    void show(GFG obj) {
        cout << "Friend Class: " << obj.value << endl;
    }
};

int main() {
    GFG obj;
    FriendClass f;

    display(obj);
    f.show(obj);

    return 0;
} 

Output
Friend Function: 100
Friend Class: 100

Explanation: display() is declared as a friend function, while FriendClass is declared as a friend class of GFG. Therefore, both can access the private member value.

Friend Class

A friend class can access private and protected members of other classes in which it is declared as a friend.

  • Remember one thing, friendship is not mutual. If class A is a friend of B, then B doesn't become a friend of A automatically.
  • We can declare a friend class in C++ by using the friend keyword.
Friend class
C++
#include <iostream>
using namespace std;

class Geeks {
private:
    int private_variable;

protected:
    int protected_variable;

public:
    Geeks() {
        private_variable = 10;
        protected_variable = 99;
    }

    // friend class declaration
    friend class GFG;
};

// class GFG is declared as a friend
// inside class Geeks, therefore
// Class GFG can access private members
// of class Geeks.
class GFG {
public:
    void display(Geeks& t) {
        cout << "The value of Private Variable = "
             << t.private_variable << endl;
        cout << "The value of Protected Variable = "
             << t.protected_variable;
    }
};

int main() {
    Geeks g;
    GFG fri;
    fri.display(g);
    return 0;
}

Output
The value of Private Variable = 10
The value of Protected Variable = 99

Explanation: GFG is declared as a friend of Geeks, so its member function display() can directly access the private and protected members of Geeks.

Note: A friend class can be declared in the public, protected, or private section of a class. The access section does not affect the friendship.

Properties of Friend Class

A friend class has special access to the class that grants it friendship, with the following key properties:

  • Not Mutual: If A is a friend of B, B does not automatically become a friend of A.
  • Not Inherited: Friendship is not automatically passed to derived classes.
  • Not Transitive: If A is a friend of B and B is a friend of C, A does not become a friend of C.
  • Access: A friend class can access the private and protected members of the class that declares it as a friend.

Friend Function

Like friend classes, a friend function can be granted special access to private and protected members of a class in C++. They are not the member functions of the class but can access and manipulate the private and protected members of that class for they are declared as friends. A friend function can be:

1. Global Function as Friend

A global function can be declared as a friend by placing the friend keyword before its declaration inside the class.

C++
#include <iostream>
using namespace std;

class base {
private:
    int private_variable;

protected:
    int protected_variable;

public:
    base() {
        private_variable = 10;
        protected_variable = 99;
    }
    
    // Friend function declaration
    friend void friendFunction(base& obj);
};


// friend function definition
void friendFunction(base& obj) {
    cout << "Private Variable: " 
         << obj.private_variable << endl;
    cout << "Protected Variable: " 
         << obj.protected_variable;
}

int main() {
    base object1;
    friendFunction(object1);
    return 0;
}

Output
Private Variable: 10
Protected Variable: 99

Explanation: friendFunction() is not a member of Base, but it can access its private and protected members because Base explicitly declares it as a friend.

Note: The friend keyword is used only in the declaration inside the class. It is not required in the function definition or when calling the function.

2. Member Function of Another Class as a Friend

A specific member function of another class can also be declared as a friend. The class containing the member function must be declared before the friendship is specified.

C++
#include <iostream>
using namespace std;

// Forward Declaration needed
class base;

// Another class in which function is declared
class GFG {
public:
    void GFG_Function(base& obj);
};

// Base class declare a frined
// function of another class
class base {
private:
    int private_variable;

protected:
    int protected_variable;

public:
    base() {
        private_variable = 10;
        protected_variable = 99;
    }

    // Friend function declaration
    friend void GFG::GFG_Function(base&);
};

// Friend function definition
void GFG::GFG_Function(base& obj) {
    cout << "Private Variable: " << 
             obj.private_variable
         << endl;
    cout << "Protected Variable: " << 
             obj.protected_variable;
}

int main() {
    base object1;
    GFG object2;
    object2.GFG_Function(object1);

    return 0;
}

Output
Private Variable: 10
Protected Variable: 99

Explanation:m GFG::display() is declared as a friend of Base, allowing it to access Base's private and protected members. The forward declaration of Base allows GFG to declare the member function before Base is fully defined.

Note: The order in which we define the friend function of another class is important and should be taken care of. We always have to define both the classes before the function definition. Thats why we have used out of class member function definition.

Friend Function for Multiple Classes

The same friend function can be declared in multiple classes, allowing it to access the private or protected members of each class.

C++
#include <iostream>
using namespace std;

// Forward declaration
class ABC; 

class XYZ {
    int x;

public:
    void set_data(int a) 
    { 
      x = a; 
    }

    friend void max(XYZ, ABC);
};

class ABC {
    int y;

public:
    void set_data(int a) 
    { 
      y = a; 
    }

    friend void max(XYZ, ABC);
};

void max(XYZ t1, ABC t2)
{
    if (t1.x > t2.y)
        cout << t1.x;
    else
        cout << t2.y;
}

// Driver code
int main()
{
    ABC _abc;
    XYZ _xyz;
    _xyz.set_data(20);
    _abc.set_data(35);

    // calling friend function
    max(_xyz, _abc); 
    return 0;
}

Output
35

Explanation: max() is declared as a friend in both XYZ and ABC. Therefore, it can directly access the private members x and y of both classes and compare their values.

Advantages of Friend Functions and Classes

Friend functions and classes provide controlled access to private and protected members when needed.

  • Controlled Access: Provide access to private and protected members when required.
  • Better Cooperation: Allow related classes and functions to work closely together.
  • Operator Overloading: Useful for implementing certain operator overloading scenarios.
  • Flexible Access: Allow access without inheritance or additional getter functions.

Disadvantages of Friend Functions and Classes

Friendship provides useful access, but excessive use can affect encapsulation and increase coupling.

  • Weaker Encapsulation: Excessive use can weaken data hiding and encapsulation.
  • Tighter Coupling: Creates stronger dependency between the friend and the class.
  • Limited Friendship: Friendship is not inherited or automatically extended to related classes.
  • Maintenance Issues: Changes to private members may require updates to friend code.
Comment