# Addition of two number using ‘-‘ operator?

Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user-defined meaning to it. The overloaded operator is used to perform the operation on the user-defined data type. For example, '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation), etc.

Input

10 20
20 30

Output

30
50

### Explanation

To perform addition of two numbers using ‘-‘ operator by Operator overloading. Binary operators will require one object as an argument so they can perform the operation. If we are using Friend functions here then it will need two arguments.

The operator is being invoked: ob1-ob2. The object before the operator will invoke the function and the object after the operator will be passed as an argument to the function. So, in this case, ob1 is invoking object and ob2 is passed as an argument to the function.

We are passing 10, 20 as the values of ob1’s x and y and 20, 30 as the values of ob2’s x and y.

## Example

#include <iostream>
using namespace std;
class sum {
public:
int x, y, z;
void getdata(int a, int b) {
x=a;
y=b;
}
void display() {
cout<<"\nSum of X:"<<x;
cout<<"\nSum of Y:"<<y;
}
void operator-(sum &);
};
void sum::operator-(sum &ob) {
x=x+ob.x;
y=y+ob.y;
display();
}
int main() {
sum ob1, ob2;
ob1.getdata(10,20);
ob2.getdata(20,30);
ob1-ob2;
}
Published on 16-Aug-2019 11:52:50