#include < iostream >
using namespace std;
class Clock//时钟类定义
{
public:
Clock(int hour = 0, int minute = 0, int second = 0);
void showTime() const;
Clock & operator ++ ();//前置单目运算符重载
Clock operator ++ (int);//后置单目运算符重载,其中int用于区分前置和后置
private:
int hour, minute, second;
};
Clock::Clock(int hour, int minute, int second)
{
if(hour >= 0 && hour <= 24 && minute >= 0 && minute<= 60 && second >= 0 && second <= 60)
{
this->hour = hour;
this->minute = minute;
this->second = second;
}
else
{
cout << “Time error” << endl;
}
}
void Clock::showTime() const
{
cout << hour << “:” << minute << “:” << second <<
C/C++程序语言设计——重载前置++和后置++为时钟类成员函数
![](https://img-home.csdnimg.cn/images/20240711042549.png)