Update 重载++的时钟.cpp
修复了++的时候判断时使用if(second=60)的bug,增加了后缀形式的++重载,完善了++的返回值。
This commit is contained in:
parent
696b01a42c
commit
75a7691610
@ -1,5 +1,5 @@
|
||||
/*
|
||||
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
|
||||
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
|
||||
*/
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
@ -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){
|
||||
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user