Lecture 13 Operator Overloading
Lecture 13 Operator Overloading
+ - * / % ^ & |
~ ! = < > += -= *=
new[] delete[]
. .* :: ?:
Operator overloading is an important concept in C++. It is a type of polymorphism in which an
operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform
operation on user-defined data type. For example '+' operator can be overloaded to perform
Almost any operator can be overloaded in C++. However there are few operator which can not
be overloaded. Operator that are not overloaded are follows
scope operator - ::
sizeof
member selector - .
member pointer selector - *
ternary operator - ?:
1. Member Function
2. Non-Member Function
3. Friend Function
Operator overloading function can be a member function if the Left operand is an Object of that
class, but if the Left operand is different, then Operator overloading function must be a non-
member function.
Operator overloading function can be made friend function if it needs access to the private and
protected members of class.
Following are some restrictions to be kept in mind while implementing operator overloading.
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.
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
#include<iostream.h>
#include<conio.h>
class time
{
inth,m,s;
public:
time()
{
h=0, m=0; s=0;
}
voidgetTime();
void show()
{
cout<< h<< ":"<< m<< ":"<< s;
}
time operator+(time);//overloading '+' operator
};
time time::operator+(time t1) //operator function
{
time t;
inta,b;
a=s+t1.s;
t.s=a%60;
b=(a/60)+m+t1.m;
t.m=b%60;
t.h=(b/60)+h+t1.h;
t.h=t.h%12;
return t;
}
void time::getTime()
{
cout<<"\n Enter the hour(0-11) ";
cin>>h;
cout<<"\n Enter the minute(0-59) ";
cin>>m;
cout<<"\n Enter the second(0-59) ";
cin>>s;
}
void main()
{
clrscr();
time t1,t2,t3;
cout<<"\n Enter the first time ";
t1.getTime();
cout<<"\n Enter the second time ";
t2.getTime();
t3=t1+t2; //adding of two time object using '+' operator
cout<<"\n First time ";
t1.show();
cout<<"\n Second time ";
t2.show();
cout<<"\n Sum of times ";
t3.show();
getch();
}
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.
#include<iostream.h>
#include<conio.h>
class time
{
inthr,min,sec;
public:
time()
{
hr=0, min=0; sec=0;
}
void main()
{
time tm(3,15,45);
cout<< tm;
}
Output
You can also overload Relational operator like == ,!= , >= , <= etc. to compare two user-
defined object.
Example
class time
{
inthr,min,sec;
public:
time()
{
hr=0, min=0; sec=0;
}
Assignment operator is used to copy the values from one object to another already existing
object. It is present in all classes by default. For example
Copy constructor is a special constructor that initializes a new object from an existing object.
#include<iostream>
usingnamespace std;
classDistance
{
private:
int feet;// 0 to infinite
int inches;// 0 to 12
public:
// required constructors
Distance(){
feet=0;
inches=0;
}
Distance(int f,inti){
feet= f;
inches=i;
}
// method to display distance
voiddisplayDistance()
{
cout<<"F: "<< feet <<" I:"<< inches <<endl;
}
// overloaded minus (-) operator
Distanceoperator-()
{
feet=-feet;
inches=-inches;
returnDistance(feet, inches);
}
// overloaded < operator
booloperator<(constDistance& d)
{
if(feet <d.feet)
{
returntrue;
}
if(feet ==d.feet&& inches <d.inches)
{
returntrue;
}
returnfalse;
}
};
int main()
{
DistanceD1(11,10), D2(5,11);
if( D1 < D2 )
{
cout<<"D1 is less than D2 "<<endl;
}
else
{
cout<<"D2 is less than D1 "<<endl;
}
return0;
}