Open In App

Friend Class and Function in C++

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, friend functions and friend classes are concepts that allow certain functions or classes to access the private and protected members of another class. These are useful concepts in such situations where you need to give a function or another class access to internal data, while still keeping it hidden from the outside world.

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 and protected members of other classes. 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

Example:

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

Note: We can declare friend class or function anywhere in the base class body whether its private, protected or public block. It works all the same.

Friend Function in C++

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. A global function
  2. A member function of another class

1. Global Function as Friend Function

We can declare any global function as a friend function. The following example demonstrates how to declare a global function as a friend function in C++. The keyword “friend” is placed only in the function declaration of the friend function and not in the function definition or call.

Example

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

In the above example, we have used a global function as a friend function. A friend function can be declared in any section of the class i.e. public or private or protected.

2. Member Function of Another Class as Friend Function

We can also declare a member function of another class as a friend function in C++. Forward declaration of the class is needed if we want to make a member function of another class a friend inside that class.

Example

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 anotherClass::memberFunction(base& obj) {
    cout << "Private Variable: " << 
             obj.private_variable
         << endl;
    cout << "Protected Variable: " << 
             obj.protected_variable;
}

int main() {
    base object1;
    anotherClass object2;
    object2.memberFunction(object1);

    return 0;
}

Output
Private Variable: 10
Protected Variable: 99

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.

Function Friendly to Multiple Classes

C++
// C++ Program to demonstrate 
// how friend functions work as
// a bridge between the classes
#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

Friend Function vs Virtual Function

You might be confused between friend functions and virtual functions in C++ because both concepts deal with function accessibility and behavior, but they serve very different purposes.

Advantages of Friend Functions

Following is the list of advantages of friend functions in C++:

  • A friend function is able to access members without the need of inheriting the class.
  • The friend function acts as a bridge between two classes by accessing their private data.
  • It can be used to increase the versatility of overloaded operators.
  • It can be declared either in the public or private or protected part of the class.

Demerits of Friend Functions

The friend function provides us with a way to access private data, but it also has its demerits:

  • Friend functions have access to private members of a class from outside the class which violates the law of data hiding.
  • Friend functions cannot do any run-time polymorphism in their members.
  • Declaring too many functions or external classes as friends with access to a class’s private or protected data reduces the effectiveness of encapsulation. This compromises one of the core principles of object-oriented programming.
  • Friendship is not inherited. In layman's terms, if a base class has a friend function, then the function doesn’t become a friend of the derived class(es).

Next Article
Article Tags :
Practice Tags :

Similar Reads