#include<iostream.h>
#include<conio.h>
class Op
{
public :
int a,b;
int add()
{
return a+b;
}
Op operator +(Op m) // operator overloading format
{
Op temp;
temp.a=a+m.a;
temp.b=b+m.b;
return temp;
}
};
void main()
{
Op op1,op2,op3;
op1.a=2,op1.b=3;
cout<<"Sum is :"<<op1.add(); //5
op2.a=2,op2.b=8;
cout<<"\nSum is :"<<op2.add(); //10
op3=op1+op2; //passing object as parameter
cout<<"\nSum is :"<<op3.add(); //15
getch();
}
No comments:
Post a Comment