Operator Overloading Examples

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Operator Overloading Examples

Almost all the operators can be overloaded in infinite different ways. Following are some examples to learn more about operator overloading. All the examples are closely connected.


Overloading Arithmetic Operator

Arithmetic operator are most commonly used operator in C++. Almost all arithmetic operator can be overloaded to perform arithmetic operation on user-defined data type. In the below example we have overridden the + operator, to add to Time(hh:mm:ss) objects.


Example: overloading '+' Operator to add two time object

Overloading I/O operator

  • Overloaded to perform input/output for user defined datatypes.
  • Left Operand will be of types ostream& and istream&
  • Function overloading this operator must be a Non-Member function because left operand is not an Object of the class.
  • It must be a friend function to access private data members.

You have seen above that << operator is overloaded with ostream class object cout to print primitive type value output to the screen. Similarly you can overload << operator in your class to print user-defined type to screen. For example we will overload << in time class to display time object using cout.

time t1(3,15,48);
cout << t1;

NOTE: When the operator does not modify its operands, the best way to overload the operator is via friend function.


Example: overloading '<<' Operator to print time object

#include< iostream.h>  
#include< conio.h>  
class time  
{  
 int hr,min,sec;  
 public:  
  time()  
  {  
   hr=0, min=0; sec=0;  
  }  
  
  time(int h,int m, int s)  
  {  
   hr=h, min=m; sec=s;  
  }
 friend ostream& operator << (ostream &out, time &tm);  //overloading '<<' operator
};

ostream& operator<< (ostream &out, time &tm)	//operator function 
{
 out << "Time is " << tm.hr << "hour : " << tm.min << "min : " << tm.sec << "sec"; 
 return out;
}

void main()
{
 time tm(3,15,45);
 cout << tm;
}

Output

Time is 3 hour : 15 min : 45 sec 

Overloading Relational operator

You can also overload Relational operator like == != >= <= etc. to compare two user-defined object.


Example

class time  
{  
int hr,min,sec;  
 public:  
  time()  
  {  
   hr=0, min=0; sec=0;  
  }  
  
  time(int h,int m, int s)  
  {  
   hr=h, min=m; sec=s;  
  }  
 friend bool operator==(time &t1, time &t2);   //overloading '==' operator
};
bool operator== (time &t1, time &t2)	      //operator function  	
{
return ( t1.hr == t2.hr &&
	 t1.min == t2.min && 
         t1.sec == t2.sec );
} 

Copy constructor Vs. Assignment operator

Assignment operator is used to copy the values from one object to another already existing object. For example

time tm(3,15,45);	//tm object created and initialized
time t1;  		//t1 object created
t1 = tm; 		//initializing t1 using tm

Copy constructor is a special constructor that initializes a new object from an existing object.

time tm(3,15,45);	//tm object created and initialized
time t1(tm);  		//t1 object created and initialized using tm object