Friend Function in C++
Introduction
A friend function is a function that is not a member of a class, but it is allowed to access the private and protected members of that class.
A friend function is declared inside the class using the friend keyword.
Definition
A friend function is a non-member function that has permission to access the private and protected members of a class.
1. Syntax
class ClassName { private: int data; public: friend void functionName(ClassName object); };
The function is declared using the friend keyword inside the class.
2. Simple Friend Function Example
#include <iostream> using namespace std; class Student { private: int age; public: Student() { age = 22; } friend void displayAge(Student s); }; void displayAge(Student s) { cout << "Age: " << s.age << endl; } int main() { Student s1; displayAge(s1); return 0; }
Output
Age: 22
Here, displayAge() is not a member of Student, but it can access the private member age because it is declared as a friend.
3. Friend Function with Parameters
A friend function can receive an object as a parameter.
Example
#include <iostream> using namespace std; class Number { private: int value; public: Number(int v) { value = v; } friend int add(Number n1, Number n2); }; int add(Number n1, Number n2) { return n1.value + n2.value; } int main() { Number n1(10); Number n2(20); cout << "Sum = " << add(n1, n2); return 0; }
Output
Sum = 30
The add() function accesses the private value of both objects.
4. Friend Function Accessing Private and Protected Members
A friend function can access both private and protected members.
#include <iostream> using namespace std; class Demo { private: int privateData = 10; protected: int protectedData = 20; public: friend void display(Demo d); }; void display(Demo d) { cout << "Private data: " << d.privateData << endl; cout << "Protected data: " << d.protectedData << endl; } int main() { Demo d; display(d); return 0; }
Output
Private data: 10 Protected data: 20
5. Friend Function vs Member Function
| Friend Function | Member Function |
|---|---|
| It is not a member of the class. | It is a member of the class. |
Declared using friend. | Does not use friend. |
| Can access private and protected members. | Can access private and protected members. |
| Called like a normal function. | Usually called using an object. |
Does not have a this pointer. | Has a this pointer. |
Example
Friend function:
display(s1);
Member function:
s1.display();
6. Friend Function with Two Classes
A friend function can access private members of more than one class.
Example
#include <iostream> using namespace std; class B; class A { private: int valueA; public: A() { valueA = 10; } friend int add(A a, B b); }; class B { private: int valueB; public: B() { valueB = 20; } friend int add(A a, B b); }; int add(A a, B b) { return a.valueA + b.valueB; } int main() { A a; B b; cout << "Sum = " << add(a, b); return 0; }
Output
Sum = 30
Here, add() can access private members of both A and B.
7. Friend Class
C++ also allows an entire class to be declared as a friend.
A friend class can access the private and protected members of another class.
Example
#include <iostream> using namespace std; class Student { private: int marks = 90; friend class Teacher; }; class Teacher { public: void display(Student s) { cout << "Marks = " << s.marks << endl; } }; int main() { Student s; Teacher t; t.display(s); return 0; }
Output
Marks = 90
Here, Teacher is a friend class of Student.
8. Important Properties of Friend Functions
A friend function:
- Is declared using the
friendkeyword. - Is not a member of the class.
- Can access private members.
- Can access protected members.
- Can be called like a normal function.
- Does not use the
thispointer. - Can be declared in the
public,private, orprotectedsection of a class. - Friendship is granted by the class; it is not automatically mutual.
9. Advantages of Friend Functions
1. Access to Private Data
A friend function can access private and protected members.
2. Useful for Operator Overloading
Friend functions are commonly used when implementing certain operator overloads.
3. Can Work with Multiple Classes
A single friend function can access private members of multiple classes when each class grants friendship.
4. Useful for Closely Related Classes
Friendship can be useful when two classes need to cooperate closely.
10. Disadvantages of Friend Functions
1. Reduces Data Hiding
Because a friend function can access private data, excessive use can weaken encapsulation.
2. Less Object-Oriented
It is not a member function, so it does not operate through the normal member-function interface.
3. Can Increase Coupling
A friend function may become strongly dependent on a class's internal implementation.
Therefore, friend functions should be used only when they provide a clear design benefit.
11. Real-Life Example
Consider a bank system where an authorized auditing function needs to inspect private account information.
#include <iostream> using namespace std; class BankAccount { private: double balance; public: BankAccount(double b) { balance = b; } friend void audit(BankAccount account); }; void audit(BankAccount account) { cout << "Account balance: " << account.balance << endl; } int main() { BankAccount account(50000); audit(account); return 0; }
Output
Account balance: 50000
Key Points
- A friend function is a non-member function.
- It is declared using the
friendkeyword. - It can access private and protected members.
- It is called like a normal function.
- It does not have a
thispointer. - A friend function can be associated with one or more classes.
- A class can also declare another class as its friend.
- Friendship should be used carefully because it can weaken encapsulation.
Summary
Friend Function | ┌─────────┴─────────┐ ↓ ↓ Non-member Declared with function friend | ↓ Accesses private and protected members
Simple Definition
Friend Function = A non-member function that is granted access to a class's private and protected members.