Friend class
Encyclopedia
A friend class in C++
, can access the "private" and "protected" members of the class in which it is declared as a friend. On declaration of friend class all member function of the friend class become friend of the class in which the friend class was declared. An important point about friendship in case of friend class is that friendship is not inherited we have to declare every friendship explicitly. Friend class
help in improving encapsulation
if used wisely.
. The friend class has the same level of access irrespective of whether the friend declaration appears in either the public, protected or private sections of the class definition. Friend status is granted by using the class keyword:
class Y{
friend class Z;
}
}
is equivalent to
class Z
class X{
class Y{
friend class Z;
};
};
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, can access the "private" and "protected" members of the class in which it is declared as a friend. On declaration of friend class all member function of the friend class become friend of the class in which the friend class was declared. An important point about friendship in case of friend class is that friendship is not inherited we have to declare every friendship explicitly. Friend class
help in improving encapsulation
Encapsulation
- Chemistry :* Molecular encapsulation, in chemistry, the confinement of an individual molecule within a larger molecule* Capsule , in pharmacy, the enclosure of a medicine within a relatively stable shell for administration...
if used wisely.
Declaration
Classes are declared as friends within the definition of the class to whom access is to be given; this prevents a class from giving itself access to another's protected members, which enforces encapsulationEncapsulation (object-oriented programming)
In a programming language encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:* A language mechanism for restricting access to some of the object's components....
. The friend class has the same level of access irrespective of whether the friend declaration appears in either the public, protected or private sections of the class definition. Friend status is granted by using the class keyword:
Example
Advantages Of Using Friend Class
- Sometimes private datamembers are needed to be accessed and used by two different classes simultaneously. At that time, we can use friend function which can access private and protected data members of both the classes. For that purpose,it should be declared as 'friend' in both classes. They should not be member of any one of the classes.
- It provides additional functionality which is kept outside the class.
- It provides functions with data which is not normally used by the class.
- It allows sharing private class information by a non member function.
Scope
The class name which is introduced in the friend class declaration its scope is not in class granting friendship and also is not the member of the class granting access. If the name of the friend class is declared before the friend class declaration then the compiler searches for the class with the same name as the friend class at the scope of friend declaration. If the name of the friend class is same as that of the enclosing class which is nested then the nested class is a friend of enclosing class.Example
class X{class Y{
friend class Z;
}
}
is equivalent to
class Z
class X{
class Y{
friend class Z;
};
};
Features
- Friendships are not corresponded – If class
A
is a friend of classB
, classB
is not automatically a friend of classA
. - Friendships are not transitive – If class
A
is a friend of classB
, and classB
is a friend of classC
, classA
is not automatically a friend of classC
. - Friendships are not inherited – A friend of class
Base
is not automatically a friend of classDerived
and vice versa; equally ifBase
is a friend of another class,Derived
is not automatically a friend and vice versa. - Access due to friendship is inherited – A friend of
Base
can access the restricted members ofDerived
that were inherited fromBase
. Note though that a friend ofDerived
only has access to members inherited fromBase
to which Derived has access itself, e.g. ifDerived
inherits publicly fromBase
,Derived
only has access to the protected (and public) members inherited fromBase
, not the private members, so neither does a friend.
Friend and Encapsulation
It is generally said that friend classes violate encapsulation as it has an access to internals of the class in which it is declared. A friend allows a class a by hiding details that may be needed by anything but the friends of the class.External links
- http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr043.htm
- http://www.cplusplus.com/doc/tutorial/inheritance/