In C++, a class encapsulates data and functions that operate on that data. Data members are usually made private so that they cannot be accessed from the class’s non-member functions. However, in certain situations, there is a need to access the private data members of a class by a function that is not the class member. It can be achieved by making the non-member function as a: friend function.
A friend function is a non-member function that has been granted access to all the private members of a class. To make a function a friend of a Class to access private members of the class, we declare the class’s function by prefixing its declaration with the keyword friend. Just like we allow our friends to share our private information, a function declared as a friend of a class will be able to access the private data of that class. The declaration of friend function can be placed anywhere in the class specification irrespective of access specifiers. The friend function definition is specified outside the class body and is not treated as part of the class.
The significant difference between a member function of a class and a friend function is that the member function is accessed through the object while a friend function requires objects to be passed by value or reference.
Since the friend function can access the private members of a class, it violates encapsulation rules. Therefore it should be used less frequently. One should have a strong justification for using the friend function in a program.
#include<iostream.h> #include<conio.h> class demo { int x; public: demo(int xx) { x=xx; } friend void display(demo}; //Friend function declaration }; void display(demo dd1) { cout<<dd1.x;} int main() { clrscr(); demo d1(5); display(d1);//friend function call return 0; }
Explanation: In the above program, we have defined a class demo having a private data member x and a public constructor member function used to initialize data member x of object d1. Also, within the class demo, we declare the function display() as a class demo friend whose definition define outside the class. Since friend function display() is the class demo’s non-member function, it defines outside the class body without a scope resolution operator.
It can access the private member of the class demo. The statement,
display(d1);
calls a friend function just like a normal function which then displays the value of data member x of the passed object d1 of a class demo. As display() is a non-member function of the class demo, it cannot invoke through an object d1 of the class demo. Hence, we need to pass the object d1 as an argument to access its private data member within the friend function definition.
The main objectives of using a friend function in a program are as follow.
• It acts as a bridge between two classes. To understand this, consider a situation where we want a function to access the private data members of two or more different classes. To solve this problem, we can define this function as a friend of two or more different classes, and hence in this sense, a friend function acts as a bridge between two classes.
• A friend function can be used to overload operators.
#include<iostream.h> class yyy; //forward declaration of class class xxx { private: int x; public: xxx(int xx) {x=xx;} friend void f1 (xxx,yyy); //Friend function declaration }; class yyy { private: int y; public: yyy(int yy) { y=yy; } friend void f1(xxx,yyy);//Friend Function declaration }; void f1(xxx objx,yyy objy) { cout<<"Difference = "<<objx.x-objy.y; } int main() { xxx ob1(10); yyy ob2(5); f1(ob1,ob2);//Friend Function call return 0; }
Explanation: This program aims to calculate the difference of data members of the objects of two different classes. For this, we have defined two classes, xxx and yyy, having a data member x and y respectively, which initialized through their respective single parameter constructors. To compute the difference of the private data members of objects ob1 and ob2 of two classes xxx and yyy, respectively, we make a function f1(), a friend to both the classes. This function declared as a friend in both the classes as
friend void f1 (xxx,yyy);
This friend declaration has two arguments of class types xxx and yyy. The first declaration of the friend function in class xxx expects that the class yyy is already defined, but it is not. To refer to the name of the class yyy respectively as an argument without actually defining it, C++ provides a way known as forwarding Declaration. The statement class yyy; At the beginning of the program is the forward declaration of class yyy, which allows us to refer to class yyy without defining it.