C++ Friend Function
A friend function of a class is defined outside that class scope but it has the right to access all private
and protected members of the class. Even though the prototypes for friend functions appear in the
class definition, friends are not member functions.A friend can be a function, function template,
member function, or a class or class template, in which case the entire class and all of its members
are friends.
Declaring Friend Function
To declare a function as a friend of a class, precede the function prototype in the class definition with
the keyword friend as follows −
When the above code is compiled and executed, it produces the following result −
Width of box : 10
Accessing Private and Protected Members
The private and protected members of a class are not accessible outside of the class. Still, if you want
to access them, you can use the friend function. The friend function provides the ability to directly
access the class's private and protected members.
Friend Function vs Member Function
In C++, both friend functions and member functions are used to access and manipulate the data of a
class, but still, they have significant differences in their scope and usage.
Friend Function
A friend Function is a non-member function that is declared inside a class using the "friend"
keyword, it has special access to the class's private and protected members. Since it's not a member
it is not bound to a specific object, can't overloaded based on objects, not use this pointer, and
cannot be inherited by derived classes. They are defined outside the class but declared inside it.
Member function
Whereas the member function is defined within the class and operates using this pointer. It can
access all members of the class (private, protected, and public), and as it is tied to class objects, it
can be overloaded and inherited by derived classes.
Features of Friend Functions
• A friend function is a special function in C++ that in spite of not being a member function of
a class has the privilege to access the private and protected data of a class.
• A friend function is a non-member function or ordinary function of a class, which is declared
as a friend using the keyword “friend” inside the class. By declaring a function as a friend, all
the access permissions are given to the function.
• The keyword “friend” is placed only in the function declaration of the friend function
and not in the function definition or call.
• A friend function is called like an ordinary function. It cannot be called using the object name
and dot operator. However, it may accept the object as an argument whose value it wants to
access.
• A friend function can be declared in any section of the class i.e. public or private or
protected.
1. Friends should be used only for limited purposes. Too many functions or external classes are
declared as friends of a class with protected or private data access lessens the value of
encapsulation of separate classes in object-oriented programming.
2. Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A
automatically.
3. Friendship is not inherited.
4. #include <iostream>
5. using namespace std;
6.
7. class Distance {
8. private:
9. int meter;
10.
11. // friend function
12. friend int addFive(Distance);
13.
14. public:
15. Distance() : meter(0) {}
16.
17. };
18.
19. // friend function definition
20. int addFive(Distance d) {
21.
22. //accessing private members from the friend function
23. d.meter += 5;
24. return d.meter;
25. }
26.
27. int main() {
28. Distance D;
29. cout << "Distance: " << addFive(D);
30. return 0;
31. }