Lafore Chap8 q6
Lafore Chap8 q6
Lafore Chap8 q6
two time values using the overloaded (-) operator, and to multiply a time value
by a number, of type float, using the overloaded (*) operator.*/
#include<iostream>
#include<cmath>
using namespace std;
class Time
{
private:
int h,m,s;
public:
Time():h(0),m(0),s(0){}
Time(int hh,int mm,int ss);
~Time(){}
int Timetosec()
{return h*3600+m*60+s;}
void sectoTime(int sec);
void Tadjust();
void display();
};
int main()
{
Time t,t1(12,19,59),t2(06,58,59);
t=t1+t2;
cout<<"\nAfter t=t1+t2;";
cout<<"\nt1: ";t1.display();
cout<<"\nt2: ";t2.display();
cout<<"\nt: ";t.display();
t=t1-t2;
cout<<"\nAfter t=t1-t2(actually t=t1~t2);";
cout<<"\nt1: ";t1.display();
cout<<"\nt2: ";t2.display();
cout<<"\nt: ";t.display();
++t1;
cout<<"\n\nt1(after ++t1;)= ";t1.display();
t1++;
cout<<"\nt1(after t1++;)= ";t1.display();
--t1;
cout<<"\nt1(after --t1;)= ";t1.display();
t1--;
cout<<"\nt1(after t1--;)= ";t1.display();
Time tm=t;
float f=1.5;
cout<<"\n\ntm(before tm=tm*f;)= ";tm.display();
tm=tm*f;
cout<<"\ntm(after tm=tm*f;f=1.5)= ";tm.display();
cout<<endl;
return 0;
}
void Time::Tadjust()
{
if(s>59||m>59||s<0||m<0)
sectoTime(Timetosec());
}
void Time::display()
{
Tadjust();
(h>=0 && h<10)?cout<<0<<h:cout<<h;
cout<<':';
(m>=0 && m<10)?cout<<0<<m:cout<<m;
cout<<':';
(s>=0 && s<10)?cout<<0<<s:cout<<s;
}