Operator Overloading

Operator Overloading is the mechanism of giving special meaning to an operator is called operator overloading.
We are familiar with the use of different types of operators with primitive data types. But with abstract data types like queues, stacks, lists etc. we have to define each operator. This is called operator overloading.

Operator overloading is a technique which enhances the extensibility of C++. For example, if we overload an operator, we can achieve different functionalities with that operator. Like "+" operator. We can achieve concatenation - in case of strings and addition - in case of numbers. This can be the best example of operator overloading. It means that "+" operator is overloaded for integers and strings. C++ has the ability to provide the operators with a special meaning for a data type.

Which operators can we overload?

There is a range of operators which can be overloaded. It includes unary operators, arithmetic operators, logical operators and many more. We can overload all operators in C++ except:
  • Scope resolution operator (::)
  • Ternary operator (?:)
  • Size of operator (sizeof)
  • Dot operator(. , .*)

type className :: operator op (argList)
{
   function body 	// task defined.
}

Overloading Unary Operator:

We are going to oveload "--" operator and converting its functionality to "++" operator.

operatorOverloading.cpp
#include <iostream>
using namespace std;

class UnaryOp{
	int a;
	public:
		UnaryOp(int a){
			this->a = a;
		}
		void show(){
			cout<<"Value of a is "<<a<<endl;
		}
		void operator --(){
			a = a+1;
		}
};
int main(){
	UnaryOp op1(10);
	--op1;
	op1.show();
}

In this example, we have overloaded the pre-decrement operator and now it is working like pre-increment operator.

Overloading Binary Operator:

We are going to add "+" operator for adding complex numbers.

operatorOverloading.cpp
#include <iostream>
using namespace std;

class complex
{
double real;
double imag;

public:
	//default constructor
	complex() {}
		//parameterized constructor
	complex(double r, double i){
		real = r;
		imag = i;
	}
	complex operator+(complex);
	void print(){
		cout<<real<<" + "<<imag<<"i";
	}
};


complex complex :: 
  operator+(complex param)
{
complex temp;
temp.real = this->real + param.real;
temp.imag = this->imag + param.imag;
return temp;
}


int main()
{
complex c1(3.1, 1.5);
complex c2(1.2, 2.2);
complex c3;
c3 = c1 + c2;  

c3.print();
}

Overloading Insertion Operator:

Primarily operator used for insertion is the right shift operator used for shifting bits to right. In this example, we will overload thid operator.

operatorOverloading.cpp
#include <iostream>
using namespace std;

class Person {
  public:
    Person(const string& name, int age)
      : name(name), age(age) {}

    // Overloading the insertion operator 
    friend ostream& operator<<
      (ostream& os, const Person& person) {
      os << "Name: " << person.name;
      os << "Age: "<< person.age;
      return os;
    }

  private:
    string name;
    int age;
  };

int main() {
  Person john("John Doe", 30);
  Person jane("Jane Smith", 25);

  cout << "Person 1: " << john << endl;
  cout << "Person 2: " << jane << endl;

  return 0;
}


Overloading Extraction Operator:

Primarily operator used for extraction is the left shift operator used for shifting bits to left. In this example, we will overload thid operator.

operatorOverloading.cpp
#include <iostream>
#include <string>
using namespace std;

class Person {
public:
    Person() : name(""), age(0) {}
    
    // Overloading the extraction operator 
    friend istream& operator>>
      (istream& is, Person& person) {
        cout << "Enter name: ";
        is >> person.name;
        cout << "Enter age: ";
        is >> person.age;
        return is;
    }

private:
    string name;
    int age;
};

int main() {
    Person john;
    
    /* Inputting data into the object 
    using the overloaded >> operator */
    cin >> john;

    // Outputting the entered data
    cout << "Name: " << john.getName() << ", "
     << "Age: " << john.getAge() << endl;

    return 0;
}