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




