using a friendusing a friend function is quite


Using a Friend

Using a friend function is quite easy. The following example explains a friend function to access members of two classes.

class Bclass;                                                        // Forward Declaration

class Aclass

 {

  public :

                                Aclass(int v)

                                 {

                                                Avar = v;

                                 }

 

friend int addup(Aclass &ac, Bclass &bc);

  private :

                                int Avar;

 };

class Bclass

 {

  public :

                                Bclass(int v)

                                 {

                                                Bvar = v;

                                 }

 

                                friend int addup(Aclass &ac, Bclass &bc);

  private :

int Bvar;

 

 };

int addup(Aclass &ac, Bclass &bc)

 {

                return( ac.Avar + bc.Bvar);

 }

void main()

 {

  Aclass aobj;

  Bclass bobj;

                  int total;

                                total = addup(aobj,bobj);

                 }

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: using a friendusing a friend function is quite
Reference No:- TGS0309474

Expected delivery within 24 Hours