Friend Function and Friend Class
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
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.Why are friend functions used in C++?
What is a friend class in C++?
Does friendship break encapsulation in C++?
Is friendship inherited or mutual in C++?
Friend function क्या है?
Private members सामान्यतः बाहरी code के लिए वर्जित होते हैं। Friend function एक अपवाद है: एक non-member function जिस पर class स्पष्ट रूप से भरोसा करती है, उसे private और protected members तक पहुँच देते हुए। Class इसे friend keyword से declare करती है।
Friends क्यों हैं
कभी कोई function जो member नहीं है फिर भी वाकई internals चाहता है — जैसे दो objects की तुलना करने वाला operator, या एक helper जो दो अलग classes का private data पढ़े। सबके लिए data public बनाने के बजाय, class केवल उस एक function को पहुँच देती है।
एक चलता उदाहरण
#include <iostream>
using namespace std;
class Account {
double balance = 1000; // private
friend void show(const Account &a); // access dein
};
void show(const Account &a) { // member nahi
cout << "Balance: " << a.balance << "\n"; // private padh sakta hai
}
int main() {
Account acc;
show(acc); // normal function ki tarah call
return 0;
}Balance: 1000
ध्यान दें show अपने आप call होता है — acc.show() नहीं — क्योंकि यह member नहीं है।
Friend classes
पूरी class friend हो सकती है, तो उसके सभी methods दूसरे के private members तक पहुँच सकते हैं:
class Engine {
int power = 200;
friend class Car; // Car, Engine ke privates dekh sakti hai
};
class Car {
public:
void showPower(const Engine &e) { cout << e.power; }
};Friend बनाम member function
| Member function | Friend function | |
|---|---|---|
| Class का है? | हाँ | नहीं |
| Call ऐसे | obj.f() | f(obj) |
this है? | हाँ | नहीं |
| Privates access? | हाँ | हाँ (दी गई) |
संक्षिप्त syntax उदाहरण और operator इस्तेमाल के लिए, friend function syntax reference देखें।
Friends कब इस्तेमाल करें
- ऐसे operators जिन्हें दोनों operands का private data चाहिए (जैसे
operator<<)। - दो घनिष्ठ संबंधित classes जहाँ एक को दूसरे के internals पढ़ने हों।
- केवल तब जब member function या public getter इतनी साफ़ तरह फिट न बैठे।
Friendship encapsulation में जानबूझकर एक छेद है। कुछ friends ठीक हैं; बहुत सारे design की गंध हैं।
आम गलतियाँ
- Friend function को
obj.f()की तरह call करना — यह member नहीं है। - Friendship को mutual या inherited मानना (यह न कोई है)।
- Friends का अति-इस्तेमाल करके चुपचाप encapsulation नष्ट करना।
- Class के अंदर
frienddeclaration भूलना।
एक Distance class लिखें जो metres private रखे, और एक friend function add(const Distance&, const Distance&) जो उनका योग लौटाए। पुष्टि करें कि यह दोनों objects के private metres पढ़ सकता है।
सारांश
- Friend function एक non-member है जिसे private members तक पहुँच दी गई।
- Class के अंदर
friendसे declare, बाहर परिभाषित। - Normal function की तरह call, object पर नहीं; इसके पास
thisनहीं। - Friend class अपने सभी methods के साथ पहुँच साझा करती है।
- Friendship mutual या inherited नहीं — कम इस्तेमाल करें।