Addition of two number using ‘-‘ operator?

C++Server Side ProgrammingProgramming

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.


10 20
20 30




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.


#include <iostream>
using namespace std;
class sum {
      int x, y, z;
   void getdata(int a, int b) {
   void display() {
Sum of X:"<<x;       cout<<"
Sum 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; }
Updated on 16-Aug-2019 12:05:43