Know More About Friend Functions In C++ With Examples.
C++ supports the feature of encapsulation in which the data is bundled together with the functions operating on it to form a single unit. By doing this C++ ensures that data is accessible only by the functions operating on it and not to anyone outside the class.
This is one of the distinguishing features of C++ that preserves the data and prevents it from leaking to the outside world.
=> Read Through The Easy C++ Training Series.
But in some real-time applications, sometimes we might want to access data outside the bundled unit. For example, an outsider class might want to access private and protected data of a C++ class.
C++ provides a facility for accessing private and protected data by means of a special feature called “friend” function or class which we will discuss here in this tutorial.
Table of Contents:
Friend Function In C++
A friend function in C++ is a function that is preceded by the keyword “friend”. When the function is declared as a friend, then it can access the private and protected data members of the class.
A friend function is declared inside the class with a friend keyword preceding as shown below.
class className{ …… friend returnType functionName(arg list); };
As shown above, the friend function is declared inside the class whose private and protected data members are to be accessed. The function can be defined anywhere in the code file and we need not use the keyword friend or the scope resolution, operator.
There are some points to remember while implementing friend functions in our program:
- A friend function can be declared in the private or public section of the class.
- It can be called like a normal function without using the object.
- A friend function is not in the scope of the class, of which it is a friend.
- A friend function is not invoked using the class object as it is not in the scope of the class.
- A friend function cannot access the private and protected data members of the class directly. It needs to make use of a class object and then access the members using the dot operator.
- A friend function can be a global function or a member of another class.
Example Of A Friend Function
Let us implement a programming Example to better understand the usage of Friend Function.
#include <iostream> #include <string> using namespace std; class sample{ int length, breadth; public: sample(int length, int breadth):length(length),breadth(breadth) {} friend void calcArea(sample s); //friend function declaration }; //friend function definition void calcArea(sample s){ cout<<"Area = "<<s.length * s.breadth; } int main() { sample s(10,15); calcArea(s); return 0; }
Output:
Area = 150
In the above program, we have a class sample with private members length and breadth. We have a public constructor that initializes the values of length and breadth. Next, we have a friend function “calcArea” that calculates the area by taking length and breadth into account.
Note that calcArea is a friend function and is a not part of the class. In the main function, after creating an object of the class sample, we pass it to the calcArea function that calculates area and displays the value.
Friend Class
Just like friend functions, we can also have a friend class. Friend class can access private and protected members of the class to which it is a friend.
class A{ …… friend class B; }; class B{ …….. };
As depicted above, class B is a friend of class A. So class B can access the private and protected members of class A.
But this does not mean that class A can access private and protected members of the class B. Note that the friendship is not mutual unless we make it so.
Similarly, the friendship of the class is not inherited. This means that as class B is a friend of class A, it will not be a friend of the subclasses of class A.
Let us take a programming example to demonstrate the friend class.
#include <iostream> #include <string> using namespace std; class Area{ int length,breadth,area; public: Area(int length,int breadth):length(length),breadth(breadth) {} void calcArea(){ area = length * breadth; } friend class printClass; }; class printClass{ public: void printArea(Area a){ cout<<"Area = "<<a.area; } }; int main(){ Area a(10,15); a.calcArea(); printClass p; p.printArea(a); return 0; }
Output:
Area = 150
In this program, we have two classes. The Class “Area” which calculates the area using the length and breadth parameters. Note that the fields, area, length, and breadth are all private members of the class Area.
The next class that is used is the “printClass” that prints the area calculated by a function calcArea in the Area class. As the members are private, we need to make printClass a friend of Area class.
Once that is done, in the main function we create an object of the Area class, calculate the area and pass the object to printArea function of printClass class to display the area.
Advantages/Disadvantages Of Friend Function
One advantage of the friend function or class is that we can access the private and protected data of the class. For Example, if we are implementing a linked list, then we can make the linked list class as a friend of the node class and access its data as the linked list consists of nodes.
Thus in a certain situation using a friend function or class can prove to be advantageous. However, it has some disadvantages as well.
One of the distinguishing features of C++ is encapsulation i.e. bundling of data and functions operating on that data together so that no outside function or class can access the data. But by allowing the friend functions or class to access the private members of another class, we actually compromise the encapsulation feature.
In order to prevent this, we should be careful about using friend functions or class. We should ensure that we should not use too many friend functions and classes in our program which will totally compromise on the encapsulation.
Conclusion
This concludes our tutorial on friend function and classes in C++.
Readers must be very cautious while using friend classes and functions as though it can act as a boon for the programmer, it also needs to be used cautiously as over usage of friend functions and classes can compromise the OOP feature of encapsulation in our program.
In our upcoming tutorial, we will learn about Static Functions in C++ in detail.
=> Visit Here To Learn C++ From Scratch.
thanks for explaining in details please bring more content like this