Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 44

Friend Function and Friend Class

Written and reviewed by · 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

C++
#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;
}
Output:
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:

C++
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 functionFriend function
Belongs to class?YesNo
Called asobj.f()f(obj)
Has this?YesNo
Accesses privates?YesYes (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.
💡 Use sparingly

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 friend declaration inside the class.
🏋️ Practice

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 friend inside 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++?
A friend function is a non-member function that a class grants special permission to access its private and protected members. You declare it inside the class with the 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++?
They are used when a function that is not a member still needs access to a class's private data — for example an operator that works on two objects, or a helper that must read internal state of more than one class. Friendship grants that access without making the data public.
What is a friend class in C++?
A friend class is a class whose member functions can all access the private and protected members of another class that declared it a friend. It is used when two classes are tightly related and one legitimately needs full access to the other's internals.
Does friendship break encapsulation in C++?
Friendship relaxes encapsulation deliberately and in a controlled way: only the specific functions or classes named as friends get access, and the class itself grants that permission. Used sparingly it is safe, but overusing friends weakens the benefits of keeping data private.
Is friendship inherited or mutual in C++?
No. Friendship is neither inherited nor automatically mutual. If class A declares B a friend, B's access to A does not mean A can access B, and classes derived from B are not automatically friends of A. Each friendship must be granted explicitly.

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 को पहुँच देती है।

एक चलता उदाहरण

C++
#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;
}
Output:
Balance: 1000

ध्यान दें show अपने आप call होता है — acc.show() नहीं — क्योंकि यह member नहीं है।

Friend classes

पूरी class friend हो सकती है, तो उसके सभी methods दूसरे के private members तक पहुँच सकते हैं:

C++
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 functionFriend 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 के अंदर friend declaration भूलना।
🏋️ अभ्यास

एक 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 नहीं — कम इस्तेमाल करें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.