C is not object oriented programming language thus there are no friend functions in C.
In C++, which is object oriented, the advantage of a friend is that it allows code that cannot otherwise be regarded as being a member of the class to gain private access to the class representation, where public access to the representation would be considered undesirable. Although many see friendship as an undermining of encapsulation, the reality is it strengthens encapsulation by limiting exposure to the representation to only those functions that actually require that access. Public access is fully accessible, of course, but is not always desirable. Interfaces must be kept as simple as possible, but exposing an interface that is only of use to a few non-member functions is less desirable than simply making those functions friends of the class.
It is important to understand the relationships between functions and classes. All functions can be divided into one of three distinct categories with respect to any one class: non-member functions; static member functions or; nonstatic member functions.
Nonstatic member functions of a class have all three of the following properties:
1. The function has private access to the class representation.
2. The function is scoped to the class.
3. The function must be invoked upon an object of the class (has a 'this' pointer).
Static member functions only have the first two properties while friend functions only have the first property. External (non-friend) functions do not have any of these properties.
The only disadvantage to a friend function is when that friend is outwith the control of the class designer. Since friends have private access they must obey the class invariants, just as any member of the class must. Although they are not members of the class per se, they must be implemented just as if they were. In other words, they must not invalidate the representation. When the class designer has full control over the friends of that class then this is not a major problem. But when third-party programmers have access to the friend implementations, encapsulation can no longer be guaranteed by the class designer -- it is seriously undermined. But when used appropriately, friends actively re-enforce encapsulation and are no more an undermining of encapsulation than is public inheritance.
A friend function can use any of the private data of an object without going thru a mutator or gateway function. As a result, since it can do anything it wants with the private data of an object there is no built-in checking on validity, etc.
But, then again, that is what the friend function is for. An old saying revolving around this is that you need to trust your friends, and your friend better be a 'very good friend' to you (the object class).
There are no disadvantages as such. Some will tell you at great length that friends undermine encapsulation but this is errant nonsense. Friends no more undermine encapsulation than do members of the class itself, and friends are effectively members of the class. They extend the class interface, nothing more.
The only disadvantage is misuse. When used correctly, friend functions extend a class interface. But if a friend function undermines encapsulation, then there is a clear design flaw in the class, and the use of a friend function should be reconsidered.
It coincide the concept of data hiding
OOPS is learn to hard and not easy to new developer.
No
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:
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++.
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
Every C plus plus program that is a main program must have the function 'main'.
Control is returning to the caller of the function.