Operator Overloading in C++ - Part I

You do not like to call a function like
ans = add(num1,num2); Instead you would prefer ans = num1+num2;
That is the whole idea of operator overloading.

Operator overloading is writing functions in such a way that, they can be called like operators e.g. + for addition, [] operator for element of an array etc. You are just writing a user friendly class.

Operator overloading is done through functions. These functions have a name operator followed by the symbol of the operator e.g. ++. And these functions take zero, one or two parameters depending on the type of operator.

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


class Integer
{
    int num;
 public:
    Integer(int n=0);
    int getNum();
    void setNum(int n);
    Integer operator +(Integer  a);
    void operator +=(Integer a);
 };

Integer::Integer(int n):num(n){}

int Integer::getNum()
{
   return num;
}

void Integer::setNum(int n)
{
   num = n;
}

Integer Integer::operator +(Integer a)//line 23
{
   // Integer temp ;
  //  temp.num = this->num + a.num;
   // return temp;
    return Integer(num+a.num);
}

void Integer::operator +=(Integer a)
{
    num+=a.num;
}
  
Finally. (Was able to type the code while retaining white spaces. Used code and pre html tags.).

Look at the function in line 23. The name of the function is operator +. And the function takes one argument of the type Integer. The function adds the num of current object (num or this->num) and num of parameter and returns the resultant object.

In order to use the function, you need to just use +   only.

   int main()
   {
      Integer obj1(10),obj2(5),obj3;
      obj3 = obj1+obj2; //This calls the + operator function. obj3.num will be 15

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


     obj2+=obj1; //obj2.num will be 15. obj1 is unmodified

Some points must be remembered while overloading the operators.
  1. You can also overload operators as friend non-member functions. (not advisable in most situations)
  2. There are some operators which can not be overloaded at all e.g. ::(scope resolution), .(member) , ?:(ternary) etc.
  3. When overloading operators, you should maintain the number of operands as in the original operators.  Unary operators must remain unary and binary operators must remain binary.
  4. You can NOT create a new operator of your own.
Do you now wonder now , + operator is binary and my function above takes only one operand?  Do you think there is some hidden operand somewhere? Yes, the hidden operand is the current object and will be first operand. Similarly if the operator is unary, you do not pass any parameter to the function, as the only operand will be the current object. (The situation is different if you use a non-member function. For non-member friend function, unary operators take exactly one parameter and binary operators take two parameters. )

For the sake of completeness, let us rewrite our + operator as friend, non-member function. 
class Integer
{
    int num;
 public:
    Integer(int n=0);
    int getNum();
    void setNum(int n);
    friend Integer operator +(Integer  a,Integer b);//our friend + operator
    void operator +=(Integer a);
 };
 Integer operator +(Integer a, Integer b)//line 13
{   
    return Integer(a.num+b.num);
}


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

If you do not remember, this  function must be a friend, in order to access the private data and functions of your class.

In the next post, we will consider some special cases of operator overloading like a++ vs ++a, arr[i], () or functor and  conversion operators. 

Comments

Popular Posts