Friend Function and Friend Class in C++ – Complete Tutorial
Last Updated on: 18th Dec 2025 18:10:43 PM
C++ follows the principle of data hiding, where class members are usually kept private.
However, in some real-world situations, trusted external functions or classes need access to private data.
For this purpose, C++ provides:
-
Friend Function
-
Friend Class
These features allow controlled access to private and protected members.
A friend function is a non-member function that is allowed to access the private and protected members of a class. Even though it is not part of the class, it behaves like a trusted helper.
Friend Function – Definition
A friend function is a function that is declared using the friend keyword inside a class and can access its private and protected data.
Real-life example (Friend Function)
A bank auditor:
-
Not an employee of the bank
-
But allowed to access confidential account details
1. Declaring and Using a Friend Function
Concept Explanation
-
Friend function is declared inside the class using
friend -
It is defined outside the class
-
It is called like a normal function
Example
#include <iostream>
using namespace std;
class BankAccount {
private:
int balance;
public:
BankAccount(int b) {
balance = b;
}
friend void showBalance(BankAccount acc);
};
void showBalance(BankAccount acc) {
cout << "Account Balance: ₹" << acc.balance << endl;
}
int main() {
BankAccount account(12000);
showBalance(account);
return 0;
}
Output
Account Balance: ₹12000
Explanation
The friend function accesses the private balance of the class directly.
Key Points about Friend Function
-
Not a class member
-
Can access private data
-
Declared inside class
-
Defined outside class
-
Called like a normal function
Friend Class – Concept Explanation
A friend class is a class that can access all private and protected members of another class. Once a class is declared as a friend, all its member functions gain access.
Friend Class – Definition
A friend class is a class whose objects can access private and protected members of another class.
Real-life example (Friend Class)
A hospital management system:
-
Doctor class accesses patient’s private medical records
1. Declaring and Using a Friend Class
Concept Explanation
-
Friend class is declared using friend class
-
All functions of friend class can access private data
Example
#include <iostream>
using namespace std;
class Patient {
private:
string disease;
public:
Patient(string d) {
disease = d;
}
friend class Doctor;
};
class Doctor {
public:
void diagnose(Patient p) {
cout << "Patient Disease: " << p.disease << endl;
}
};
int main() {
Patient p("Viral Fever");
Doctor d;
d.diagnose(p);
return 0;
}
Output
Patient Disease: Viral Fever
Explanation
The Doctor class accesses the private data of Patient using friendship.
Friend Function vs Friend Class
| Feature | Friend Function | Friend Class |
|---|---|---|
| Access Scope | Single function | All functions of class |
| Declaration | friend void func() | friend class ClassName |
| Use case | Limited access | Full class access |
| Control | More restricted | Less restricted |
Important Rules of Friendship
-
Friendship is not mutual
-
Friendship is not inherited
-
Friendship is not transitive
-
Friendship breaks encapsulation (use carefully)
Advantages of Friend
-
Controlled access
-
Improves flexibility
-
Useful for operator overloading
-
Supports real-world relationships
Disadvantages of Friend
-
Breaks data hiding
-
Reduces security
-
Increases dependency
Summary
-
Friend function accesses private data of a class
-
Friend class grants access to all its members
-
Useful when two entities work closely
-
Should be used only when necessary
Keep practicing — you're doing amazing!
Happy Coding! ![]()