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 13

Example:16
Declare a class named “distance” with attributes as km, m, cm and mm. Take input for two objects calculate and print their sum using overloaded binary operator “+”.

code

Output:

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

code

Output:

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

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

#include<iostream>
#include<conio.h>
using namespace std;
class time
{
private:
	int h,m,s;
public:
	void read();
	void show();
	time operator+(time t1);
};
void time::read()
{
  cout<<"enter h,m and s ";
  cin>>h>>m>>s;
}
void time::show()
{
  cout<<h<<" : "<<m<<" : "<<s<<endl;
}
time time::operator+(time t1)
{
  time t;
  t.h=h+t1.h;
  t.m=m+t1.m;
  t.s=s+t1.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=e1.operator(e2);
  e3.show();
  getch();
  return(0);
}


Output:

enter h,m and s 3 6 9
enter h,m and s 4 5 8
3 : 6 : 9
4 : 5 : 8
7 : 11 : 17