Operator Overloading C++

C++ Operator overloading

Operator Overloading C++ 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.

operator overloading

Operator Overloading in C++

  • 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.

Why do we need operator overloading?

Operators work with operands. For example, we can use an increment operator with int values or int type variables like count ++.

But, we can’t use these operators with objects, directly.

However, we can overload these operators and give them special meanings to run along with objects and write code to increment the value by 100 for all variables. This is done via operator overloading.

Operator Overloading C++

How to define Overloading?

  1. We must use a keyword ‘operator’ before the operator want to overload
  2. It works as a function itself thus, a 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 wherein we want to –

  • Overload the ++ operator to increment all the int variables in the class
  • Increment data member variables by 100 not just simple 1 value

Run

// Program to overload ++ operator to increment data member
// values by 100 each, everytime ++obj is called using operator overloading
#include <iostream>
using namespace std;

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

int main()
{
    PrepInsta obj;
  
    cout << "Values initially were -" << endl;
    obj.print();
    
    // calling overloaded ++ operator on obj object
    ++obj;
  
    cout << "\nNew Values are -" << endl;
    obj.print();

    return 0;
}

Output

Values initially were -
1 2 3 4 respectively

New Values are - 
101 102 103 104 respectively

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.

::.*.?:

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

Implicit Conversion via Conversion Constructors as assignment operator is overloaded

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

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

Let us look at an example to understand this –

Run

#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 can call it
    p1 = 100;
    p1.display();
 
    return 0;
}

Output

The value is: 50
The value is: 100

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription