Write a program to perform Member Function of one class as a friend of another class

/*
  Member Function of one class as a friend of another class
*/
#include<iostream>
using namespace std;

class Demo;

class Sample
{
   int x;

   public:
            void accept()
            {
                cout<<"\n\n Enter any number: ";
                cin>>x;
            }

            void display()
            {
              cout<<"\n Sample: "<<x;
            }

            void getdata(Demo D);
};


class Demo
{
   int y;

   public:
            void accept()
            {
                cout<<"\n\n Enter any number: ";
                cin>>y;
            }

            void display()
            {
              cout<<"\n Sample: "<<y;
            }

            friend void Sample::getdata(Demo D);
};

void Sample::getdata(Demo D)
{
  cout<<"\n Sample: "<<x;
  cout<<"\n Demo:"<<D.y;
}

int main()
{
  Sample s;
  Demo d;

  s.accept();
  d.accept();

  cout<<"\n Normal: ";
  s.display();
  d.display();

  cout<<"\n\n Friend: ";
  s.getdata(d);
  return 0;
}

Previous
Next Post »