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
  Sum
*/

#include<iostream>
using namespace std;

class Demo;

class Sample
{
    int a;

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

            void sum(Demo D);
};


class Demo
{
    int b;

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

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

void Sample::sum(Demo D)
{
    int c;
    c=a+D.b;

    cout<<"\n\n Addition: "<<c;
}

int main()
{
  Sample s;
  Demo d;

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

  s.sum(d);

  return 0;
}

Previous
Next Post »