Write a program to perform Function Returning Object

/*
 Function Returning Object
*/

#include<iostream>
using namespace std;

class Sample
{
   int a,b;
   public:
        void accept()
  {
  cout<<"\n\n Enter any two number: ";
  cin>>a>>b;
    }
    void display()
    {
    cout<<"\n\n A:"<<a;
    cout<<"\n B:"<<b;
}

         Sample add(Sample s1,Sample s2)
         {
          Sample s3;
         
s3.a=s1.a+s2.a;
          s3.b=s1.b+s2.b;
         
          return s3;
}
};

int main()
{
Sample s1,s2,s3;

s1.accept();
s2.accept();

s3=s3.add(s1,s2);

s3.display();

    return 0;
}

Previous
Next Post »