OPERATOR OVERLOADING IN C++

 Operator Overloading

The method of making operators to work for user defined classes and having the ability to provide operators with a special user defined meaning is known as operator overloading.

OR,

Operator overloading is a type of polymorphism in which an operation is overloaded to give user defined meaning to it.


Operator Overloading Fundamentals

◈ C++ allows the use of certain operators in some other meaning apart from its usual meaning

◈ Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators

◈ For eg, the operator ‘+’ can be overloaded to add two complex numbers

◈ However, when an operator is overloaded, its original meaning is not lost

◈ If the ‘+’ operator is overloaded, it can still be used to add two integers

◈ Although semantics of an operator can be extended, we cannot change its syntax (number of operators, precedence and associativity)


Restrictions of Operator Overloading

There are certain restrictions and limitations to be kept in mind while overloading operators:

  •  Only existing operators can be overloaded. New operators cannot be created.
  •  The overloaded operator must have at least one operand that is of user-defined type.
  •  We cannot change the basic meaning of an operator. That is to say, we cannot redefine the plus(+) operator to subtract one value from the other.
  •  Overloaded operators follow the syntax rules of the original operators. They cannot be overridden.
  •  There are some operators that cannot be overloaded:
    •  sizeof [Size of operator]
    •  . [Membership operator]
    •  .* [Pointer-to-member operator]
    •  :: [Scope resolution operator]
    •  ?: [Conditional op]

  •  We cannot use friend functions to overload certain operators. However, member functions can be used to overload them:
    •  = [Assignment operator]
    •  ( ) [Function call operator]
    •  [ ] [Subscripting operator]
    •  -> [Class member access operator]
  •  Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values, but, those overloaded by means of a friend function, take one reference argument (the object of the relevant class).
  •   Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit argument.
  •  When using binary operators overloaded through a member function, the left hand side operand must be an object of the relevant class.
  •  Binary arithmetic operators such as +, -, *, and / must explicitly return a value. They must not attempt to change their own argument


General Syntax 

  •  The general syntax for operator overloading is as follows: 

return_type operator operator_to_be_overloaded(parameters); 

  •  Here, “operator” is a keyword 
  •  Operator functions must be either non-static member functions or friend functions

Binary Operator Overloading

(1) Assignment operator overloading (using member function) 


#include <iostream>
using namespace std;
class Sample
{
private:
    int x, y;

public:
    Sample(int, int);
    void operator=(Sample);
    void display();
};
Sample::Sample(int a, int b)
{
    x = a, y = b;
}
void Sample::operator=(Sample obj)
{
    x = obj.y;
    y = obj.x;
}
void Sample::display()
{
    cout << endl
         << "x = " << x;
    cout << endl
         << "y = " << y;
}
int main()
{
    Sample obj1(20, 30), obj2(5, 7);
    obj1.display();
    obj2.display();
    obj2 = obj1; // overloaded definition called
    obj1.display();
    obj2.display();
    return 0;
}


OUTPUT:

x = 20

y = 30

x = 5

y = 7

x = 20

y = 30

x = 30

y = 20



(2) Comparison operator overloading (using member function)


#include <iostream>
using namespace std;
class Sample
{
private:
    int x;

public:
    void setvalue()
    {
        cout << "Enter value : ";
        cin >> x;
    }
    int operator<(Sample obj2)
    {
        if (x < obj2.x)
        {
            return x;
        }
        else
        {
            return obj2.x;
        }
    }
};

int main()
{
    Sample s1, s2;
    s1.setvalue();
    s2.setvalue();
    int res;
    res = s1 < s2; // overloaded definition called
    // res = s1.operator <(s2);
    cout << endl
         << "Smaller value = " << res;
    return 0;
}


OUTPUT :

Enter value : 55

Enter value : 65


Smaller value = 55


(3) Arithmetic Operator Overloading (using friend function)


#include <iostream>
using namespace std;
class Complex
{
private:
    int real, img;

public:
    Complex()
    {
        real = 0;
        img = 0;
    }
    Complex(int real, int img)
    {
        this->real = real;
        this->img = img;
    }
    friend Complex operator+(Complex, Complex);
    void display()
    {
        cout << real << " + " << img << "i";
    }
};
Complex operator+(Complex obj1, Complex obj2)
{
    /*Complex temp;
    temp.real = obj1.real + obj2.real;
    temp.img = obj1.img + obj2.img;
    return temp;*/
    // nameless temporary object
    return Complex(obj1.real + obj2.real, obj1.img + obj2.img);
}

int main()
{
    Complex c1(2, 3), c2(5, 1), c3;
    c3 = c1 + c2; // overloaded definition called
    //  or
    //  c3 = operator+(c1,c2);
    cout << "c1 = ";
    c1.display();
    cout << endl
         << "c2 = ";
    c2.display();
    cout << endl
         << "c3 = ";
    c3.display();
    return 0;
}

OUTPUT :

c1 = 2 + 3i

c2 = 5 + 1i

c3 = 7 + 4i