Friend Fun
Friend Fun
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{
……
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.
1 #include <iostream>
#include <string>
2
using namespace std;
3 class sample{
4 int length, breadth;
5
6 public:
7 sample(int length, int breadth):length(length),breadth(breadth)
{}
8 friend void calcArea(sample s); //friend function declaration
9
10 };
11
12
13 //friend function definition
void calcArea(sample s){
14 cout<<"Area = "<<s.length * s.breadth;
15 }
16 int main()
17 {
sample s(10,15);
18 calcArea(s);
19
20 return 0;
21 }
22
23
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.
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.
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.