Operator Overloading in C++

Introduction

  • Operator overloading is a type of polymorphism in which an operator is overloaded to give user defined meaning to it.
  • The main purpose of operator overloading is to perform operation on user defined data type. For eg. The '+' operator can be overloaded to perform addition on various data types.
  • Operator overloading is used by the programmer to make a program clearer.
  • It is an important concept in C++.
  • Syntax:
    Return_type Classname :: Operator OperatorSymbol (Argument_List)
    {
         //Statements;
    }

  • The operator keyword is used for overloading the operators.
There are a few operators which cannot be overloaded are follows,

i. Scope resolution operator (::)
ii. sizeof
iii. member selector (.)
iv. member pointer selector (*)
v. ternary operator (? :)

There are some restrictions considered while implementing the operator overloading,

1. The number of operands cannot be changed. Unary operator remains unary, binary remains binary etc.
2. Only existing operators can be overloaded.
3. The precedence and associativity of an operator cannot be changed.
4. Cannot redefine the meaning of a procedure.

Overloadable Operators

Following is the list of operators which can be overloaded:

+-*/%^
&|~!,=
<><=>=++-
<<>>==!=&&||
+=-=/=%=^=&=
|=*=<<=>>=[ ]( )
→*newnew[]deletedelete[]

Unary Operator Overloading

Unary operator works with one operand and therefore the user defined data types, operand becomes the caller and hence no arguments are required.

Example : Program demonstrating the Unary Increment & Decrement Operator Overloading

#include<iostream>
using namespace std;
//Increment and Decrement overloading

class IncreDecre
{
      private:
           int cnt ;
      public:
           IncreDecre() //Default constructor
           {
                cnt = 0 ;
           }
           IncreDecre(int C)  // Constructor with Argument
           {
                cnt = C ;
           }
           IncreDecre operator ++ ()     // Operator Function Definition for prefix
           {
                return IncreDecre(++cnt);
           }
           IncreDecre operator ++ (int)       // Operator Function Definition with dummy argument for postfix
           {
                return IncreDecre(cnt++);
           }
           IncreDecre operator -- ()       // Operator Function Definition for prefix
           {
                return IncreDecre(--cnt);
           }
           IncreDecre operator -- (int)       // Operator Function Definition with dummy argument for postfix
           {
                return IncreDecre(cnt--);
           }
           void show()
           {
                cout << cnt << endl ;
           }
};
int main()
{
     IncreDecre a, b(5), c, d, e(2), f(5);
     cout<<"Unary Increment Operator : "<<endl;
     cout << "Before using the operator ++()\n";
     cout << "a = ";
     a.show();
     cout << "b = ";
     b.show();
     
     ++a;
     b++;

     cout << "After using the operator ++()\n";
     cout << "a = ";
     a.show();
     cout << "b = ";
     b.show();

     c = ++a;
     d = b++;

     cout << "Result prefix (on a) and postfix (on b)\n";
     cout << "c = ";
     c.show();
     cout << "d = ";
     d.show();
     cout<<"\n Unary Decrement Operator : "<<endl;
     cout << "Before using the operator --()\n";
     cout << "e = ";
     e.show();
     cout << "f = ";
     f.show();

     --e;
     f--;

     cout << "After using the operator --()\n";
     cout << "e = ";
     e.show();
    cout << "f = ";
    f.show();

    c = --e;
    d = f--;

    cout << "Result prefix (on e) and postfix (on f)\n";
    cout << "c = ";
    c.show();
    cout << "d = ";
    d.show();
    return 0;
}


Output:
Unary Increment Operator :
Before using the operator ++()
a = 0
b = 5
After using the operator ++()
a = 1
b = 6
Result prefix (on a) and postfix (on b)
c = 2
d = 6

Unary Decrement Operator :
Before using the operator --()
e = 2
f = 5
After using the operator --()
e = 1
f = 4
Result prefix (on e) and postfix (on f)
c = 0
d = 4

In the above program, int is a dummy argument to redefine the functions for the unary increment (++) and decrement (– –) overloaded operators. Remember one thing int is not an Integer, it is just a dummy argument. It is a signal to compiler to create the postfix notation of the operator. Bjarne Stroustrup has introduced the concept of dummy argument, so it becomes function overloading for the operator overloaded functions.