Operator Overloading
When an operator is overloaded with multiple jobs it is known as operator overloading. It is a way to implement compile time polymorphism.
Key Points about Operator Overloading
- For operator overloading to work, at least one of the operands must be a user defined class object.
- Compiler automatically creates a default assignment operator with every class. The default assignment operator does assign all members of right side to the left side and works fine most of the cases (this behavior is same as copy constructor).
- We can also write conversion operators that can be used to convert one type to another type.
- Any constructor that can be called with a single argument works as a conversion constructor, means it can also be used for implicit conversion to the class being constructed.
1. Arithmetic Operator Overloading
Following is the example to demonstrate arithmetic operator overloading.
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Check
{
private:
float x,y;
public:
void setData(int a,int b)
{
x=a; y=b;
}
void getData()
{
cout<<"\nx:"<<x<<" y:"<<y;
}
//Defining an operator function + to overload
Check operator +(Check c)
{
Check temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
//Defining an operator function - to overload
Check operator -(Check c)
{
Check temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}
//Defining an operator function * to overload
Check operator *(Check c)
{
Check temp;
temp.x=x*c.x;
temp.y=y*c.y;
return temp;
}
//Defining an operator function / to overload
Check operator /(Check c)
{
Check temp;
temp.x=x/c.x;
temp.y=y/c.y;
return temp;
}
};
void main()
{
clrscr();
Check c1,c2,c3,c4,c5,c6;
c1.setData(10,20);
c2.setData(15,25);
//Overloading operator +
c3=c1+c2;
c3.getData();
//Overloading operator -
c4=c1-c2;
c4.getData();
//Overloading operator *
c5=c1*c2;
c5.getData();
//Overloading operator /
c6=c2/c1;
c6.getData();
getch();
} |
Output
x:25 y:45
x:-5 y:-5
x:150 y:500
x:1.5 y:1.25 |
2. Increment/Decrement Operator Overloading
Following is the example to demonstrate increment/decrement operator overloading.
Example :
#include<iostream.h>
#include<conio.h>
class Overload
{
private:
int i;
public:
Overload(int x)
{
i=x;
}
void operator ++()
{
++i;
}
void operator --()
{
--i;
}
void Display()
{
cout << "i=" << i << endl;
}
};
void main()
{
clrscr();
cout<<"\nOriginal ";
Overload op(10);
// Displays the value of data member i for object op
op.Display();
// Invokes operator function void operator ++( )
++op;
cout<<"\nAfter Increment ";
// Displays the value of data member i for object op
op.Display();
// Invokes operator function void operator ++( )
--op;
cout<<"\nAfter Decrement ";
// Displays the value of data member i for object op
op.Display();
getch();
} |
Output :
3. Memory Management Operator Overloading : New and Delete
New and Delete operators can be overloaded globally or they can be overloaded for specific classes.
- If these operators are overloaded using member function of a class, it means that these operators are overloaded only for that specific class.
- If overloading is done outside a class (i.e. it is not a member function of a class), the overloaded ‘new’ and ‘delete’ will be called anytime you make use of these operators (within classes or outside classes). This is called global overloading.
Syntax :
1.new Operator
void* operator new(size_t size); |
2. delete Operator
void operator delete(void*); |
Overloading new and delete at Global Level
Example :
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
void * operator new(size_t size)
{
cout << "New operator overloading\n ";
void * ptr = malloc(size);
return ptr;
}
void operator delete(void * p)
{
cout << "\nDelete operator overloading\n ";
free(p);
}
void main()
{
int n = 5, i;
int * p = new int[3];
clrscr();
for (i = 0; i<n; i++)
{
p[i]= i;
}
cout << "Array: ";
for(i = 0; i<n; i++)
{
cout << p[i] << " ";
}
delete p;
getch();
} |
Output :
Overloading new and delete for a Specific Class
Example :
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class student
{
private:
int num;
public:
student()
{
cout<<"\nConstructor Called";
}
student(int number)
{
num=number;
}
void display()
{
cout<< "\nnum:"<<num;
}
void* operator new(size_t size)
{
cout<< "\nOverloading new operator with size:"<<size<<"\n";
void* p =::new student(20);
return p;
}
void operator delete(void * p)
{
cout<< "\nOverloading delete operator\n";
free(p);
}
};
void main()
{
student* p = new student(10);
clrscr();
p->display();
delete p;
getch();
} |
Output :
For More Object Oriented Programming Lab Experiments Click Here