Source: Safalta.comThe term "friend" appears only in the function declaration of the friend function, not in the definition.When the friend function is called, neither the object's name nor the dot operator are used. Friends should only be utilised for particular purposes. When too many functions or external classes are defined as friends of a class with protected or private data, the benefit of encapsulating various classes in object-oriented programming is diminished.
Free Demo Classes
Register here for Free Demo Classes
1. Introduction to Digital Marketing
A friend class has access to secret and protected members of the class to which it has been designated as a friend. Allowing a specific class to access private members of another class is occasionally beneficial. For example, a LinkedList class may be granted access to Node's secret members. A friend class has access to both private and protected members of the class that has declared it as a friend.
Place the following statement in the definition of class ClassOne to declare all member functions of class ClassTwo as friends of class ClassOne:
friend class ClassTwo;
class buddy ClassTwo;
The outcome of compiling and running the aforementioned code is as follows:Width of box : 10
A straightforward and comprehensive C++ application that shows how friends are used in another class
OutputB::b = 0
You can also check