Table of contents
1.
Introduction
2.
Syntax of the Binary Operator Overloading in C++
3.
Binary Operator Overloading Algorithm in C++
4.
Examples of Binary Operator Overloading in C++
4.1.
Example 1: Overloading the Addition Operator for a Point Class
4.2.
Example 2: Overloading the Multiplication Operator for a Matrix Class
5.
Frequently Asked Questions
5.1.
Can all operators in C++ be overloaded?
5.2.
What are some common mistakes when overloading operators?
5.3.
Is it possible to overload operators for primitive types like int or char?
6.
Conclusion
Last Updated: Apr 27, 2024
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++, operator overloading is a powerful feature that allows you to redefine the behavior of operators for user-defined types. Binary operator overloading specifically focuses on overloading operators that work with two operands, such as addition (+), subtraction (-), multiplication (*), and division (/). By overloading these operators, you can make your code more intuitive & readable, especially when working with custom objects. 

Binary Operator Overloading in C++

In this article, we'll learn about the syntax of binary operator overloading in C++, looking into the algorithm behind it, with few examples to understand it properly.

Syntax of the Binary Operator Overloading in C++

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.

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.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass