/* 程序中定义了类A,要求实现对前缀++和后缀++运算符的重载,请在横线处补充程序以使程序完整, 本例输出: 11:11:11 11:11:13 11:11:13 */ #include<iostream> using namespace std; class A{ int hour,minute,second; public: A(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){} void print(){cout<<hour<<":"<<minute<<":"<<second<<endl;} A operator++(); A operator++(int); }; A A::operator++() { second++; if(second>=60){ second-=60; minute++; if(minute>=60){ minute-=60; hour++; if(hour>=24){ hour%=24; } } } return *this; } A A::operator++(int) { A temp=*this; //请选中下方下划线,并补充代码 _____________①______________ return temp; } int main() { A t1(11,11,11),t2,t3; t2=t1++; //请选中下方下划线,并补充代码 _____________②______________ t2.print(); t3.print(); t1.print(); return 0; }
时间: 2024-03-31 19:36:46 浏览: 70
#include<iostream>
using namespace std;
class A{
int hour,minute,second;
public:
A(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
void print(){cout<<hour<<":"<<minute<<":"<<second<<endl;}
A operator++();
A operator++(int);
};
A A::operator++() {
second++;
if(second>=60){
second-=60;
minute++;
if(minute>=60){
minute-=60;
hour++;
if(hour>=24){
hour%=24;
}
}
}
return *this;
}
A A::operator++(int) {
A temp=*this;
hour++;
if(hour>=24){
hour%=24;
}
return temp;
}
int main() {
A t1(11,11,11),t2,t3;
t2=t1++;
t3=++t1;
t2.print();
t3.print();
t1.print();
return 0;
}
阅读全文