//page434客户主程序
#include <iostream>
#include "Time.h" using namespace std; void main() { Time time1(5,30,0); Time time2; int i; cout<<"time1: "; time1.Write(); cout<<" time2: "; time2.Write(); cout<<endl; if(time1.Equal(time2)) cout<<"Times are equal"<<endl; else cout<<"Times are NOT equal"<<endl; time2=time1; cout<<"time1: "; time1.Write(); cout<<" time2: "; time2.Write(); cout<<endl; if(time1.Equal(time2)) cout<<"time are equal"<<endl; else cout<<"time are NOT equal"<<endl; time2.Incrment(); cout<<"New time2: "; time2.Write(); cout<<endl; if(time1.LessThan(time2)) cout<<"time1 is less than time2"<<endl; else cout<<"time1 is NOT less than time2"<<endl; if(time2.LessThan(time1)) cout<<"time2 is less than time1"<<endl; else cout<<"time2 is NOT less than time1"<<endl; time1.Set(23,59,55); cout<<"Increment time1 from 23:59:55:"<<endl; for(i=1;i<=10;i++) { time1.Write(); cout<<' '; time1.Incrment(); } cout<<endl; } //time.h
class Time
{ private: int hrs; int mins; int secs; public: void Set(int,int,int); void Incrment(); void Write() const; bool Equal(Time) const; bool LessThan(Time) const; Time(int,int,int); Time(); }; //time.cpp
#include "Time.h"
#include <iostream> using namespace std; void Time::Set(int hours,int minutes,int seconds) { hrs=hours; mins=minutes; secs=seconds; } void Time::Incrment() { secs++; if(secs>59) { secs=0; mins++; if(mins>59) { mins=0; hrs++; if(hrs>23) hrs=0; } } } void Time::Write()const { if(hrs<10) cout<<'0'; cout<<hrs<<':'; if(mins<10) cout<<'0'; cout<<mins<<':'; if(secs<10) cout<<'0'; cout<<secs; } bool Time::Equal(Time otherTime)const { return (hrs==otherTime.hrs&&mins==otherTime.mins&&secs==otherTime.secs); } bool Time::LessThan(Time otherTime)const { return (hrs<otherTime.hrs|| hrs==otherTime.hrs&&mins<otherTime.mins|| hrs==otherTime.hrs&&mins==otherTime.mins&&secs<otherTime.secs ); } Time::Time(int hours,int minutes,int seconds) { hrs=hours; mins=minutes; secs=seconds; } Time::Time() { hrs=0; mins=0; secs=0; } |
|