Friend Functions in C++
In this lesson: What friend functions and friend classes are in C++ and when to use them — examples with output. In Hindi & English.
What is a friend function?
Private members are normally off-limits to outside code. A friend function is an exception: a non-member function that the class explicitly trusts, granting it access to private and protected members. The class declares it with the friend keyword.
Why friends exist
Sometimes a function that is not a member still genuinely needs the internals — for instance an operator comparing two objects, or a helper that reads private data from two different classes. Rather than making the data public for everyone, the class grants access to just that one function.
A working example
#include <iostream>
using namespace std;
class Account {
double balance = 1000; // private
friend void show(const Account &a); // grant access
};
void show(const Account &a) { // not a member
cout << "Balance: " << a.balance << "\n"; // can read private
}
int main() {
Account acc;
show(acc); // called like a normal function
return 0;
}Balance: 1000
Notice show is called on its own — not as acc.show() — because it is not a member.
Friend classes
A whole class can be a friend, so all its methods can reach the other's private members:
class Engine {
int power = 200;
friend class Car; // Car can see Engine's privates
};
class Car {
public:
void showPower(const Engine &e) { cout << e.power; }
};Friend vs member function
| Member function | Friend function | |
|---|---|---|
| Belongs to class? | Yes | No |
| Called as | obj.f() | f(obj) |
Has this? | Yes | No |
| Accesses privates? | Yes | Yes (granted) |
For a compact syntax example and operator use, see the friend function syntax reference.
When to use friends
- Operators that need both operands' private data (e.g.
operator<<). - Two closely related classes where one must read the other's internals.
- Only when a member function or a public getter would not fit as cleanly.
Friendship is a deliberate hole in encapsulation. A few friends are fine; many are a design smell.
Common mistakes
- Calling a friend function as
obj.f()— it is not a member. - Expecting friendship to be mutual or inherited (it is neither).
- Overusing friends and quietly destroying encapsulation.
- Forgetting the
frienddeclaration inside the class.
Write a Distance class storing metres privately, and a friend function add(const Distance&, const Distance&) that returns their sum. Confirm it can read the private metres of both objects.
Summary
- A friend function is a non-member granted access to private members.
- Declared with
friendinside the class, defined outside. - Called like a normal function, not on an object; it has no
this. - A friend class shares access with all its methods.
- Friendship is not mutual or inherited — use it sparingly.
Frequently Asked Questions
What is a friend function in C++?
friend keyword, but it is defined outside and is not called on an object like a member function.