Declaring a function to be a friend of a class allows the friend function to gain private (and protected) access to that class, just as it would if it were declared a static member function of the class. Most of the time you will want to use static functions rather than friend functions, but sometimes it is necessary for a member method of another class or an external function to be permitted private access. Although some will tell you this undermines the encapsulation of the class, it does not. If anything, it reinforces that encapsulation, by only allowing the specified function to gain private access. Although the friend function is not physically a member of the class, it automatically becomes tightly bound to the class. That is not necessarily a bad thing when you're only dealing with a limited number of friends, but a class that has many friends can often be an indication of poor class design. Thus it is important to limit friendship and to only use it when it is absolutely required, whenever possible.
By way of an example, suppose you have designed a parent and child class. Even without friend functions, the two are implicitly bound to each other: the parent object is a container for child objects, while child objects refer to their parents. All work is delegated and coordinated between the two classes accordingly; they work together as one. Most of the work can be encapsulated without the need to explicitly bind the classes together, however child objects typically refer to their parent object (via a member pointer) and might also be permitted to change their parent, but we wouldn't want to expose that pointer outside of the class, nor would we want to expose any mutators such as child::set_parent() outside of the class. But since the two classes are imlicitly bound to each other, we can declare specific methods of the parent class to be friends of the child class, such as parent::add_child(). In this way we can be sure that whenever we add a child to a parent, the parent not only attaches the child to itself, but also detaches the child from any existing parent beforehand. Far from undermining the encapsulation, this friendship reinforces it, assuring us that both classes are tightly bound.
A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.
Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.
Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.
There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.
...a function call.
Something like this:
There is no "power" operator in C or C++. You need to the use the math library function pow().
A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.
The keyword "friend" allows a function or variable to have access to a protected member inside a class.
Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.
There is no such term as "building function" in C++.
Yes, there can be friend functions in C++.
You need to #include the header file that contains the missing function's declaration.
Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.
There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.
...a function call.
yes,we can make function inline