Saturday, September 19, 2015

What are friend functions? Explain with example.

Solution:-
Friend Function:-

       A function is said to be a friend function of a class if it can access the members (including private members) of the class even if it is not the member function of this class.  A friend function is a non-member function that has access to the private members of the class.

Example:-

#include< iostream.h >
#include< conio.h >
class student
{
        int money ;
        public:
        void getmoney( )         {
                  cout<< " Enter the money: ";
                  cin>>money;
        }
        void display( )
        {
                  cout<<"money<<endl;
        }
        friend student share(student ) ;
} ;
student share(student x)
{
        studnt temp ;
        temp . money = x . money;
        return temp ;
}
void main( )
{
        clrscr( );
        student netra , durga ;
        netra.getmoney( );
        netra.display( );
        durga=share(netra) ;
        durga.display( );
        getch( );
}