Binary Operator Overloading in C++ (With Example Programs)

Binary operator overloading

Operator overloading is a compile polymorphic technique where a single operator can perform multiple functionalities. As a result, the operator that is overloaded is capable to provide special meaning to the user-defined data types as well. We can overload binary operators like +,*/, – etc to directly manipulate the object of a class.

BINARY OPERATOR OVERLOADING

Generally asked as –

Construct a program to illustrate binary operator overloading and explain in detail.

Syntax:

return_type::operator binary_operator_symbol(parameters){
    // function definition
}

Here the operator is a keyword and binary operator symbol is the operator to be overloaded.

Binary Operator Overloading in C++

Example 1

Objective: C++ Program to Add and subtract two complex numbers using Binary Operator Overloading

Here we will try to write a program and demonstrate how Binary Operator Overloading works –

In the below program we add/subtract two complex numbers

  • Complex Number 1(obj1): 7 + 5i
  • Complex Number 2(obj2): 3 + 4i

Operations done –

  • Operation 1: Obj1 + Obj2
  • Operation 1: Obj1 – Obj2
Run
// Write a program to demonstrate binary operator overloading
#include <iostream>
using namespace std;
class complex 
{
    int a, b;
    public:

    void get_data(){
        cout << "Enter the value of Complex Numbers a, b: "; cin >> a >> b;
    }
  
    complex operator+(complex ob)// overaloded operator function +
    {
        complex t;
        t.a = a + ob.a;
        t.b = b + ob.b;
        return (t);
    }
   
    complex operator-(complex ob)// overaloded operator function -
    {
        complex t;
        t.a = a - ob.a;
        t.b = b - ob.b;
        return (t);
    }
   
    void display(){
        cout << a << "+" << b << "i" << "\n";
    }
};

int main() 
{
    complex obj1, obj2, result, result1;
    obj1.get_data();
    obj2.get_data();
    
    result = obj1 + obj2;
    result1 = obj1 - obj2;
  
    cout << "\n\nInput Values:\n";
  
    obj1.display();
    obj2.display();

    cout << "\nResult:";
  
    result.display();
    result1.display();
    
    return 0;
}

Output

Enter the value of Complex Numbers a, b: 7 5
Enter the value of Complex Numbers a, b: 3 4

Input Values:
7+5i
3+4i

Result:10+9i
4+1i

Example 2

We will perform all mathematical operations on two objects obj1 and obj2

  • Addition
  • Subtraction
  • Multiplication
  • Division
Run
// C++ Program to perform mathemtical operations on two objects
// using Binary Operator Overloading
#include <iostream>   
using namespace std;

class Math  
{
    int num;  
    public:  
    // setter to set value 
    void setValue(int val){
        num = val;
    }
    // overloading + operator to add values in two objects
    Math operator + (Math &obj){  
        Math temp;  
        temp.num = num + obj.num;  
        return (temp);  
    }
    
    // overloading - operator to subtract values in two objects
    Math operator - (Math &obj){  
        Math temp;  
        temp.num = num - obj.num;  
        return (temp);  
    }
    
    // overloading * operator to multiply values in two objects
    Math operator * (Math &obj){  
        Math temp;  
        temp.num = num * obj.num;  
        return (temp);  
    }
    
    // overloading / operator to divide values in two objects
    Math operator / (Math &obj){  
        Math temp;  
        temp.num = num / obj.num;  
        return (temp);  
    }
    
    // display result value getter
    void getValue(){  
        cout << num;  
    }         
};
int main ()  
{  
    // created objects obj1 and obj2 to perform mathematical operations on
    // created resObj to store results
    Math obj1, obj2, resObj;
    
    // accepting the values  
    obj1.setValue(20);  
    obj2.setValue(10);
    
    cout << "Obj 1: ";
    obj1.getValue();
    cout << "\nObj 2: ";
    obj2.getValue();
    
    
    // assign result of obj1 and obj2 to resObj
    // addition
    resObj = obj1 + obj2;  
    cout << "\n\nObj1 + Obj2 : " ;  
    resObj.getValue();      

    // subtraction
    resObj = obj1 - obj2;
    cout << "\nObj1 - Obj2 : " ;  
    resObj.getValue();      

    // multiplication
    resObj = obj1 * obj2;  
    cout << "\nObj1 * Obj2 : " ;  
    resObj.getValue();
    
    // division  
    resObj = obj1 / obj2;  
    cout << "\nObj1 / Obj2 : " ;  
    resObj.getValue();
    
    return 0;     
}  

Output

Obj 1: 20
Obj 2: 10

Obj1 + Obj2 : 30
Obj1 - Obj2 : 10
Obj1 * Obj2 : 200
Obj1 / Obj2 : 2

Example 3

Adding the dimension of two Cube objects and printing the results for volume for the resultant cube

Cube 3 = Cube1 + Cube2

Run
#include <iostream>
using namespace std;
 
class Cube {
   double length;      // Length of a Cube
   double breadth;     // Breadth of a Cube
   double height;      // Height of a Cube
   
   public:
 
   double getVolume(void) {
      return length * breadth * height;
   }
   
   void setDimensions(double len, double h, double b) {
      length = len;
      breadth = b;
      height = h;
   }
 
   // Overload + operator to add two Cube objects
   Cube operator+(const Cube& c) {
      Cube temp;
      
      temp.length = this->length + c.length;
      temp.breadth = this->breadth + c.breadth;
      temp.height = this->height + c.height;
      return temp;
   }
};

// Main function for the program
int main() {
    Cube cube1, cube2, cube3; // Declare 3 cube objects
 
    // setters for cube
    cube1.setDimensions(3.0, 3.0, 3.0); 
    cube2.setDimensions(5.0, 5.0, 5.0); 
 
    // print values of cube1 and cube2
    cout << "Volume of cube1 : " << cube1.getVolume() << endl;
    cout << "Volume of cube2 : " << cube2.getVolume() << endl;
 
    // Lets add two objects using binary overloading + operator
    cube3 = cube1 + cube2;
 
    // result
    cout << "Volume of cube3 : " << cube3.getVolume() << endl;
 
    return 0;
} 

Output

Volume of cube1 : 27
Volume of cube2 : 125
Volume of cube3 : 512

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription