C++ Operator Overloading

Operator overloading is a feature in C++ that allows operators like +, -, *, /, etc. to be redefined to work with user-defined data types. By overloading an operator, it can be given a new meaning and functionality specific to the data type it is being used with. In this tutorial, we will discuss the basics of operator overloading and how to overload different types of operators in C++.

Syntax:

return_type operator symbol (parameters) {
    // code to overload the operator
    // return statement
}


C++ Program For Overloading of + Operator

#include 

using namespace std;

class Complex {
    public:
        int real, imag;

        Complex operator + (Complex const &obj) {
            Complex res;
            res.real = real + obj.real;
            res.imag = imag + obj.imag;
            return res;
        }
};

int main() {
    Complex c1, c2, c3;
    c1.real = 1;
    c1.imag = 2;
    c2.real = 3;
    c2.imag = 4;
    c3 = c1 + c2;
    cout << c3.real << " + i" << c3.imag << endl;
    return 0;
}
Output
4 + i6

Types of Operators that can be Overloaded
  • Unary operators
  • Binary operators
  • Ternary operator (?:)
  • Comparison operators
  • Assignment operators
  • Subscript operator ([])
  • Function call operator (())
  • Class member access operator (->)
  • Increment and Decrement operators

Unary Operators Overloading

Unary Operators: Unary operators work on only one operand. Some of the commonly overloaded unary operators are the unary minus (-), unary plus (+), increment (++) and decrement (--) operators.

C++ Program For Unary Operator Overloading

#include <iostream>

using namespace std;

class Distance {
    public:
        int meter;

        Distance operator - () {
            Distance d;
            d.meter = -meter;
            return d;
        }
};

int main() {
    Distance d1, d2;
    d1.meter = 10;
    d2 = -d1;
    cout << d2.meter << endl;
    return 0;
}
Output
-10

Binary Operators Overloading

Binary operators work on two operands. Some of the commonly overloaded binary operators are the addition (+), subtraction (-), multiplication (*), and division (/) operators.

C++ Program For Binary Operator Overloading

#include <iostream>

using namespace std;

class Distance {
    public:
        int meter;

        Distance operator + (Distance const &d) {
            Distance res;
            res.meter = meter + d.meter;
            return res;
        }
};

int main() {
    Distance d1, d2, d3;
    d1.meter = 10;
    d2.meter = 20;
    d3 = d1 + d2;
    cout << d3.meter << endl;
    return 0;
}
Output
30

Comparison Operators Overloading

Comparison operators are used to compare two values. Some of the commonly overloaded comparison operators are the less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=) operators.

C++ Program For Comparison Operator Overloading

#include <iostream>

using namespace std;

class Distance {
    public:
        int meter;

        bool operator < (Distance const &d) {
            return meter < d.meter;
        }
};

int main() {
    Distance d1, d2;
    d1.meter = 10;
    d2.meter = 20;
    if (d1 < d2) {
        cout << "d1 is less than d2" << endl;
    } else {
        cout << "d1 is not less than d2" << endl;
    }
    return 0;
}
Output
d1 is less than d2

Restrictions on Operator Overloading
  • Overloading of certain operators such as the conditional operator (?:), scope resolution operator (::), and sizeof operator cannot be done.
  • The overloaded operators must have at least one user-defined data type in their parameters.
  • The overloaded operators must have the same precedence and associativity as the original operator.