C++ friend functions and friend classes
In C++, friend functions and friend classes are used to give specific functions or classes access to the private and protected members of another class. Although C++ provides access control using public, private, and protected access specifiers, sometimes it is necessary to allow certain functions or other classes to access private data. This is where friend functions and friend classes come in.
A friend function is not a member of a class but can access its private and protected members. A friend class is a class whose methods can access the private and protected members of another class.
A friend function in C++ is a function that is not a member of a class but is given access to the class’s private and protected members. The friend function can be a regular function or a member function of another class.
friend
keyword inside the class whose members it will access.To declare a function as a friend of a class, the friend
keyword is used in the class definition.
class ClassName {
friend returnType functionName(parameters);
};
Example of a Friend Function:
#include <iostream>
using namespace std;
class Box {
private:
double length;
double width;
public:
Box(double l, double w) : length(l), width(w) {} // Constructor to initialize the dimensions
// Declaring the function `calculateArea` as a friend function
friend double calculateArea(Box b);
};
// Friend function definition
double calculateArea(Box b) {
return b.length * b.width; // Accessing private members of Box
}
int main() {
Box box1(5.0, 3.0);
cout << "Area of the box: " << calculateArea(box1) << endl; // Output: Area of the box: 15
return 0;
}
Explanation:
Box
class has private members length
and width
, and we want to calculate the area of the box using a function that is not a member of the Box
class.calculateArea()
as a friend function, giving it access to the private members of Box
.calculateArea
function can access the private members directly, even though it's not a member of the Box
class.A friend class in C++ is a class that is allowed to access the private and protected members of another class. When one class is declared as a friend of another, all the member functions of the friend class can access the private and protected members of the class.
friend
keyword.To declare a class as a friend, the friend
keyword is used followed by the class name.
class ClassName {
friend class FriendClassName;
};
Example of a Friend Class:
#include <iostream>
using namespace std;
class Box {
private:
double length;
double width;
public:
Box(double l, double w) : length(l), width(w) {}
// Declaring the `Helper` class as a friend class
friend class Helper;
};
class Helper {
public:
double calculateArea(Box b) {
return b.length * b.width; // Accessing private members of Box
}
void displayDimensions(Box b) {
cout << "Length: " << b.length << ", Width: " << b.width << endl; // Accessing private members of Box
}
};
int main() {
Box box1(5.0, 3.0);
Helper helper;
// Using a friend class method to calculate area and display dimensions
cout << "Area of the box: " << helper.calculateArea(box1) << endl; // Output: Area of the box: 15
helper.displayDimensions(box1); // Output: Length: 5, Width: 3
return 0;
}
Explanation:
Box
class has private members length
and width
.Helper
class is declared as a friend class of Box
, which allows all the methods in the Helper
class to access the private members of Box
.Helper
class methods can access and manipulate the Box
class’s private data directly.Friend functions and friend classes are useful in the following scenarios:
When two or more classes are tightly coupled and need to access each other’s private or protected data, you can declare them as friend classes. This allows them to interact closely without exposing sensitive data to the outside world.
Friend functions can be used when you have a utility function that operates on multiple objects of different classes and needs access to their private data. Rather than making the function a member of each class, you can declare it as a friend.
Sometimes, friend functions may be used to avoid unnecessary getters and setters when performance is critical, particularly when multiple functions need to access internal data directly.
In cases where operator overloading is needed, friend functions are often used to provide direct access to the private data of a class. This is particularly common with binary operators.
While friend functions and friend classes provide powerful capabilities, they come with some limitations: