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