Table of contents
1.
Introduction
2.
What is Binary Operator Overloading in C++?
3.
Syntax of the Binary Operator Overloading
4.
Binary Operator Overloading Algorithm in C++
5.
Examples of Binary Operator Overloading in C++
5.1.
Example 1: Overloading the Addition Operator for a Point Class
5.2.
Example 2: Overloading the Multiplication Operator for a Matrix Class
6.
Steps to Overload a Binary Operator for Summing Two Complex Numbers
6.1.
Step 1: Define a Class for Complex Numbers
6.2.
Step 2: Declare an Overloaded + Operator Function
6.2.1.
Using a Member Function
6.2.2.
Using a Friend Function
6.3.
Step 3: Implement the Overloaded + Operator Function
6.3.1.
Implementation Using a Member Function
6.3.2.
Implementation Using a Friend Function
6.4.
Step 4: Display the Complex Number
6.5.
Step 5: Test the Overloaded Operator in main()
7.
Frequently Asked Questions
7.1.
Can all operators in C++ be overloaded?
7.2.
What are some common mistakes when overloading operators?
7.3.
Is it possible to overload operators for primitive types like int or char?
8.
Conclusion
Last Updated: Feb 25, 2025
Medium

Binary Operator Overloading in C++

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C++ programming, operator overloading stands out as a powerful feature that allows you to redefine the behavior of operators like +, -, *, and / for user-defined types. Among these, binary operator overloading plays a crucial role in customizing how objects interact with these operators, offering flexibility and enhancing code readability. In this blog, we deep dive into the concept of binary operator overloading in C++.

Binary Operator Overloading in C++

What is Binary Operator Overloading in C++?

Binary operator overloading in C++ is a feature that allows programmers to redefine the behavior of binary operators (such as +, -, *, /, ==, !=, etc.) for user-defined data types (like classes and structures). A binary operator requires two operands, and overloading enables these operators to work with objects in a meaningful way.

For example, if you have a class representing complex numbers, you can overload the + operator to allow direct addition of two complex number objects instead of manually adding their real and imaginary parts.

Syntax of the Binary Operator Overloading

Binary operator overloading in C++ involves writing a function that redefines a binary operator for user-defined types. This process allows objects of a class to interact using traditional operators like +, -, *, etc. To overload a binary operator, you must specify the operator keyword followed by the operator you intend to overload.

Here's the general form to overload an operator:

ReturnType operatorX(const ClassName &obj1, const ClassName &obj2) {
    // Code to handle the operator X between obj1 and obj2
}


In the context of C++, the operatorX function is a method inside a class. It must be declared in the class where it is to be used. Let’s consider overloading the + operator for a class called Complex that represents complex numbers:

class Complex {
public:
    float real;
    float imag;

 Complex(float r = 0.0, float i = 0.0) : real(r), imag(i) {}
    // Overload the + operator
    Complex operator+(const Complex& other) {
        return Complex(real + other.real, imag + other.imag);
    }
};


In this example, the + operator is overloaded to add two Complex objects. When two Complex objects are added, the real parts are added together, & the imaginary parts are added together, resulting in a new Complex object.

This approach allows you to perform operations directly on objects just as you would with primitive data types, which makes your code easier to understand & maintain.

Binary Operator Overloading Algorithm in C++

To effectively overload a binary operator in C++, you can follow a straightforward algorithm. This process ensures that your operator works as expected & integrates seamlessly with the types of operations typical in C++ programming. Here’s a step-by-step breakdown:

  • Identify the Operator & Class: Determine which binary operator you need to overload & in which class it should be implemented. The operator should be relevant to the operations of the class.
     
  • Declare the Operator Overloading Function: Inside your class, declare the operator overloading function. The function should be marked with the keyword operator followed by the operator symbol (e.g., +, -, *).
     
  • Define Function Parameters: The function usually takes one parameter representing the right operand of the operator, while the left operand is the object (*this) on which the method is called.
     
  • Implement the Function: Write the logic inside the function to specify how the operator should combine the two operands. This typically involves accessing member variables of the operands & returning a new object of the same class.
     
  • Return the Right Type: Ensure that the return type of your function matches the expected type of the operation’s result. Often, you’ll return a new object by value.
     
  • Test the Overloaded Operator: After implementation, test the overloaded operator to ensure it behaves as intended with various object states.
     

Here is an example of overloading the * operator for a class that represents a mathematical vector to perform vector multiplication:

class Vector {
public:
    int x, y;

    Vector(int x, int y) : x(x), y(y) {}

    // Overload the * operator to perform vector multiplication
    Vector operator*(const Vector& other) {
        return Vector(x * other.x, y * other.y);
    }
};


In this code:

  • The Vector class has two integer properties, x & y.
     
  • The * operator is overloaded to perform element-wise multiplication of two vectors.
     
  • The result is a new Vector object with each component of the vector multiplied accordingly.

Examples of Binary Operator Overloading in C++

Example 1: Overloading the Addition Operator for a Point Class

Imagine a class Point that represents a point in a 2D space with x & y coordinates. We will overload the + operator to add two Point objects.

class Point {
public:
    int x, y;
    // Constructor to initialize x & y
    Point(int x = 0, int y = 0) : x(x), y(y) {}

    // Overload the + operator to add two Point objects
    Point operator+(const Point& other) {
        return Point(x + other.x, y + other.y);
    }
};


In this example:

  • The + operator is overloaded to add corresponding coordinates of two points.
     
  • When you add two Point objects, the result is a new Point object with its coordinates being the sum of the corresponding coordinates of the operands.

Example 2: Overloading the Multiplication Operator for a Matrix Class

Next, consider a Matrix class that represents a mathematical matrix. We'll overload the * operator to multiply two matrices.

class Matrix {
public:
    vector<vector<int>> data;
    int rows, cols;
    // Constructor to initialize the matrix size & elements
    Matrix(int rows, int cols, vector<vector<int>> values) : rows(rows), cols(cols), data(values) {}
    // Overload the * operator to multiply two matrices
    Matrix operator*(const Matrix& other) {
        vector<vector<int>> result(rows, vector<int>(other.cols, 0));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < other.cols; j++) {
                for (int k = 0; k < cols; k++) {
                    result[i][j] += data[i][k] * other.data[k][j];
                }
            }
        }
        return Matrix(rows, other.cols, result);
    }
};


In this example:

  • The * operator is overloaded to perform matrix multiplication.
     
  • The resulting matrix dimensions are determined by the row number of the first matrix & the column number of the second.
     
  • Each element in the resulting matrix is computed by multiplying elements of the rows & columns of the input matrices & summing them up.

Steps to Overload a Binary Operator for Summing Two Complex Numbers

Overloading a binary operator (such as +) for adding two complex numbers in C++ involves a structured approach. Below are the step-by-step instructions to achieve this:

Step 1: Define a Class for Complex Numbers

Create a class to represent a complex number with real and imaginary parts.

class Complex {
    int real, imag; // Private data members

public:
    Complex(int r = 0, int i = 0) : real(r), imag(i) {}  // Constructor
};

Step 2: Declare an Overloaded + Operator Function

To overload the + operator, declare a function inside the class. It can be:

  1. A member function, where the left operand is the calling object (this).
  2. A friend function, where both operands are passed explicitly.

Using a Member Function

Complex operator+(const Complex& obj);

Using a Friend Function

friend Complex operator+(const Complex& obj1, const Complex& obj2);

Step 3: Implement the Overloaded + Operator Function

Define how the + operator should add two complex numbers.

Implementation Using a Member Function

Complex Complex::operator+(const Complex& obj) {
    return Complex(real + obj.real, imag + obj.imag);
}

Implementation Using a Friend Function

Complex operator+(const Complex& obj1, const Complex& obj2) {
    return Complex(obj1.real + obj2.real, obj1.imag + obj2.imag);
}

Both implementations add the real and imaginary parts separately and return the result as a new Complex object.

Step 4: Display the Complex Number

Create a function to print the real and imaginary parts.

void display() {
    cout << real << " + " << imag << "i" << endl;
}

Step 5: Test the Overloaded Operator in main()

Create Complex objects and use the + operator.

int main() {
    Complex c1(3, 4), c2(2, 5);
    Complex sum = c1 + c2;  // Calls overloaded + operator
    sum.display();           // Output: 5 + 9i
    return 0;
}

Frequently Asked Questions

Can all operators in C++ be overloaded?

Not all operators can be overloaded. Operators like :: (scope resolution), .* (pointer-to-member), . (member access), ?: (ternary), and sizeof are not overloadable because their behavior is fundamental to the programming model of C++.

What are some common mistakes when overloading operators?

Common mistakes include not returning the correct type, causing unintended side effects, not handling self-assignment in operators like =, and overloading operators where it doesn't logically make sense, which can lead to confusing code.

Is it possible to overload operators for primitive types like int or char?

No, you cannot overload operators for primitive types directly. Operator overloading only works with user-defined types (i.e., classes and structs). However, you can manipulate these types within user-defined types by overloading operators.

Conclusion

In this article, we have learned the fundamentals of binary operator overloading in C++. We explored the syntax for declaring & defining overloaded operators, discussed the algorithm to correctly implement them, with proper examples. By learning operator overloading, you can enhance the readability and efficiency of your C++ code, making it more intuitive to work with complex data types. This feature is a powerful tool that, when used correctly, can significantly improve the functionality of your custom types.

Live masterclass