Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
// Program to illustrate friend class example

#include<iostream.h>
class second;
class first
{

     private:
          int a;
          int b;
     public:
     int multiply()
     {
          return a*b;
     }
     void assign(second s);
};


class second
{
     private:
     int c;
     public:
     void setvalue(int x)
     {
          c=x;
     }
          friend class first;
};


void first::assign(second s)
{
     a=s.c;
     b=s.c;
}




int main()
{
     first obj1;
     second obj2;
     obj2.setvalue(10);
     obj1.assign(obj2);
     cout<<"\n Result ="<<obj1.multiply();
     return 0;
}



Test data

Result = 100