Binary Operator Overloading in C++:
Binary operator takes two operands to operate
Program of Binary Operator (+) Overloading
//Binary operator overloading
#include<iostream>
using namespace std;
class Demo
{
int x;
int y;
public:
Demo()//default constructor
{
x=0;
y=0;
}
void setData(int n1,int n2)
{
x=n1;
y=n2;
}
int showData()
{
cout<<endl<<"x="<<x<<"y="<<y;
}
Demo operator+(Demo &d)//this is operator function
{
Demo temp;
temp.x=x+d.x;
temp.y=y+d.y;
return temp;
}//end of function
};//end of class
int main()
{
Demo d1,d2,d3;//creating objects of Demo
d1.setData(10,20);//calling function to set value
d1.showData();
d2.setData(100,200);//setting values to d2
d2.showData();
d3=d1+d2;//calling operator+() function
d3.showData();
}//end of main
Output:
x=10y=20x=100y=200
x=110y=220