//Program : Part B -5
//Create a class called 'Timer' which includes the data members- hours,minutes
//and seconds. Use the method
//a)To accept the time
//b)To display the time
//c)To increment time by one second by overloading unary operator ++.
//d)To decrement time by one second by overloading unary operator --.
//Write a menu driven program for the above operation.(Hint : Minutes and
//seconds must be always within the range 0-59).
//Name : LDK E-NOTES Reg.No. : **********
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
class timer
{
private:
int h,m,s;
public:
void accept();
void display();
void operator++();
void operator--();
};
void timer::accept()
{
while(1)
{
cout<<"Enter hour : ";
cin>>h;
cout<<"Enter minutes : ";
cin>>m;
cout<<"Enter seconds : ";
cin>>s;
if(h<0||m<0||s<0||h>23||m>59||s>59)
cout<<"Invalid time"<<endl;
else
break;
}
}
void timer::display()
{
cout<<"Time = "<<h<<":"<<m<<":"<<s<<endl;
}
void timer::operator++()
{
s=s+1;
if(s>59)
{
s=0;
m=m+1;
}
if(m>59)
{
m=0;
h=h+1;
}
if(h>23)
{
h=0;
}
}
void timer::operator--()
{
s=s-1;
if(s<0)
{
s=59;
m=m-1;
}
if(m<0)
{
m=59;
h=h-1;
}
if(h<0)
{
h=23;
}
}
void main()
{
timer t;
int ch;
clrscr();
while(1)
{
cout<<"1.Accept time\n";
cout<<"2.Dispaly time\n";
cout<<"3.Increment\n";
cout<<"4.Decrement\n";
cout<<"5.Exit\n";
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : t.accept();
break;
case 2 : t.display();
break;
case 3 : ++t;
cout<<"Time is incremented by one second"<<endl;
break;
case 4 : --t;
cout<<"Time is decremented by one second"<<endl;
break;
case 5 : exit(0);
default : cout<<"Invalid choice...";
}
getch();
}
}
Output :
1.Accept time
2.Dispaly time
3.Increment
4.Decrement
5.Exit
Enter your choice : 1
Enter hour : 3
Enter minutes : 40
Enter seconds : 45
1.Accept time
2.Dispaly time
3.Increment
4.Decrement
5.Exit
Enter your choice : 2
Time = 3:40:45
1.Accept time
2.Dispaly time
3.Increment
4.Decrement
5.Exit
Enter your choice : 3
Time is incremented by one second
1.Accept time
2.Dispaly time
3.Increment
4.Decrement
5.Exit
Enter your choice : 2
Time = 3:40:46
1.Accept time
2.Display time
3.Increment
4.Decrement
5.Exit
Enter your choice : 4
Time is decremented by one second
1.Accept time
2.Display time
3.Increment
4.Decrement
5.Exit
Enter your choice : 2
Time = 3:40:45
No comments:
Post a Comment