Friend Function & Class
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
Quick recap
A friend function is a non-member that a class trusts with access to its private data. This page is a practical syntax reference; for the full concept, when to use friends, and friend classes, see the main friend functions lesson.
Declaration syntax
Put friend before the prototype inside the class; define the body outside with no friend and no scope prefix:
class Box {
int w = 5;
friend void print(const Box &b); // declaration
};
void print(const Box &b) { // definition (no Box::)
cout << b.w;
}Three friend forms
| Form | Declaration |
|---|---|
| Free function | friend void f(const T&); |
| Another class's method | friend void C::m(const T&); |
| Whole class | friend class C; |
Friend for operator<<
The classic real use: printing with cout << obj. Because the stream is the left operand, the operator must be a non-member — so it is made a friend to reach private data:
#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int a, int b) : x(a), y(b) {}
friend ostream& operator<<(ostream &os, const Point &p);
};
ostream& operator<<(ostream &os, const Point &p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
int main() {
Point p(3, 4);
cout << p << "\n"; // (3, 4)
}(3, 4)
See operator overloading for the wider topic.
Reading two objects
A friend can also read the privates of two objects at once — handy for comparisons or sums:
class Money {
int paise;
public:
Money(int p) : paise(p) {}
friend int total(const Money &a, const Money &b) {
return a.paise + b.paise; // both privates
}
};Common mistakes
- Adding
ClassName::before a friend's definition — it is not a member. - Writing
friendagain on the outside definition (only in the declaration). - Making
operator<<a member — the stream must be the left operand. - Forgetting to
returnthe stream fromoperator<<.
Give a Time class private hours and minutes, then write a friend operator<< that prints it as hh:mm. Test with cout << t;.
Summary
- Declare with
friendinside the class; define outside with no scope prefix. - Three forms: free function, another class's method, or a whole class.
- Friends are the standard way to write
operator<<for a class. - A friend can read the private data of two objects at once.
- For the concept and design guidance, see the main friend functions lesson.
Frequently Asked Questions
How do you declare a friend function in C++?
friend before the function's prototype, such as friend void print(const Box &b);. The function is then defined outside the class without the friend keyword and without the class scope, because it is not a member.How is a friend function used for operator overloading in C++?
operator<< that must take the stream on the left and the object on the right, which a member function cannot do. Declaring the operator a friend lets it access the object's private data while still being a non-member.Why use a friend function for operator<< instead of a member?
cout << obj has the stream as the left operand, the operator must be a non-member so its first parameter can be the stream. Making it a friend gives it access to the object's private members while keeping the natural stream << object syntax.Can a friend function be a member of another class in C++?
friend void OtherClass::method(...). This grants just that one method access, which is more restrictive and often cleaner than making the whole other class a friend.Where is a friend function defined in C++?
friend declaration goes inside the class, but the function body is defined outside the class like an ordinary non-member function, with no ClassName:: prefix. It can be defined inline inside the class body too, though it is still treated as a non-member.त्वरित सार
Friend function एक non-member है जिस पर class अपने private data तक पहुँच का भरोसा करती है। यह पृष्ठ एक व्यावहारिक syntax reference है; पूरे concept, friends कब इस्तेमाल करें, और friend classes के लिए, मुख्य friend functions lesson देखें।
Declaration syntax
Class के अंदर prototype से पहले friend रखें; body बाहर बिना friend और बिना scope prefix परिभाषित करें:
class Box {
int w = 5;
friend void print(const Box &b); // declaration
};
void print(const Box &b) { // definition (Box:: nahi)
cout << b.w;
}तीन friend रूप
| रूप | Declaration |
|---|---|
| Free function | friend void f(const T&); |
| अन्य class का method | friend void C::m(const T&); |
| पूरी class | friend class C; |
operator<< के लिए friend
Classic असली इस्तेमाल: cout << obj से printing। चूँकि stream बायाँ operand है, operator को non-member होना चाहिए — तो इसे private data तक पहुँचने को friend बनाया जाता है:
#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int a, int b) : x(a), y(b) {}
friend ostream& operator<<(ostream &os, const Point &p);
};
ostream& operator<<(ostream &os, const Point &p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
int main() {
Point p(3, 4);
cout << p << "\n"; // (3, 4)
}(3, 4)
व्यापक विषय के लिए operator overloading देखें।
दो objects पढ़ना
Friend एक साथ दो objects के privates भी पढ़ सकता है — तुलना या योग के लिए काम का:
class Money {
int paise;
public:
Money(int p) : paise(p) {}
friend int total(const Money &a, const Money &b) {
return a.paise + b.paise; // dono privates
}
};आम गलतियाँ
- Friend की definition से पहले
ClassName::जोड़ना — यह member नहीं है। - बाहरी definition पर फिर
friendलिखना (केवल declaration में)। operator<<को member बनाना — stream बायाँ operand होना चाहिए।operator<<से streamreturnकरना भूलना।
एक Time class को private hours और minutes दें, फिर एक friend operator<< लिखें जो इसे hh:mm के रूप में print करे। cout << t; से test करें।
सारांश
- Class के अंदर
friendसे declare; बाहर बिना scope prefix परिभाषित। - तीन रूप: free function, अन्य class का method, या पूरी class।
- Friends किसी class के लिए
operator<<लिखने का मानक तरीका हैं। - Friend एक साथ दो objects का private data पढ़ सकता है।
- Concept और design मार्गदर्शन के लिए, मुख्य friend functions lesson देखें।