Create a class Time which contains data members as:Hours,Minutes and Seconds. Write a C++ program to perform following necessary member functions:
i. To read time.
ii. To display time in format like: hh:mm:ss
iii.To add two different times(Use Objects as argument).
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void convert(int,int &,int &,int &);
class Time
{
public:
int hour,minute,second;
Time()
{
hour=0;
minute=0;
second=0;
}
~Time()
{
hour=0;
minute=0;
second=0;
}
Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void get()
{
cout<<"\n\n*********** Enter the Time ***********";
cout<<"\nHour:";
cin>>hour;
cout<<"\nMinute:";
cin>>minute;
cout<<"\nSecond:";
cin>>second;
}
void show()
{
cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute;
cout<<":"<<setw(2)<<setfill('0')<<second<<endl;
}
Time operator +(Time t)
{
int h,m,s;
int totalsec;
totalsec=(hour*3600+minute*60+second)+(t.hour*3600+t.minute*60+t.second);
convert(totalsec,h,m,s);
return Time(h,m,s);
}
void convert(int totalsec,int &h,int &m,int &s)
{
s=totalsec%60;
totalsec=totalsec/60;
m=totalsec%60;
h=(totalsec/60)%12;
if(h==0)
h=12;
return;
}
};
int main()
{
Time time1,time2,added;
clrscr();
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
time1.get();
time2.get();
added=time1+time2;
cout<<"\nTime 1:";
time1.show();
cout<<"\nTime 2:\t";
time2.show();
cout<<"\nAddition:";
time1.show();
cout<<"+";
time2.show();
cout<<"---------------\n";
added.show();
getch();
return 0;
}
i. To read time.
ii. To display time in format like: hh:mm:ss
iii.To add two different times(Use Objects as argument).
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void convert(int,int &,int &,int &);
class Time
{
public:
int hour,minute,second;
Time()
{
hour=0;
minute=0;
second=0;
}
~Time()
{
hour=0;
minute=0;
second=0;
}
Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void get()
{
cout<<"\n\n*********** Enter the Time ***********";
cout<<"\nHour:";
cin>>hour;
cout<<"\nMinute:";
cin>>minute;
cout<<"\nSecond:";
cin>>second;
}
void show()
{
cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute;
cout<<":"<<setw(2)<<setfill('0')<<second<<endl;
}
Time operator +(Time t)
{
int h,m,s;
int totalsec;
totalsec=(hour*3600+minute*60+second)+(t.hour*3600+t.minute*60+t.second);
convert(totalsec,h,m,s);
return Time(h,m,s);
}
void convert(int totalsec,int &h,int &m,int &s)
{
s=totalsec%60;
totalsec=totalsec/60;
m=totalsec%60;
h=(totalsec/60)%12;
if(h==0)
h=12;
return;
}
};
int main()
{
Time time1,time2,added;
clrscr();
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
time1.get();
time2.get();
added=time1+time2;
cout<<"\nTime 1:";
time1.show();
cout<<"\nTime 2:\t";
time2.show();
cout<<"\nAddition:";
time1.show();
cout<<"+";
time2.show();
cout<<"---------------\n";
added.show();
getch();
return 0;
}