top of page

Operator Overloading - I

What is operator overloading?


When you have two objects of a class- num1 and num2, you can write a function to add them such as 
 

          ans = add(num1,num2);


That does not look very simple nor intuitive.  You would prefer to write 
 

    ans = num1+num2;


as you would write for integers, floats etc. - for PODs.

 

Operator overloading lets you write such statements for objects too.

 

Overloaded operators are member functions of a class, with keyword operator  followed by operator symbol in place of function name. These functions get called whenever the corresponding operator is used with an object of that class.

Names of overloaded operator functions start with keyword operator followed by  symbol of the operator e.g. +, - etc.

 
Parameters to operator overload functions


Unary operator functions take zero parameters for members. Binary member operator functions take one parameter.

 

What is the reason? Where is the operand for unary operator and second operand for binary operator?

 

The reason is,  the object which calls these functions will be the first parameter.

Let us look at a simple Integer class with overloaded +, += operators.

For the class Integer, we have overloaded + operator and += operator.

 

Look at the function in line 11. The name of the function is operator +. And the function takes one argument of the type Integer. The function adds the num  of invoking object ( this->num) and num of parameter and returns Integer object with the sum.


You can call this overloaded operator function as shown below.

          int main()

          {

              Integer obj1(10),obj2(5),obj3;

              obj3 = obj1+obj2; /*obj3.num will be 15*/

          }

When we call + operator in line 2 of main() function, we call operator function in line 11, parameter a is obj2. And line 13 adds 10 (num of calling object - obj1) and 5 of obj2 and creates an Integer object with the sum 15.

The other overloaded operator in the program above  is += which takes one operand and modifies the current object. You can call this function as shown below.
 

                     obj2+=obj1; /*obj2.num = obj1.num+obj2.num. obj1 is not modified*/

 

Some points must be remembered while overloading the operators

 

  1. You can overload operators as member functions or non-member friend functions.

  2. There are some operators which can not be overloaded at all  e.g. ::(scope resolution), .(member of) , ?:(ternary),sizeof,.* .

  3. When overloading operators, unary operators must remain unary and binary operators must remain binary.

    • Binary operator function, if it is member function, takes one parameter.

    • Binary operator non-member function takes 2 parameters.

    • Unary operator member takes no parameters and unary non-member operator function takes one parameter.

  4. You can NOT create a new operators of your own.

  5. You can not change the precedence, grouping, or number of operands of operators.

 

Let us repeat this fact again.

 

Do you now wonder now , + operator is binary and  function earlier for Integer class took only one operand instead of 2?  Do you think there is some hidden operand somewhere?

Yes, and the hidden operand is the current object (*this) and will be the first operand.

     obj3 = obj1+obj2;

In the line above, obj1 is treated as *this, obj2 is the parameter because the above code is equivalent to


    obj3 = obj1.(operator+(obj2));

Similarly if the operator is unary, member operator function does not take any parameters, as the only operand will be the current object.

The situation is different when you use a non-member function. For non-member friend functions, unary operator takes exactly one parameter and binary operator takes two parameters.

 

For the sake of completeness, let us rewrite our + operator as friend, non-member function.

Notice that in line 11, we do not say Integer::operator+. Instead we say operator +. Because the function is not a member of class, it is just a friend.

As the operator function is friend, it can access even  private data and functions of Integer class.

A non-member operator function need not  be a friend but still access the members using getters and setters.

bottom of page