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:
- The class
Box
declares the functiondisplayLength
as a friend using thefriend
keyword. - The friend function can access the private member
length
of theBox
object. - 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:
- The class
Rectangle
declares the classAreaCalculator
as a friend. - The friend class
AreaCalculator
can access the private memberswidth
andheight
ofRectangle
. - The function
calculateArea
uses the private members ofRectangle
to compute and return the area.
Key Points about friend
Keyword
- The
friend
keyword allows non-member functions or other classes to access private and protected members of a class. - Friendship is declared explicitly and does not follow inheritance rules.
- Friendship is not reciprocal; if Class A is a friend of Class B, Class B is not automatically a friend of Class A.
- 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.