Operator Overloading in c++

We have learned about objects and methods. Now, let’s learn about Operator Overloading in C++.

Operator Overloading

In C++ programming language, we can overload operators, i.e., make operators work for user-defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is operator overloading.

For example, we can overload the ‘+’ operator in a class like String to concatenate two strings just by using the ‘+’ operator. Other examples where arithmetic operators may be overloaded are Complex Numbers, Fractional Numbers, Big Integers, etc.

As we have already seen polymorphism, operator overloading is one example of compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.

Example:

int a;
float b, sum;
sum = a+b;

In the above example, the variables ‘a’ and ‘b’ are of types “int” and “float”, which are built-in data types. Hence the addition operator ‘+’ can easily add the contents of “a” and “b”. This is because the addition operator ‘+’ is predefined to add variables of built-in data type only.

Now let’s look at another example,

class A
{

};

int main()
{
      A   a1,a2,a3;

      a3= a1 + a2;

      return 0;
}

In this example, we have 3 variables “a1”, “a2” and “a3” of type “class A”.

Here we are trying to add two objects “a1” and “a2”, which are user-defined types i.e. of type “class A” using the “+” operator. This is not allowed, because the addition operator “+” is predefined to operate only on built-in data types. But here, “class A” is a user-defined type, so the compiler generates an error. This is where the concept of “Operator overloading” comes in.

See also  Types of Pointers (Continuation)

Now, if the user wants to make the operator “+” add two class objects, the user has to redefine the meaning of the “+” operator such that it adds two class objects. This is done by using the concept “Operator overloading”. So the main idea behind “Operator overloading” is to use C++ operators with class variables or class objects. Redefining the meaning of operators really does not change their original meaning; instead, they have been given additional meaning along with their existing ones.

Example:

#include<iostream>
using namespace std;
 
class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i = 0) {real = r;   imag = i;}
     
    // This is automatically called when '+' is used with
    // between two Complex objects
    Complex operator + (Complex const &obj) {
         Complex res;
         res.real = real + obj.real;
         res.imag = imag + obj.imag;
         return res;
    }
    void print() { cout << real << " + i" << imag << '\n'; }
};
 
int main()
{
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2;
    c3.print();
}

Output:

12 + i9

Now that we know operator overloading and how to use it to overload predefined operators you might get a question, can we overload all operators?

Almost all operators can be overloaded except a few. Following is the list of operators that cannot be overloaded.

  • sizeof
  • typeid
  • Scope resolution (::)
  • Class member access operators “.” (dot), “.*” (pointer to member operator)
  • Ternary Operator (?:)

Operators that can be overloaded

We can overload the following operators:

  • Unary operators
  • Binary operators
  • Special operators ( [ ], ( ) etc)

 That is all for this article, Happy Coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh