Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 44

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

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.
← 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.
Next topicInheritance in C++ Your progress: 44 / 81 (54%) · Lesson 45 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free