#include<iostream.h>
#include<process.h>
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
Time();
void addhour(int);
void addminute(int);
void addsecond(int);
void displaytime();
void addtime();
int gethour();
int getminute();
int getsecond();
void sethour(int);
void setminute(int);
void setsecond(int);
};
Time::Time()
{
sethour(0);
setminute(0);
setsecond(0);
}
void Time::addhour(int h)
{
h=gethour()+h;
if(h>24)
{
h=h%24;
}
sethour(h);
}
//function to get the value of hour
int Time::gethour()
{
return hours;
}
//function to set the value of the hour
void Time::sethour(int h)
{
hours=h;
}
//function to add the minutes in current minutes
void Time::addminute(int m)
{
m=getminute()+m;
if(m>60)
{
m=m%60;
addhour(1);
}
setminute(m);
}
//function to get the value of current minutes
int Time::getminute()
{
return minutes;
}
//function to set the value of current value
void Time::setminute(int m)
{
minutes=m;
}
//function to add the seconds in current second value
void Time::addsecond(int s)
{
s=getsecond()+s;
if(s>60)
{
s=s%60;
addminute(1);
}
setsecond(s);
}
//function to get the value of current second value
int Time::getsecond()
{
return seconds;
}
//function to set the value of current second value
void Time::setsecond(int s)
{
seconds=s;
}
void Time::displaytime()
{
cout<<"Time:: ";
cout.width(2);
cout.fill('0');
cout<<gethour()<<":";
cout.width(2);
cout.fill(2);
cout<<getminute()<<":";
cout.width(2);
cout.fill('0');
cout<<getsecond();
}
void Time::addtime()
{
int h,m,s;
cout<<"Enter hours, minutes and seconds to add in time";
cin>>h>>m>>s;
addsecond(s);
addminute(m);
addhour(h);
}
void main()
{
int i;
Time t;
int h;
int m;
int s;
do
{
cout<<endl<<"Enter 1.Add hours \n2.Add minutes \n3.Add seconds \n4.Add time \n5.Show time \n6.Exit";
cout<<endl<<"Enter your choice";
cin>>i;
switch(i)
{
case 1:
cout<<endl<<"How many hours to add";
cin>>h;
t.addhour(h);
break;
case 2:
cout<<endl<<"How many minutes to add";
cin>>m;
t.addminute(m);
break;
case 3:
cout<<endl<<"How many seconds to add";
cin>>s;
t.addsecond(s);
break;
case 4:
t.addtime();
break;
case 5:
t.displaytime();
break;
case 6:
exit(0);
default:
cout<<endl<<"Please enter a correct choice";
}
}while(i!=6);
}
output
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
1
How many hours to add5
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
2
How many minutes to add25
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
5
Time::05:25:00
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
6
#include<process.h>
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
Time();
void addhour(int);
void addminute(int);
void addsecond(int);
void displaytime();
void addtime();
int gethour();
int getminute();
int getsecond();
void sethour(int);
void setminute(int);
void setsecond(int);
};
Time::Time()
{
sethour(0);
setminute(0);
setsecond(0);
}
void Time::addhour(int h)
{
h=gethour()+h;
if(h>24)
{
h=h%24;
}
sethour(h);
}
//function to get the value of hour
int Time::gethour()
{
return hours;
}
//function to set the value of the hour
void Time::sethour(int h)
{
hours=h;
}
//function to add the minutes in current minutes
void Time::addminute(int m)
{
m=getminute()+m;
if(m>60)
{
m=m%60;
addhour(1);
}
setminute(m);
}
//function to get the value of current minutes
int Time::getminute()
{
return minutes;
}
//function to set the value of current value
void Time::setminute(int m)
{
minutes=m;
}
//function to add the seconds in current second value
void Time::addsecond(int s)
{
s=getsecond()+s;
if(s>60)
{
s=s%60;
addminute(1);
}
setsecond(s);
}
//function to get the value of current second value
int Time::getsecond()
{
return seconds;
}
//function to set the value of current second value
void Time::setsecond(int s)
{
seconds=s;
}
void Time::displaytime()
{
cout<<"Time:: ";
cout.width(2);
cout.fill('0');
cout<<gethour()<<":";
cout.width(2);
cout.fill(2);
cout<<getminute()<<":";
cout.width(2);
cout.fill('0');
cout<<getsecond();
}
void Time::addtime()
{
int h,m,s;
cout<<"Enter hours, minutes and seconds to add in time";
cin>>h>>m>>s;
addsecond(s);
addminute(m);
addhour(h);
}
void main()
{
int i;
Time t;
int h;
int m;
int s;
do
{
cout<<endl<<"Enter 1.Add hours \n2.Add minutes \n3.Add seconds \n4.Add time \n5.Show time \n6.Exit";
cout<<endl<<"Enter your choice";
cin>>i;
switch(i)
{
case 1:
cout<<endl<<"How many hours to add";
cin>>h;
t.addhour(h);
break;
case 2:
cout<<endl<<"How many minutes to add";
cin>>m;
t.addminute(m);
break;
case 3:
cout<<endl<<"How many seconds to add";
cin>>s;
t.addsecond(s);
break;
case 4:
t.addtime();
break;
case 5:
t.displaytime();
break;
case 6:
exit(0);
default:
cout<<endl<<"Please enter a correct choice";
}
}while(i!=6);
}
output
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
1
How many hours to add5
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
2
How many minutes to add25
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
5
Time::05:25:00
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
6
No comments:
Post a Comment