问题遇到的现象和发生背景
(time++).show();和前面的++time;time. show();有什么区别吗
把(time++).show();改成time++;time. show();结果就变了
问题相关代码,请勿粘贴截图
#include<iostream>
using namespace std;
class Time {
int hour;
int minute;
int second;
public:
Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s) {
cout<<"构造函数!"<<endl;
}
Time operator++();
Time operator++(int);
void show() {
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
~Time() {
cout<<"析构函数!"<<endl;
}
};
Time Time::operator ++() {
second++;
if(second>=60) {
second-=60;
minute++;
if(minute>=60) {
{
minute-=60;
hour++;
}
if(hour>=24) {
hour=0;
}
}
}
return *this;
}
Time Time::operator++(int) {
Time old=*this;
second++;
if(second>=60) {
second-=60;
minute++;
if(minute>=60) {
{
minute-=60;
hour++;
}
if(hour>=24) {
hour=0;
}
}
}
return old;
}
int main() {
Time time(11,59,58);
int i;
for(i=0; i<3; i++) {
++time;
time.show();
}
for(i=0; i<3; i++) {
(time++).show();
}
time.show();
return 0;
}