Friday 31 May 2013

Friend Function in C++


Introduction to friend function

  • Member function can access private members of the class along with other members of the same class.
  • Non member functions can not access private members of the class.
  • Friend functions can access private members of the class along with other members of the class though it is not a member function.
  • Friend function is a special non member function whose declaration part should be in the class body preceded by keyword friend. This makes clear to the compiler that this function is friend to the class in which it is declared.
  • Definition part should be kept outside the class body.
  • Remember, not to put membership label while defining friend function as it is not a member function.

Consider the following example:
#include<iostream.h>
class complex
{
     private:
     int a,b;
     public:
     void getdata();
     void showdata();  
     friend void dost();
};
void complex:: getdata()
{
     cout<<”Enter two numbers”;
     cin>>a>>b;
}
void complex:: showdata()
{
     cout<<”a=”<<a<<”b=”<<b;
}
void complex::dost()
{
     cout<<a<<b;
}
void main()
{
  complex c1;
  c1.getdata();
  c1.dost();
  c1.showdata();
}

l  Don’t trust on the above code. Above program is to make you understand about few of the common mistakes.
l  First of all object cannot call friend function as dost() is not a member function. So dost() should be called independently like any other non member function.
l  Second mistake is during definition of function dost(). Remember membership label should be used for member functions.
l  Third mistake is, friend function can access private members of the class but not independently (without using object and dot operator).
l  For any member function object is required to invoke it and the variables used in member functions are of the object who invokes the member function.
l  Here, dost() is not a member, hence can not be called by any object then whose variable a and b are being used by function dost(). This forces the programmer to pass object during function call so that friend function can access object members.
l  The following example is correction in the previous program:

#include<iostream.h>
class complex
{
     private:
     int a,b;
     public:
     void getdata();
     void showdata();  
     friend void dost(complex);
};
void complex:: getdata()
{
     cout<<”Enter two numbers”;
     cin>>a>>b;
}
void complex:: showdata()
{
     cout<<”a=”<<a<<”b=”<<b;
}
void dost(complex c)
{
     cout<<c.a<<c.b;
}
void main()
{
  complex c1;
  c1.getdata();
  dost(c1);
  c1.showdata();
}


Defining operator as a friend function
#include<iostream.h>
class complex
{
     private:
     int a,b;
     public:
     void getdata();
     void showdata();  
     friend complex operator +(complex, complex);
};
complex operator +(complex d1, complex d2)
{
  complex temp;
  temp.a=d1.a+d2.a;
  temp.b=d1.b+d2.b;
  return(temp);
}
void complex:: getdata()
{
     cout<<”Enter two numbers”;
     cin>>a>>b;
}
void complex:: showdata()
{
     cout<<”a=”<<a<<”b=”<<b;
}
void main()
{
  complex c1,c2,c3;
  c1.getdata();
  c2.getdata();
  c3=c1+c2; 
  c3.showdata();
}
l  Observe main() function, specially the line c3=c1+c2;. Calling convention is same as we did in operator overloading without friend function. But the interpretation is not same.
l  Here, the valid interpretation is, operator ‘+’ is called independently (as it is non member) and c1 and c2 objects are passed.
l  These objects are then received in d1 and d2 (see definition part of ‘+’)
l  Object temp is used to temporary hold the addition result and return back to main() where the value of temp copied into object c3.

Overloading insertion (<<) and extraction (>>) operator

#include<iostream.h>
class complex
{
     private:
     int a,b;
     public:
     void getdata();
     void showdata();  
     friend ostream & operator <<(ostream &, complex);
            friend istream & operator >>( istream &,complex &);
};
ostream & operator <<(ostream &dout, complex c)
{
  dout<<c.a<<” “<<c.b;
  return(dout);
}
istream  & operator >>(istream &din, complex &c)
{
  din>>c.a>>c.b;
  return(din);
}
void complex:: getdata()
{
     cout<<”Enter two numbers”;
     cin>>a>>b;
}
void complex:: showdata()
{
     cout<<”a=”<<a<<”b=”<<b;
}
void main()
{
  complex c1,c2;
  cout<<”Enter real and img part: “;
  cin>>c1;
  cout<<”Enter real and img part: ”;
  cin>>c2;
  cout<<”Values of object c1:”
  cout<<c1;
  cout<<”Values of object c2:”
  cout<<c2; 
}

l  We cannot create object of class istream and ostream but we can make references of these classes (as we do in friend functions).
l  Here we overload two operators inserter (<<) and extractor (>>).
l  Notice the line cin>>c1 in main(), operator  >> is called and references of cin and c1 are passed. This is then received in reference object din and reference variable c.
l  Similarly, notice the line cout<<c1, operator << is called and reference of cout and c1 are passed. This is then received in reference object dout and variable c.
l  Coding inside inserter and extractor are self explanatory.


No comments:

Post a Comment