Merge pull request #161 from dangwi/master

Fix issue #159
This commit is contained in:
Francis 2022-04-10 16:34:30 +08:00 committed by GitHub
commit c2fbda3137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
//设计一个时间类Time它能够完成秒钟的自增运算。 //设计一个时间类Time它能够完成秒钟的自增运算。
#include<iostream> #include<iostream>
using namespace std; using namespace std;
@ -9,7 +9,7 @@ class Time{
public: public:
Time(int h,int m, int s); Time(int h,int m, int s);
Time operator++(); Time operator++();
//友元重载需要参数 //友元重载需要参数
friend Time operator--(Time &t); friend Time operator--(Time &t);
void display(); void display();
}; };
@ -42,14 +42,14 @@ Time Time::operator++(){
} }
Time operator--(Time &t){ Time operator--(Time &t){
--t.second; --t.second;
if (t.second>=60){ if (t.second<0){
t.second=0; t.second=59;
++t.minute; --t.minute;
if(t.minute>=60){ if(t.minute<0){
t.minute=0; t.minute=59;
++t.hour; --t.hour;
if(t.hour>=24) if(t.hour<0)
t.hour=0; t.hour=23;
} }
} }
return t; return t;
@ -61,9 +61,9 @@ int main(int argc, char const *argv[])
{ {
Time t1(23,59,59); Time t1(23,59,59);
t1.display(); t1.display();
++t1; //隐式调用 ++t1; //隐式调用
t1.display(); t1.display();
t1.operator++(); //显式调用 t1.operator++(); //显式调用
t1.display(); t1.display();
Time t2(24,60,60); Time t2(24,60,60);
t2.display(); t2.display();