Friend Function in C++
IntroductionC++ language uses object oriented approach which provides great deal of security by binding data and functions together. There are concepts in OOPS (data hiding and encapsulation ) that ensure that a non-member function should not be able to access an object’s private or protected data.
If you are not the member of a class, then u cannot access its private data. It adds security to the C++ programs, but can also add inconvenience. There are situations when it is required to allow functions outside the class to access and manipulate the private members of the class. This can be achieved by friend functions.
Use and Real-life example
[sws_blue_box box_size="100"] Suppose there are two classes, manager and teacher, and there is a need to use a income_tax() function on these two classes. Here C++ has the feature of friend function where it allows a common function to be made friend with both the classes, thereby allowing the function to access the private data of these classes and more importantly such function need not be a member of any of these classes. [/sws_blue_box] The concept of friend function is similar to a real life situation when you given your room keys to your friend and that friend only has access to the room and other people can access your room only through that friend.
Syntax and Working
A friend function is a non-member function that has been granted an access to a class’s private and protected members.
A non-member function can access the private and protected members of a class if it is declared a friend of that class. That is done by including a declaration of this external function within the class, and preceding it with the keyword friend. For instance to make a function tax() as a friend to class teacher , the class definition must look like as follows:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
class teacher
{
private :
…..................
…..................
public :
….................
…..................
friend void tax(void);
….................
….................
};
When declaring a function as friend function, the keyword friend appears only in the prototype declaration of the function inside the class definition, but the actual function declaration and definition does not contain any friend keyword and these occur outside the class definition.
Characteristics of Friend Functions
There are certain properties that friend functions should have :
Friend Function cab be invoked without the help of any object like a normal function.
The meaning and functionality of friend function is not affected if it is declared either in the public and private part of the class.
It is not in the scope of the class to which it has been declared as friend.
It cannot be called using the object of that class as it is not in the scope of the class.
Friend Functions can also be used in operator loading.
The member names cannot be accessed by the friend function directly, it has to use an object name and dot operator.
The functionality of friend function is not mutual. This means that if A is declared friend to B, this does not give B right to access the private members of class A.
An Example:-
#include<iostream>
using namespace std;
class comp
{ public:
int real;
int img;
public:
comp()
{
real=0;
img=0;
}
comp(int r,int i)
{
real=r;
img=i;
}
comp(const comp &c)
{
real=c.real;
img=c.img;
}
comp operator=(comp c);
friend ostream& operator<<(ostream &,comp &c);
friend istream& operator>>(istream &,comp &c);
comp operator+(comp c);
comp operator,(comp c);
};
comp comp::operator,(comp c)
{ comp temp;
temp.real=c.real;
temp.img=c.img;
return(temp);
}
comp comp::operator+(comp c)
{
comp temp;
temp.real=real+c.real;
temp.img=img+c.img;
return(temp);
}
ostream& operator<<(ostream &dout,comp &c)
{
dout<< c.real<<endl;
dout<< c.img<<endl;
}
comp comp::operator=(comp c)
{
real=c.real;
img=c.img;
return(*this);
}
istream& operator>>(istream &din,comp &c)
{
din>>c.real;
din>>c.img;
}
int main()
{
comp c1,c2(2,3),c3,c4;
cout<<c1;
c3=c1;
cout<<c3<<endl;
cout<<c2;
c3=(c1,c2,c3);
cout<<c3;
return 0;
}
Comments