C++ Tutorial Home Page

C++ Operator Overloading

What is operator overloading
Rules for overloading Operators
Operator Overloading restrictions
List of operators that cannot be overloaded.
Overloading Unary operator
Overloading increment operator(++)
Overloading decrement operator(–)
Overloading unary operator(-)
Overloading increment operator(++) postfix
Overloading decrement operator(–) postfix
Overloading increment operator (++) with friend function
Overloading increment operator (–) with friend function
Increment the time using ++
Decrement the time using ++
Overloading Binary operator(+,-,*,/,%)
Add two object using “+” operator with friend function
Overloading “-” operator
Overloading “*” operator
Overloading “/” operator
Overloading “%” operator
overloading all (+,-,*,/,%)
Overloading “+” operator with friend function
class distance (feet,inches) with “+” operator
class distance (feet,inches) with “+” operator with friend function
class distance (km,m) with “+” operator
class distance (km,m) with “+” operator with friend function
class distance (m,cm) with “+” operator
class distance (m,cm) with “+” operator with friend function
class distance (km,m,cm) with “+” operator
class distance (km,m,cm) with “+” operator with friend function
class distance (km,m,cm,mm) with “+” operator
class distance (km,m,cm,mm) with “+” operator with friend function
class time(hh,mm,ss) with “+” operator
class time(hh,mm,ss) with “+” operator with friend function
class complex(real,ing) with “+” operator
class complex(real,ing) with “+” operator with friend function
class string(str) with “+” operator
class string(str) with “+” operator with friend function
overloading “+=” operator

C++:Operator Overloading 14

Example:19
Declare a class named “distance” with attributes as hh, mm, ss. Take input for two objects calculate and print their sum using overloaded binary operator “+” with friend function.

//Add two time objects 
//using "+" overloaded operator
//using friend function

#include<iostream>
#include<conio.h>
using namespace std;
class time
{
private:
	int h,m,s;
public:
	void read();
	void show();
	friend time operator+(time t1,time t2);
};
void time::read()
{
  cout<<"enter h,m and s ";
  cin>>h>>m>>s;
}
void time::show()
{
  cout<<h<<" : "<<m<<" : "<<s<<endl;
}
time operator+(time t1,time t2)
{
  time t;
  t.h=t1.h+t2.h;
  t.m=t1.m+t2.m;
  t.s=t1.s+t2.s;
  if(t.s>=60)
  {
    t.m=t.m+t.s/60;
    t.s=t.s%60;
  }
  if(t.m>=60)
  {
    t.h=t.h+t.m/60;
    t.m=t.m%60;
  }
  return(t);
}
int main()
{
  time e1,e2,e3;
  e1.read();e2.read();
  e1.show();e2.show();
  e3=e1+e2;//e3=operator+(e1,e2);
  e3.show();
  getch();
  return(0);
}


Output:

enter h,m and s 2 5 36
enter h,m and s 4 25 12
2 : 5 : 36
4 : 25 : 12
6 : 30 : 48

Example:20
Declare a class named “complex” with attributes as real and img. Take input for two objects calculate and print their sum using overloaded binary operator “+”.

//to add two comples numbers 
//using overloaded operator "+"
#include<iostream>
#include<conio.h>
using namespace std;
class complex
{
private:
	int real,img;
public:
	void read();
	void show();
	complex operator+(complex c);
};
void complex::read()
{
 cout<<"Enter values of real and img ";
 cin>>real>>img;
}
void  complex::show()
{
  if(img>=0)
  cout<<real<<"  +i  "<<img<<endl;
  else
  cout<<real<<"  -i  "<<img*-1<<endl;
}
complex complex::operator+(complex c1)
{
 complex t;
 t.real=real+c1.real;
 t.img=img+c1.img;
 return(t);
}
int main()
{
  complex e1,e2,e3;
  e1.read();e2.read();
  e1.show();e2.show();
  e3=e1+e2;//e3=e1.operator+(e2);
  e3.show();
  getch();
  return(0);
}

Output:

Enter values of real and img 10 25
Enter values of real and img 3 6
10 +i 25
3 +i 6
13 +i 31

Example:21
Declare a class named “complex” with attributes as real and img. Take input for two objects calculate and print their sum using overloaded binary operator “+” with friend function.

//to add two comples numbers 
//using overloaded operator "+"
//Using friend function
#include<iostream>
#include<conio.h>
using namespace std;
class complex
{
private:
	int real,img;
public:
	void read();
	void show();
	friend complex operator+(complex c1,complex c2);
};
void complex::read()
{
 cout<<"Enter values of real and img ";
 cin>>real>>img;
}
void  complex::show()
{
  if(img>=0)
  cout<<real<<"  +i  "<<img<<endl;
  else
  cout<<real<<"  -i  "<<img*-1<<endl;
}
complex operator+(complex c1,complex c2)
{
 complex t;
 t.real=c1.real+c2.real;
 t.img=c1.img+c2.img;
 return(t);
}
int main()
{
  complex e1,e2,e3;
  e1.read();e2.read();
  e1.show();e2.show();
  e3=e1+e2;//e3=operator+(e1,e2);
  e3.show();
  getch();
  return(0);
}

Output:

Enter values of real and img 25 9
Enter values of real and img 3 -5
25 +i 9
3 -i 5
28 +i 4