Friend Class in C++
Definition
A
friend class in C++ is a class that has access to the private and protected
members of another class. Normally, private and protected members of a class
are only accessible to the class's own methods or friends. To make a class a
friend of another class, we use the keyword friend
inside the class definition.
Why Do
We Use Friend Classes?
We
use a friend class to allow another class access to private or protected
members of a class without making it a subclass or adding numerous getter and
setter functions.
Need
for Friend Classes
1.
Access Private Data:
A friend class can work directly with private or protected members of another
class.
2.
Convenience:
Eliminates the need for additional public methods to expose private data.
3.
Specialized Use:
Useful in scenarios where two or more classes need close collaboration and data
sharing.
Advantages
of Friend Classes
1.
Direct Access:
The friend class has direct access to private and protected members of the
other class.
2.
Simplifies Design:
Reduces the need for additional public functions to share data between classes.
3.
Improves Collaboration:
Facilitates close interaction between two tightly coupled classes.
Disadvantages
of Friend Classes
1.
Breaks Encapsulation:
It violates the principle of data hiding by allowing private data to be
accessed directly.
2.
Tight Coupling:
Increases dependency between classes, making the design harder to maintain.
3.
Not Inherited:
Friendship is not inherited, so derived classes do not automatically gain
access.
Example
of a Friend
Class
#include
<iostream>
using namespace
std;
class abc {
private:
int sal = 40000;
protected:
int pm = 5000;
public:
friend class AjaySir;
};
class AjaySir {
public:
void show(abc &obj) {
cout << "Salary is "
<< obj.sal << endl;
cout<<"Pocket
Money is " << obj.pm << endl;
}
};
int main() {
abc o1;
AjaySir f1;
f1.show(o1);
return 0;
}
How
Friend Classes Work?
- Declared
as a
friend
inside the class definition. - The
friend class gains access to all private and protected members of the
class.
- Methods
of the friend class use the object of the original class to interact with
private data.
Key in
mind at
the time of interview:
- Friendship
is not mutual: If
Class A
declaresClass B
as a friend,Class B
does not automatically makeClass A
its friend. - Friendship
is not transitive: If
Class A
is a friend ofClass B
andClass B
is a friend ofClass C
, it doesn’t meanClass A
is a friend ofClass C
. - Friendship
is not inherited: A derived class of a friend
class does not automatically gain friendship.
Real-Life
Example
A
house with locked rooms (private data) is accessible only to the owner (class
methods). However, the owner can give a trusted family (friend class) complete
access to the house. Members of the family can then access the private areas
directly.
For
instance, AjaSir (friend class) has access to your private and protected data
to manage tasks efficiently.
When
to Avoid Friend Classes?
1.
When getters and setters can achieve
the same functionality.
2.
When excessive use of friend classes
complicates the design and violates the principle of encapsulation
#Happy Coding keep learning
Comments
Post a Comment