#include<iostream>
#include<string>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0); //构造函数
Time operator+(Time&); //运算符重载函数
void disptime(string); //输出时间函数
private:
int hours; //小时
int minutes; //分钟
int seconds; //秒钟
};
Time::Time(int h,int m,int s)
{ hours=h;
if ( h<0)
{ cout<<"时钟数小于0,请修正"<<endl;
exit(0);
}
minutes=m;
if ( m>=60||m<0)
{ cout<<"分钟数小于0或大于等于60,请修正"<<endl;
exit(0);
}
seconds=s;
if (seconds>=60||seconds<0)
{ cout<<"秒钟数小于0或大于等于60,请修正"<<endl;
exit(0);
}
}
Time Time::operator+(Time& time)
{ int h,m,s;
s=time.seconds+seconds;
m=time.minutes+minutes+s/60;
h=time.hours+hours+m/60;
Time sum_time(h,m%60,s%60);
return sum_time;
}
void Time::disptime(string str)
{ cout<<str;
cout<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
int main()
{ Time t1(2,45,40),t2(21,28,34),t3;
t1.disptime("时间1是:");
t2.disptime("时间2是:");
t3=t1+t2;
t3.disptime("两个时间之和是:");
return 0;
}
7.11
最新推荐文章于 2024-08-12 15:35:54 发布