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++:Overloading Unary operator

Example:11
Declare a class named time with attributes as (hh,mm,ss) increment the time using overloaded increment operator “++”.

//Declare a class named time with attributes as
// h,m,s
// increment the time using ++ overloaded operator
#include<iostream>
#include<conio.h>
using namespace std;
class time
{
private:
	int h,m,s;
public:
	time();
	time(int h1,int m1,int s1);
	void read();
	void show();
	void operator++();
};
time::time()
{
  h=0; m=0; s=0;
}
time::time(int h1,int m1,int s1)
{
  h=h1; m=m1; s=s1;
}
void time::read()
{
  cout<<"enter time h,m,s ";
  cin>>h>>m>>s;
}
void time::show()
{
  cout<<"h "<<h<<" m "<<m<<" s "<<s<<endl;
}
void time::operator++()
{
  s++;
  if(s>=60)
  {
    m=m+1;
    s=0;
  }
  if(m>=60)
  {
   h=h+1;
   m=0;
  }
}
int main()
{
  time t;
  t.read();  t.show();
  ++t;//t.operator++();
  t.show();
  getch();
  return(0);
}

Output:

enter time h,m,s 10 10 25
h 10 m 10 s 25
h 10 m 10 s 26

Example:12
Declare a class named time with attributes as (hh,mm,ss) decrement the time using overloaded operator “–“.

//Declare a class named time with attributes as
// h,m,s
//to decrement the time using overloaded -- operator
#include<iostream>
#include<conio.h>
using namespace std;
class time
{
private:
	int h,m,s;
public:
	time();
	time(int h1,int m1,int s1);
	void read();
	void show();
	void operator--();
};
time::time()
{
  h=0;m=0;s=0;
}
time::time(int h1,int m1,int s1)
{
  h=h1;m=m1;s=s1;
}
void time::read()
{
  cout<<"enter time h,m,s ";
  cin>>h>>m>>s;
}
void time::show()
{
  cout<<"h "<<h<<" m "<<m<<" s "<<s<<endl;
}
void time::operator--()
{
  s--;
  if(s<=0)
  {
    m=m-1;
    s=59;
  }
  if(m<=0)
  {
   h=h-1;
   m=59;
  }
}
int main()
{
  time t;
  t.read();  t.show();
  --t;
  t.show();
  getch();
  return(0);
}

/*
example:
h : 5   m :6 	s=10
--t;
h:5	m:6	s=9

h:5	m:6	s=0
h:5	m=5	s=59

h:10 	m:0 	s:0
h:9 	m=:59 	s:59
*/

Output:

enter time h,m,s 10 12 25
h 10 m 12 s 25
h 10 m 12 s 24