Learn about Operator Overloading in C++ Programming



Similar to functions, operators can also be overloaded. An operator can also have more than one definition. In the previous article, we have seen how the plus + sign (that does the addition operation in arithmetic) has been re-defined by the string class to do the string concatenation job. This is a great feature of C++ and OOP that allows you to break the limits of the ordinary programming languages (including C), and master the language tools. Our topic for today’s article is operator overloading. Wish you nice reading.

The operator Keyword
To overload an operator, C++ offers a special keyword: the operator keyword.


Overloading Binary Operators
Binary operators require two operands. Well-known examples for these are +, -, *, /, and %. These operators are mainly used in arithmetic operations. However, we could redefine an operator to work with user-defined data types as well. Okay, let’s simplify things using an example.

The string class has overloaded the + operator to do string concatenation. What about overloading another operator to do string repetition (multiplication) operation? The operation requires mainly a string operand (the string that will be duplicated), and an integer operand that represents the number of times to duplicate that string. The result should be assigned to a string object. Interesting challenge, huh?!

Okay, let’s say the operand to overload is the asterisk * sign (to be analogous to its original meaning: multiplication).  Now read the following code:

Let’s see how this program will behave when executed:


Now, let’s explain things:

  • Before the main() function, the following line appeared:

This line declares the * operator. It tells the compiler that whenever it sees this operator (and realize that it is the intended version by checking the number of arguments and their data types), the compiler should invoke this function.

  • Inside the main() function:

This statement uses the overloaded * operator to duplicate the S1 string c times, and assigns (returns) the result to string S2.

Again, what makes the compiler decide to use this overloaded version, or the other one (used for arithmetic multiplication) is the data types of the operands, which are a string and an integer.

Overloading the ++ and Operators
In a similar way, we can overload unary operators, like ++ and —

Back to the counter class example (Article 16 – More on OOP)

We need to use the ++ and — operators instead of increment and decrement member functions, respectively.

Now, the main function that will use it:

Executing the counter2.cpp program will give us the following output:



In this article, we have talked about Operator Overloading. Operator Overloading means the same operator could have more than one implementation. The C++ compiler calls the suitable implementation based on the number of arguments (operands), their data types.

In the next article, we are going to start talking about Inheritance; an important and constitutive topic that is bound to be interesting. See you then.


Please enter your comment!
Please enter your name here