Important Points about Operator Overloading

1) For operator overloading to work, at least one of the operands must be a user-defined class object.

2) Assignment Operator: Compiler automatically creates a default assignment operator with every class. The default assignment operator does assign all members of the right side to the left side and works fine in most cases (this behavior is the same as the copy constructor). See this for more details.

3) Conversion Operator: We can also write conversion operators that can be used to convert one type to another type. 

Example: 

C++




// C++ Program to Demonstrate the working
// of conversion operator
#include <iostream>
using namespace std;
class Fraction {
private:
    int num, den;
 
public:
    Fraction(int n, int d)
    {
        num = n;
        den = d;
    }
 
    // Conversion operator: return float value of fraction
    operator float() const
    {
        return float(num) / float(den);
    }
};
 
int main()
{
    Fraction f(2, 5);
    float val = f;
    cout << val << '\n';
    return 0;
}


Output

0.4

Overloaded conversion operators must be a member method. Other operators can either be the member method or the global method.

4) Any constructor that can be called with a single argument works as a conversion constructor, which means it can also be used for implicit conversion to the class being constructed. 

Example:

C++




// C++ program to demonstrate can also be used for implicit
// conversion to the class being constructed
#include <iostream>
using namespace std;
 
class Point {
private:
    int x, y;
 
public:
    Point(int i = 0, int j = 0)
    {
        x = i;
        y = j;
    }
    void print()
    {
        cout << "x = " << x << ", y = " << y << '\n';
    }
};
 
int main()
{
    Point t(20, 20);
    t.print();
    t = 30; // Member x of t becomes 30
    t.print();
    return 0;
}


Output

x = 20, y = 20
x = 30, y = 0


Operator Overloading in C++

in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.

In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot overload in C++.

Similar Reads

C++ Operator Overloading

C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. Operator overloading is a compile-time polymorphism. For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are Complex Numbers, Fractional Numbers, Big integers, etc....

Example of Operator Overloading in C++

...

Difference between Operator Functions and Normal Functions

C++ // C++ Program to Demonstrate // Operator Overloading #include 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(); }...

Can We Overload All Operators?

...

Operators that can be Overloaded in C++

Operator functions are the same as normal functions. The only differences are, that the name of an operator function is always the operator keyword followed by the symbol of the operator, and operator functions are called when the corresponding operator is used....

Why can’t the above-stated operators be overloaded?

...

Important Points about Operator Overloading

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