Operator Overloading C++

C++ Operator Overloading

Operator Overloading allows us to change how we can use operators such as +, -, ++, & etc. This can be done for user defined class and we can write our own code to give them new functionality.

For example –

  • We can use + to concatenate two strings together using an equation like str1 = str2 + str3, and can choose not to use concat() function.
  • We can override ++ operator to not increment value by 1. But, rather by 100 possibly.
Operator Overloading in C++
Operator Overloading in C++

Why we need operator overloading?

  • Operators work with operands.
    • For example we can use increment operator with int values or int type variables like count ++.
    • But, we can’t use these operator with objects, directly
    • Consider we want to use this with object : obj1 as obj++ that increments all the variables inside the class by 100.
    • We can’t do that simply as obj++ as these operators don’t work with classes.
  • However, we can overload these operators and give them special meanings to run along with object and write code to increment the value by 100 for all variables.

How to define Overloading?

  1. We must use keyword operator before the operator we want to overload
  2. It works as a function itself thus, return type must also be defined

Example –

void operator ++()
{
   counter1 = counter1 + 1;
   counter2 = counter2 + 1;
}

Note – Overloaded operators will only work with objects of the class under which they are defined.

Example

Let us take an example where in we want to overload the ++ operator to increment all the int variables in the class by 100 not just 1.

#include 
using namespace std;

class PrepInsta
{
  int x = 1, y = 2, count1 = 3, count2 = 4;
  public:
   void print()
   {
     cout << x << " " << y << " " 
     << count1 << " " << count2 << " respectively"<< endl;
   }
   void operator ++()
   {
     x = x + 100;
     y = y + 100;
     count1 = count1 + 100;
     count2 = count2 + 100;
   }
};

int main()
{
  PrepInsta p1;
  
  cout << "Values initially were -" << endl;
  p1.print();
  ++p1;
  cout << "New Values are -" << endl;
  p1.print();

  return 0;
}

The output of the following will be –

Values initially were -
1 2 3 4 respectively 
New Values are - 
101 102 103 104 respectively

Important points to note

Operators for which overloading is allowed –

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

Operators for which Overloading is not allowed –

There are certain reasons because of which the writer of C++ Bjarne Stroustrup didn’t allow the same. Read it here on his official blog on why he didn’t allow overloading to certain operators.

::.*.?:

Assignment Operators working with Objects are by default overloaded by C++

When we write a class in C++ the compiler by default automatically creates its own version of opertor overloading for assignment operator. What this essentially does is, consider two objects obj1 and obj2if we simply want to copy all the data values for obj1 into obj2 we can directly use assignment operator as obj1 = obj2

It would work the same as copy constructor only it does shallow copy which may cause problem while working with pointers, you can read more about that here.

 

Operator Overloading in C++

Implicit Conversion via Conversion Constructors as assignment operator is overloaded

Now, the above line may be confusing for a few people, that may get confused. First we suggest reading the following page –

  1. The conversion constructors are the ones which can be called with only single argument.
  2. In such cases if we directly use assignment operator with an object and value.
  3. Can be used for implicit conversion.

Let us look at example to understand this –

#include <iostream>
using namespace std;

class PrepInsta
{
int x;
public:

void display()
{
cout<<"The value is: " << x <<endl;
}
PrepInsta(int x1)
{
x = x1;
}

};

int main()
{
PrepInsta p1(50);
p1.display();
// Default Operator Overloading happens here
// Calls constructor again as single passing argument cal call it.
p1 = 100;
p1.display();
return 0;
}

Please Login/Signup to comment