the concept of friend function

Concept of friend class
Class which is the friend of other class is said to be friend class
Ex - consider the class a and make class b as the friend for the class a, then class b is said to be the friend of the class a.
Create the object for the first class in the function of the second class i.e as like below and in the main we create the object for the friend class and using which we can call the function in the second class which in tern call the function of the first class

class frndclass
{
    //int a=5,b=7;   //instance variable declaration and intialization
    public: friend class  disp;    //creating the friend class by the key word friend

             void frnd(int a,int b)     //metehod with 2 argument
             {
                 int c=a*b;
                 cout<<a<<endl<<b<<endl;
                 cout<<c<<endl;
             }
};

class disp{                    //body of the friend function begins

public:
         void dip(int a,int b)    //method with 2 argument
         {
             frndclass f;      //creating the object for the first class 
             f.frnd(a,b);     //calling the method of first class 
         }
};

int main()
{
    int a=5,b=7;
    disp d;      //creating the object for the second class
    d.dip(a,b);    //calling the method in the second class
    return 0;
}
Posted on by