Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Rules for Operator Overloading in C++



Operator overloading in C++ is the feature that allows you to define how operators will behave for user-defined data types like classes and structures. Operator overloading and function overloading both support compile-time polymorphism.

In the following article, we will learn the rules that need to be followed for operator overloading.

Rules for Operator Overloading

  1. Only built-in operators can be overloaded. If some operators are not present in C++, we cannot overload them.
  2. The arity of the operators cannot be changed.
  3. The precedence and associativity of the operators remain the same and cannot be changed.
  4. The overloaded operator cannot hold the default parameters except the function call operator "()".
  5. We cannot overload operators for built-in data types. At least one user-defined data type must be there.
  6. The assignment "=", subscript "[]", function call "()", and arrow operator "->" are operators that must be defined as member functions, not friend functions.
  7. Some operators like assignment "=", address "&", and comma "," are by default overloaded.
  8. A few operators that cannot be overloaded at all are scope resolution (::), member access (.), sizeof, typeid, decltype, etc.

Steps to Overload Operators in C++

General basic steps for operator overloading are given with the following rules given above.

  • First, choose the operator you want to overload, and then define the class that will use the overloaded operator.
  • Now, declare the operator function that will overload the operator using the "operator" keyword, followed by defining.
    • Define it as a member function if unary operators and the left operand is the object.
    • Define it as a friend function for binary operators if the left operand is not an object.
  • Then implement the function logic.

Example

Here is the following example code for operator overloading.

#include <iostream>
using namespace std;

class sum {
private:
    int real, imag;

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

    // Overload the '+' operator
    sum operator+(const sum& other) {
        sum result;
        result.real = this->real + other.real;
        result.imag = this->imag + other.imag;
        return result;
    }

    // Function to display the complex number
    void display() {
        cout << real << " + " << imag << "i" << endl;
    }
};

int main() {
    sum c1(3, 4), c2(1, 2);
    sum c3 = c1 + c2;  // using overloaded '+' operator

    cout << "Sum of complex numbers: ";
    c3.display(); 

    return 0;
}

Output

Sum of complex numbers: 4 + 6i

Explanation

In this given program, the operator overloading is used to redefine the behavior of the + operator. Normally, this operator only adds built-in types like integers or floats, but in this class named sum, it allows two sum objects (c1 and c2) to be added directly using c1+ c2. Here, operator+ performs addition by adding the real and imaginary parts of the two objects and returns a new sum object as a result.

Updated on: 2025-05-12T19:38:14+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements