Friend Class in C++

The private and protected class members are not allowed to be accessed from outside the class. Only inherited or derived classes can access the protected members of its parent or base class. No other class or function can access them. But a friend class can access the private and protected members of a class where it is declared as a friend.

Let’s understand the need for a friend class.

class Point {
public:
    Point() {
        x = y = 0;
    }

    Point(int x, int y) {
        this->x = x;
        this->y = y;
    }
private:
    int x;
    int y;
};

class Line {
public:
    Line(Point &p1, Point &p2) {
        this->p1 = p1;
        this->p2 = p2;
    }

    double length() {
        // return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
    }

private:
    Point p1;
    Point p2;
};

In this example, we have two classes. The Point class represents a point in a coordinate system. It has two member variables – x and y – to represent a position on a 2D plane. The Line class contains two Point objects to represent two end points of the line.

The Line class has a function, length(), to return the length of the line. To calculate the length, the function needs to access the x and y member variables of its point objects. But this variables are private in Point class. So, the function can not access them by default. It is also not a good idea to make them public. it will increase the risk of accidental changes from many places.

One way to solve this problem is to grant access of the private members of Point only to the Line class. We can make the Line class a friend of the Point class.

Friend Class Example

#include <iostream>
#include <cmath>

using namespace std;

class Point {
public:
    Point() {
        x = y = 0;
    }

    Point(int x, int y) {
        this->x = x;
        this->y = y;
    }
private:
    int x;
    int y;

    friend class Line;
};

class Line {
public:
    Line(Point &p1, Point &p2) {
        this->p1 = p1;
        this->p2 = p2;
    }

    double length() {
        return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
    }

private:
    Point p1;
    Point p2;
};

int main() {
    Point p1(10, 15);
    Point p2(20, 25);
    Line l1(p1, p2);

    cout << "Length of the line: " << l1.length() << endl;

    return 0;
}

In the Point class, we declared Line as a friend class by putting a ‘friend‘ keyword in front of the class name.

Now the length() function can access the private member variables of Point and calculate the length of the line.

$ g++ -o test test.cpp 
$ ./test 
Length of the line: 14.1421

Note: As Line is a friend class of Point, any member function of Line can access the private members of Point.

Friend Access and Encapsulation

Friend access is not really a compromise of data encapsulation. Rather, it extends private access of a class to its components without allowing access to other classes. In our example, the Line class owns the Point objects. The Line class’s access is extended to the private members of its components, the point objects.

But it needs to be used carefully. In this example, we should not allow the Point class to access the private members of Line. Because Point class does not own Line.

Similar way, we can make a friend function also.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
0
0