The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Operator Overloading

Operator Overloading is a feature of C++ through which we can assign special meanings to the operators i.e we can create new definition for the operators. For e.g, the addition operator ( + ) can be overloaded to concatenate two strings ( " cpp " + " tutorial " = " cpptutorial " ). All operators can be overloaded except scope resolution operator ( :: ) , sizeof operator, conditional operator ( ? : ) and class member access operator ( . , .* ).

Overloading Unary Operator
Following program illustrates the overloading of unary ( - ) operator :

#include<iostream>
using namespace std;

class X {
   int a, b;
public :
   X() { }
   X(int var1, int var2) {
      a = var1;
      b = var2;
   }
   void displayValues() {
      cout << "a : " << a << "  b : " << b << endl;
   }
   void operator-() { // overload unary (-) operator
      a = -a;
      b = -b;
   }
};

int main() {
   X obj(7, 2);
   obj.displayValues();
   -obj; // calling overloaded operator
   obj.displayValues();
   return 0;
}


Overloading Assignment ( = ) and ostream ( << ) operator
Following program illustrates the overloading of assignment and ostream operator :

#include<iostream>
using namespace std;

class X {
   int a, b;
public :
   X() { }
   X(int var1, int var2) {
      a = var1;
      b = var2;
   }
   void operator=(X &obj) { // overload assignment ( = ) operator
      a = obj.a;
      b = obj.b;
   }
   // overload ostream ( << ) operator
   friend ostream &operator<<(ostream &out, X &obj) {
      out << "a : " << obj.a << "  b : " << obj.b << endl;
   }
};

int main() {
   X obj1(7, 2), obj2;
   cout << "Displaying object1 values :- " << endl;
   cout << obj1; // calling overloaded ostream ( << ) operator
   obj2 = obj1; // calling overloaded assignment ( = ) operator
   cout << "Displaying object2 values :- " << endl;
   cout << obj2; // calling overloaded ostream ( << ) operator
   return 0;
}

Note that ostream ( << ) has been declared as a friend to the class X because ( << ) operator will be used without refering to any object ( cout << "Displaying object1 values :- " << endl; ).


Overloading Binary Operator
Following program illustrates the overloading of binary ( + ) operator :

#include<iostream>
using namespace std;

class X {
   int a, b;
public :
   X() { }
   X(int var1, int var2) {
      a = var1;
      b = var2;
   }
   void displayValues() {
      cout << "a : " << a << "  b : " << b << endl;
   }
   /* overload binary (+) operator
      This function adds the data members of two objects */
   X operator+(X &obj) {
      X temp_obj;
      temp_obj.a = this->a + obj.a;
      temp_obj.b = this->b + obj.b;
      return temp_obj;
   }
};

int main() {
   X obj1(7, 2), obj2(9, 3);
   cout << "Displaying object1 values : ";
   obj1.displayValues();
   cout << "Displaying object2 values : ";
   obj2.displayValues();
   X obj_sum = obj1 + obj2; // call overloaded (+) operator
   cout << "After adding 2 objects : ";
   obj_sum.displayValues();
   return 0;
}

We have demonstrated overloading of few operators but other operators like relational operators, postfix and prefix operators can also be overloaded in a similar manner.

Back | Next