Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Rust Go Linux CPU Others Videos
Advertisement

Article by Ayman Alheraki on January 11 2026 10:34 AM

Introduction to Operator Overloading in C++

Introduction to Operator Overloading in C++

Operator overloading is a powerful feature in C++ that allows programmers to define new behaviors for traditional operators like +, -, *, and == when used with their custom objects. In object-oriented programming (OOP), this is a fundamental concept that increases the scalability of code and makes it more natural and intuitive when dealing with complex data types.

For instance, if you have an object representing a complex number, by default, you can't use the + operator to add two complex number objects. This is where operator overloading comes in, allowing you to define the behavior of addition for your objects.

Importance of Operator Overloading

Operator overloading is important because it:

  • Improves code readability.

  • Allows for writing logical and easily understood expressions.

  • Makes it easier to handle objects as if they were primitive types.

Operators that can be overloaded

In C++, the following operators can be overloaded:

  • Arithmetic operators: +, -, *, /, %, ++, --.

  • Comparison operators: ==, !=, <, >, <=, >=.

  • Logical operators: &&, ||, !.

  • Input/output operators: <<, >>.

  • Assignment operators: =, +=, -=, *=, /=, etc.

Some operators cannot be overloaded, such as the dot operator (.) and the scope resolution operator (::).

How to define Operator Overloading

Operator overloading is defined using the keyword operator followed by the operator you want to overload. You can define the overload either as a member function of the class or as a friend function if it requires access to private or protected members.


Detailed Example: Overloading the + Operator for Adding Complex Numbers

Step 1: Defining the Complex Class

Explanation:

  • Here, we define a class named Complex that represents a complex number.

  • The class contains two float variables: real and imag to represent the real and imaginary parts of the complex number.

  • The + operator is overloaded to take another Complex object and add the real and imaginary parts.

  • The function returns a new Complex object containing the result.

Step 2: Using the Overloaded Operator

Explanation:

  • In the main function, we create two Complex objects c1 and c2 and then add them using the overloaded + operator.

  • The operator + function is called, and a new Complex object representing the result is returned. The result is then displayed using the display function.

1. Arithmetic Operators

The + Operator (Addition)

The + operator is used to add two objects. The following example shows how to overload the + operator to add two Complex objects:

The - Operator (Subtraction)

The - operator works similarly to addition, subtracting the real and imaginary parts of two objects:

The * Operator (Multiplication)

Here we multiply two complex numbers using the following rule:

The / Operator (Division)

To divide two complex numbers, we use the following mathematical formula:

The % Operator (Modulus)

The modulus operator % usually cannot be overloaded for complex types but can be overloaded for custom classes dealing with integers only. For example:


2. Unary Operators

The ++ Operator (Increment)

The ++ operator can be overloaded in two ways: prefix and postfix increment.

Prefix Increment

Postfix Increment It is defined using a dummy argument to indicate the postfix form:

The -- Operator (Decrement)

It behaves the same way as the ++ operator but decreases the value:


3. Comparison Operators

The == Operator (Equality)

This operator is used to check whether two objects are equal:

The != Operator (Inequality)

This can be overloaded using the direct opposite of the equality operator:

The < Operator (Less Than)

It can be overloaded to compare based on a specific criterion. For complex numbers, we may compare their magnitudes:

The <=, >, and >= Operators

Other comparison operators can be defined based on appropriate arithmetic operations:


4. Logical Operators

The && Operator (AND)

We can overload it to check logical conditions for two objects:

The || Operator (OR)

The ! Operator (NOT)


5. Input/Output Operators

The << Operator (Output)

This operator is overloaded to allow cout to print objects of the Complex type:

The >> Operator (Input)

This is overloaded to allow direct input of values into objects using cin:


6. Assignment Operator

The = Operator (Assignment)

This allows copying values from one object to another:


7. Subscript Operator

The [] Operator

This can be overloaded to access values within the object based on an index:


8. Pointer Operators

The * Operator (Dereference)

It can be overloaded to convert an object into a primitive value. For example, if we want to access the value directly:

The -> Operator

It is used to access members of an object via a pointer:

The feature of operator overloading in C++ provides great flexibility, allowing you to work with objects naturally as if they were primitive types. This helps make the code clearer, easier to understand, and more maintainable.

 

Advertisements

Responsive Counter
General Counter
1274887
Daily Counter
127