Skip to content

Commit

Permalink
Update 重载++的时钟.cpp
Browse files Browse the repository at this point in the history
修复了++的时候判断时使用if(second=60)的bug,增加了后缀形式的++重载,完善了++的返回值。
  • Loading branch information
zhkgo committed Mar 2, 2022
1 parent 696b01a commit 75a7691
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
*/
#include<iostream>
using namespace std;
Expand All @@ -10,19 +10,24 @@ class Time{
minute = m;
second = s;
}
void operator++();
Time operator++();
Time operator++(int);
void showTime(){
cout<<"当前时间为:"<<hour<<":"<<minute<<":"<<second<<endl;
cout<<"当前时间为:"<<hour<<":"<<minute<<":"<<second<<endl;
}

private:
int hour,minute,second;

};

void Time::operator++(){
Time Time::operator++(int n){
Time tmp=*this;
++(*this);
return tmp;
}
Time Time::operator++(){
++second;
if(second=60){
if(second==60){
second=0;
++minute;
if(minute==60){
Expand All @@ -33,12 +38,15 @@ void Time::operator++(){
}
}
}
return *this;
}

int main(int argc, char const *argv[])
{
Time t(23,59,59);
++t;
t.showTime();
(t++).showTime();
t.showTime();
system("pause");
return 0;
Expand Down

0 comments on commit 75a7691

Please sign in to comment.