Operator Overloading:


Overloading is a concept we've encountered frequently. Let's delve into an example of overloading. In all our examples, we've used the Stream Insertion Operator <<with cout to display various types of data on the screen. Sometimes we print integers, characters, doubles, floats, and strings as illustrated below:

cout << "string";
cout << int;
cout << char;
cout << float;
cout << double;

We observe that although we use only one operator, <<, it serves multiple data types. This indicates that the operator is overloaded for various data types. The iostreamheader file contains prototypes for the functions that enable overloading this operator.

Without operator overloading in C++, we would require separate insertion operators for each data type. However, thanks to operator overloading, a single operator can work with multiple data types. Let's consider another example involving the +operator. We use this operator to add integers, floats, or doubles. But what if we need to add two objects? Attempting to add two objects using the +operator results in an error because computers lack the intelligence to perform such operations. Therefore, we need to overload the +operator to enable addition of objects.

Operator overloading involves implementing a function that can be a member function of a class, a non-member function of a class, or a friend function of a class. In this tutorial, we'll implement the function as a member function of a class. To overload an operator, we define a special operator function inside the class as follows:

class ClassName {
   public:
      return operator OperatorSymbol (ArgumentList) {
         // Function body
      }   
      ...
};

In the syntax above, we use the keyword operator after the return type. Following operator, we specify the operator symbol +, -, *, /, etc. that we want to overload. Arguments can be passed to the operator function in a manner similar to regular functions, and we define this function as a public member of the class. Let's illustrate with an example:

OPERATOR OVERLOADING - C++ Copy to Clipboard  
#include <iostream>
using namespace std;

class Overload {
   private:
      int counter;
   public:
      Overload() {
         counter = 5;
      } 
      void operator ++() {
          counter = counter + 1;
      }
      void Display() {
          cout << "Counter is: " << counter << endl;
      }
};

int main() {
   Overload load;
   cout << "Before Overload Operator: " << endl;
   load.Display();
   ++load;
   cout << "After Overload Operator: " << endl;     
   load.Display();
   return 0;
}

In the above example, we overload the increment operator ++to increase the value of our object. When we attempt to increment the object, the overloaded operator is called instead of the default operator since the default increment operator cannot increment objects.

Pro-Tip:

Try removing the overloaded operator function from the class, compile the program, and observe if you can increment the object value using the default increment operator.

Operators That Can Be Overloaded:

Here's a list of operators that can be overloaded:

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


Operators That Can't Be Overloaded:

Certain operators cannot be overloaded:

::.*.?:
  • Operator overloading is applicable only to user-defined data types like objects or structures.
  • Overloading operators for built-in data types such as int, char, or float is not possible.
  • Operator overloading does not alter the precedence and associativity of operators.