Friend function:-
- The concept of friend function goes against the concept of date hiding.
- The friend function is declared within the class and it is defined outside of the class.
- The friend function is not a member function of the class.
- Since friend function is not the member function of the class. It is not called for any object, rather objects are passed as parameters to it.
- Even if friend function is not a member function of the class. It can access private member of the class.
- The concept of friend function is useful when a function act as friend function of two more classes.
Program:
Consider the following example.
#include
class ABC
{
int data ;
Public:
void xyz (int) ;
void dff (int) ;
void display ( ) ;
friend void pqr ( abc &);
};
void abc :: xyz (int d)
{
data=d ;
}
void abc :: def (int d)
{
data=d ;
}
void abc :: display ( )
{
cout<
}
void pqr (abc & t)
{
t.data=5;
}
void main ( )
{
abc a ;
a.xyz (3) ;
a.display ( ) ;
a.def (4)
a.display ( ) ;
pqr (a) ;
a.display ( );
}
Explanation:-
→ In the above program the class abc defines one private data member data and it defines three public member function xyz, def and display.
→ Consider the following declarations. Friend void pqr (abc &); this declarations indicate that pqr is a function that accepts alias of object of class abc and it returns nothing.
→ The word friend indicates that it is not a member function of class ABC. The class ABC declares it as its friend function.
→ The member fn xyz, def, display are called for object a.
→ The friend fn pqr is called and object a is passed a parameter.
→ The t acts as alias of object a and the value of t data i.e a data will become 5.
→ Even if pqr is not a member function of class ABC still it can acess private datameter data of class abc. This because class abc has declared fn pqr as its friend fn
→ Suppose we write a statement a.data=6 in The main fn then error will occur this is because main is number member fn of the class ABC nor a friend of class ABC.
Program :-
Develop a class period represented in hours and minutes. The class should have following member function.
1)Setperiod ( ) 2) getperiod ( ) 3) display ( ) Define a fn add as a friend fn of class period .It will add two periods and should return answers of the addition.
Write suitable main fn
# include
class period
{
int hour, min ;
public:
void setperiod (int, int) ;
void getperiod ( ) ;
void display ( ) ;
friend period add ( period, period)
};
void period :: setperiod (int h, m)
{
hour=h ;
min=m ;
}
void period :: getperiod ( )
{
cin>>hour>>min ;
}
void period :: display ( )
{
cout<<"hour<<"hour;"<
}
period add (period x, period y)
{
period temp ;
temp.hour = x.hour + y.hour ;
temp.min = y.hour + y.hour ;
temp.min = x.min + y.min ;
if (temp.min>=60)
{
temp.min−=60 ;
temp.hour ++ ;
{
return temp ;
}
void main ( )
{
period P1,P2,P3;
P1.setperiod (2,26) ;
cout<<"Enter period in hrs and min=" ;
P2.get period ( );
P3=add (p1+p2) ;
cout<<"1st period \n" ;
p1. display ( ) ;
cout<<"2nd period\n' ;
P2.display ( ) ;
cout<<"Addition\n" ‘
P3.display ( ) ;
}
Explanation:-
In the above program add is declared as friend function by class period
Consider the statement
P3=add (P1,P2);
It indicates that friend function add is called and objects P1 and P2 are passed as parameters. Returned answer will be assigned to P3.
Even if add is not a member function of class period. It can still access private datamember of class period i.e hour and min. This is possible only because fn add acts as friend fn of class period.
Friend class:
One class can act as friend class of other class. All the member fn of the friend class can access the private members of other class.
Consider the following example:
# include
class def ;
class abc ;
{
public:
void fun 1 (def & ) ;
void fun 2 (def &) ;
void fun 3 (def &) ;
};
class def
{
int data ;
public:
void set data (int) ;
void display ( ) ;
friend class abc ;
};
void abc :: fun 1 (def & d)
{
d.data + = 5 ;
}
void abc :: fun 2 (def &d)
{
d.data *=5 ;
}
void abc :: fun 3 (def & d)
{
d.data - =5 ;
}
void def :: set data (int x)
{
data=x ;
}
void def :: display ( )
{
cout<
}
void main ( )
{
abc a1 ;
def d1 ;
d1.setdata (3) ;
d1. display ( ) ;
a1.fun 1 (d1) ;
d1. display ( ) ;
a1.fun 2 (d1) ;
d1.display ( ) ;
a1.fun 3 ( d1) ;
d1.display ( ) ;
}
Explanation:
→ In the above program fun 1, fun 2, fun 3 are all member functions of class abc. setdata and display are member functions of class def.
→ Consider the declaration within the class def friend class abc;
with this declaration class def declares that class abc its friend. It means that all the member fn of the class abc can access the private datameter data of class def.
→ a.fun 1(d1) ;
The member funtn fun 1 of class abc is called for object a1 of class abc. and object d1 of class df is passed as parameter the corresponding formal alias object will be a. The value of d.data i.e d1.data will be incremented by five. Similar explanation is applicable for calls of f2 and f3. The fun 1, fun 2, fun 3 are all member fn of class abc and they can still access private datamember data of the other class def. this is possible because that class def declares class abc as its friend.