#include<iostream> #include<iomanip> using namespace std; //运算符重载实例 class Time { private: int hour; int minute; int second; public: Time(); Time(int h, int m, int s); void show(); Time& operator ++();//前置 自加 //前置自加可以做左值,所以要加&,不过自加后做左值让自加变得无意义。 Time operator ++(int);//后置 自加 //后置自加要作为一个值先参与运算,因此要有返回值Time类对象。 }; //此处实现后置自加的重载, Time Time::operator ++(int){ } //------------------------------------------------------- //此处实现前置自加的重载 Time& Time::operator ++(){ } //------------------------------------------------------------- Time::Time() { hour = 0; minute = 0; second = 0; } Time::Time(int h, int m, int s) { hour = h; minute = m; second = s; } void Time::show() { cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl; } int main() { //时钟,分钟,秒钟 int h, m, s; //输入小时 cin >> h; //输入分钟 cin >> m; //输入秒钟 cin >> s; Time t(h, m, s); int i; for(i = 0; i < 5; i++) { t++; t.show(); (++t).show(); } return 0; }补全代码
时间: 2023-06-26 18:07:09 浏览: 114
#include_iostream.h_
4星 · 用户满意度95%
#include>
#include<iomanip>
using namespace std;
class Time {
private:
int hour;
int minute;
int second;
public:
Time();
Time(int h, int m, int s);
void show();
Time& operator ++(); //前置 自加
Time operator ++(int); //后置 自加
};
Time& Time::operator ++(){ //实现前置自加的重载
second++;
if(second >= 60)
{
second = 0;
minute++;
if(minute >= 60)
{
minute = 0;
hour++;
if(hour >= 24)
{
hour = 0;
}
}
}
return *this;
}
Time Time::operator ++(int){ //实现后置自加的重载
Time old = *this;
++(*this);
return old;
}
Time::Time() {
hour = 0;
minute = 0;
second = 0;
}
Time::Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
void Time::show() {
cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl;
}
int main() {
int h, m, s; //时钟,分钟,秒钟
//输入小时
cin >> h;
//输入分钟
cin >> m;
//输入秒钟
cin >> s;
Time t(h, m, s);
int i;
for(i = 0; i < 5; i++)
{
t++;
t.show();
(++t).show();
}
return 0;
}
阅读全文