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);
}