The Friend function and friend class are the techniques used to access the private members of a class by using friend keyword. The common difference between friend function and friend class is that when friend function is used the private class members can be accessed but in friend class, only the names of the friend class is accessed not the private members of the class.
The friend feature whether used by function or class can produce a negative impact on the object-oriented framework as it weakens the encapsulation which is against the object-oriented paradigm. This is the reason the friend feature must be used wisely otherwise it could break the data hiding of the code.
This friend feature is neither commutative and nor transitive. X is a friend of Y doesn’t infer that Y is also a friend of X. If X is a friend of Y and Y is a friend of Z, does not implicate that X is a friend of Z.
Content: Friend Function Vs Friend Class
Comparison Chart
Basis for comparison | Friend Function | Friend Class |
---|---|---|
Basic | It is a function used with a friend keyword to grant a non-member function access to the private members of a class. | It is a class used with a friend keyword to access the private members of another class. |
Forward declaration | Must be used. | Not mandatory. |
Use | A friend function can be used in some situation of operator overloading. | A friend class can be used when a class is created on the top of another class. |
Definition of Friend Function
The friend function is used to access the private and protected members of a class by permitting the non-member function to gain access. In this type of function, a friend keyword is used before the function name at the time of declaration. There are some restrictive conditions applied to friend function. The first condition is that friend function is not inherited by a child class. The second condition is that storage class specifier may not be present in friend function, which means that it can not be declared as static and extern.
The friend function is not called with an invoking object of the class. The examples of friend function are: a global function, member function of a class, function template can be a friend function. Let’s understand it with the help of an example.
#include <iostream> using namespace std; class first { int data; public: first (int i):data(i){} friend void display (const first& a); }; void display (const first& a) { cout<<"data = "<<a.data; } int main() { first obj(10); display (obj); return 0; } //Output data = 10
Definition of Friend Class
Similar to friend function we can make one class to be a friend of another class which is referred to as friend class. So that the friend class can gain access to private members defined within the other class. It’s important to remember that friend class can only access the names defined within the other class instead of inheriting other class. Precisely, the members of the first class cannot become the members of the friend class. These friend classes are rarely used.
The friend class can be declared in more than a single class. It is considered as a short alternative method for friend function because with the help of this we can create a friend class which can access the whole data members and function instead of creating multiple friend functions.
#include <iostream> using namespace std; class First { // Declare a friend class friend class Second; public: First() : a(0){} void print() { cout << "The result is "<< a << endl; } private: int a; }; class Second { public: void change( First& yclass, int x ) { yclass.a = x; } }; int main() { First obj1; Second obj2; obj1.print(); obj2.change( obj1, 5 ); obj1.print();| } //Output The result is 0 The result is 5
Key Differences Between Friend Function and Friend Class
- Friend function is a function that is able to access the private and protected members of a class. In contrast, a friend class is a class which help in accessing the private members of a class.
- A friend function is declared by including its prototype inside the class, antecede it with the keyword friend. Similarly, a friend class is also defined using keyword friend.
- The forward declaration is used in case of friend function as against, it is not necessary to use it in friend class.
Conclusion
A friend function is required when a function needs to access two or more independent classes, internal members. On the other hand, a friend class is needed when a class requires accessing the members of another class. When a multiple member function needs to be a friend of that function, in that case, it is better to use friend class.
Leave a Reply