C++ friend Keyword

The friend keyword in C++ is used to grant a non-member function or another class access to the private and protected members of a class. By default, these members are not accessible outside the class, but using the friend keyword provides a way to override this restriction.

Friendship is not reciprocal or transitive. If Class A declares Class B as its friend, the reverse is not true unless explicitly declared. Similarly, if Class A is a friend of Class B and Class B is a friend of Class C, Class A does not automatically become a friend of Class C.


Syntax

</>
Copy
class ClassName {
    friend return_type friend_function_name(parameters);
    friend class FriendClassName;
};
friend
The keyword used to declare a friend function or class.
friend_function_name
The name of the non-member function that will be granted access to private and protected members.
FriendClassName
The name of the class that will be granted access to private and protected members.

Examples

Example 1: Friend Function

In this example, we will learn how a friend function can access private members of a class.

</>
Copy
#include <iostream>
using namespace std;

class Box {
private:
    int length;

public:
    Box(int l) : length(l) {}

    friend void displayLength(Box b); // Friend function declaration
};

void displayLength(Box b) {
    cout << "Length: " << b.length << endl; // Accessing private member
}

int main() {
    Box box(10);
    displayLength(box); // Call the friend function
    return 0;
}

Output:

Length: 10

Explanation:

  1. The class Box declares the function displayLength as a friend using the friend keyword.
  2. The friend function can access the private member length of the Box object.
  3. In the main function, the friend function is called to display the private member’s value.

Example 2: Friend Class

In this example, we will learn how a friend class can access private members of another class.

</>
Copy
#include <iostream>
using namespace std;

class Rectangle {
private:
    int width, height;

public:
    Rectangle(int w, int h) : width(w), height(h) {}

    friend class AreaCalculator; // Friend class declaration
};

class AreaCalculator {
public:
    int calculateArea(Rectangle r) {
        return r.width * r.height; // Accessing private members
    }
};

int main() {
    Rectangle rect(5, 3);
    AreaCalculator calc;

    cout << "Area: " << calc.calculateArea(rect) << endl;
    return 0;
}

Output:

Area: 15

Explanation:

  1. The class Rectangle declares the class AreaCalculator as a friend.
  2. The friend class AreaCalculator can access the private members width and height of Rectangle.
  3. The function calculateArea uses the private members of Rectangle to compute and return the area.

Key Points about friend Keyword

  1. The friend keyword allows non-member functions or other classes to access private and protected members of a class.
  2. Friendship is declared explicitly and does not follow inheritance rules.
  3. Friendship is not reciprocal; if Class A is a friend of Class B, Class B is not automatically a friend of Class A.
  4. Friendship is not transitive; if Class A is a friend of Class B and Class B is a friend of Class C, Class A is not automatically a friend of Class C.