Types of Operator Overloading in C++

Operator Overloading in C++ is one of the exciting features that has enhanced the power of the extensibility of C++. C++ permits us to add two variables of user-defined types with the same syntax that is applied to the basic types. It means that C++ can provide the operators with a special meaning for a data type. The mechanism of giving such special meanings to an operator is known as Operator Overloading.

Operator Overloading provides a flexible option for creating new definitions for most of the C++ operators. We can overload all the C++ operators except the following:

1. Class member access operators (.*)
2. Scope Resolution operator (::)
3. Size Operator (sizeof)
4. Conditional operator (?:)

Overload Unary minus Operator:

Example:

#include<iostream.h>
#include<conio.h>
using namespace std;
class test
{
int x, y, z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-();
};
void test::getdata(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void test::display(void)
{
cout<<"x="<<x<<" ";
cout<<"y="<<y<<" ";
cout<<"z="<<z<<" ";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
test t1;
t1.getdata(15,-25,30);
cout<<"T:";
t1.display();
-t1;
cout<<"-T:";
t1.display();
return 0;
}

 

Output:
T: x=15 y=-25 z=30
-T: x=-15 y=25 z=-30

Overload Binary Operator:

Example:

#include<iostream.h>
#include<conio.h>
using namespace std;
class test
{
float p;
float q;
public:
test(){}
test( float r, float i){ p=r; q=i;}
test operator+(test);
void display(void);
};
test test::operator+(test t)
{
test t1;
t1.p=p+c.p;
t1.q=q+c.q;
return (t1);
}
void test::display(void)
{
cout<<p<<"+J"<<q<<"\n";
}
int main()
{
test C1, C2, C3;
C1=test(4.5, 5.5);
C2=test(2.5, 3.7);
C3=C1+C2;
cout<<"C1 =";C1.display();
cout<<"C2 =";C2.display();
cout<<"C3 =";C3.display();
return 0;
}

Output:
C1 = 4.5 + j5.5
C2 = 2.5 + j3.7
C3 = 7.0 + j9.2

Overload Operator using Friend Function:

Example:

#include<iostream.h>
const size=3;
class vector
{
int v[size];
public:
vector();
vector(int *x);
friend vector operator *(int a, vector b);
friend vector operator *(vector b, int a);
friend istream & operator >> (istream &, vector &);
friend ostream & operator >> (ostream &, vector &);
}
vector :: vector()
{
for(int i=0; i<size; i++)
v[i]=0;
}
vector :: vector(int *x)
{
for(int i=0; i<size; i++)
v[i]=x[i];
}
vector operator *(int a, vector b)
{
vector c;
for(int i=0; i<size; i++)
c.v[i]=a*b.v[i];
return c;
}
vector operator *(vector b, int a)
{
vector c;
for(int i=0; i<size; i++) c.v[i]=b.v[i] *a; return c; } istream & operator >> (istream &din, vector &b)
{
for(int i=0; i<size; i++) din>>b.v[i];
return(din);
}
ostream & operator >> (ostream &din, vector &b)
{
dout<<"("<<b.v[0];
for(int i=1; i<size; i++)
dout<<"," <<b.v[i];
dout<<")";
return (dout);
}
int x[size]=[2,4,6];
int main()
{
vector m;
vector n=x;
cout<<"Enter the elements of vector m:"<<; cin>>m;
cout<<"\n";
cout<<"m="<<m<<;
vector p,q;
p=2*m;
q=n*2;
cout<<"\n";
cout<<"p="<<p<<;
cout<<"q="<<q<<;
return 0;
}

Overloading Pointer to Member Operator:

Pointer to Member Operator in C++ (->) is normally used in conjunction with an object pointer to access any of the object’s members.

Example:

#include<iostream.h>
#include<conio.h>
using namespace std;
class temp
{
public:
int n;
temp(int j)
{
n=j;
}
temp*operator ->(void)
{
return this;
}
};
int main()
{
temp T(4);
temp *ptr=&T;
cout<<"T.n="<<T.n;
cout<<"ptr->n="<n;
cout<n="<n;
getch();
return 0;
}

Output:
T.n= 4
ptr->n= 4
T->n= 4

Subscript Operator Overloading:

Subscript Operator Overloading in C++ is normally used to access and modify a specific element in an array.
Example:

#include<iostream.h>
#include<conio.h>
using namespace std;
class test
{
int x[5];
public:
test(int *p)
{
int i;
for(i=0;i<5;i++)
x[i]=p[i];
}
int operator [] (int k)
{
return (x[k]);
}
};
int main()
{
int k[5]={1, 2, 3, 4, 5};
test T(k);
int i;
for(i=0;i<5;i++)
{
cout<<k[i]<<;
}
getch();
return 0;
}

Output:
1 2 3 4 5