设计一个钟表类Time,具有hour、minute和second三个私有数据成员,具有相应的构造函数和设置时间的函数setTime和显示时间的函数showTime。重载运算符”>”和”<”为成员函数,以实现比较两个钟表对象时间的大小(时间早的定义为小),并编写主函数进行测试。
#include<iostream>
using namespace std;
class Time{
private:
int hour,minute,second;
public:
Time(){hour=minute=second=0;}
void setTime(int h,int m,int s);
void showTime();
Time operator < ( Time &t1);
Time operator > ( Time &t1);
};
void Time::setTime(int h,int m,int s){
hour=h;
minute=m;
second=s;
}
void Time::showTime(){
cout<<"Time : "<<hour<<":"<<minute<<":"<<second<<endl;
}
Time Time::operator < (Time &t1){
Time t2;
if(t2.hour<t1.hour){
return t2;
}else if(t2.hour>t1.hour){
return t1;
}else{
if(t2.minute<t1.minute){
return t2;
}else if(t2.minute>t1.minute){
return t1;
}else{
if(t2.second<t1.second){
return t2;
}else if(t2.second>t1.second){
return t1;
}
}
}
}
Time Time::operator > (Time &t1){
Time t2;
if(t2.hour>t1.hour){
return t2;
}else if(t2.hour<t1.hour){
return t1;
}else{
if(t2.minute>t1.minute){
return t2;
}else if(t2.minute<t1.minute){
return t1;
}else{
if(t2.second>t1.second){
return t2;
}else if(t2.second<t1.second){
return t1;
}
}
}
}
void main()
{
Time t,tt,bigt;
int a,b,c;
cout<<"input h m s:"<<endl;
cin>>a>>b>>c;
t.setTime(a,b,c);
t.showTime();
cout<<"input2 h m s:"<<endl;
cin>>a>>b>>c;
tt.setTime(a,b,c);
tt.showTime();
bigt=t<tt;
bigt.showTime();
}