2023.4.23
1 #include <iostream> 2 using namespace std; 3 class Clock 4 { 5 public: 6 Clock(int hour = 0,int minute = 0,int second = 0); 7 void showTime()const; 8 Clock& operator++(); 9 Clock operator++(int); 10 private: 11 int hour,minute,second; 12 }; 13 Clock::Clock(int hour,int minute ,int second ) 14 { 15 if(0<=hour&&hour<24&&0<=minute&&minute<60&&0<=second&&second<60) 16 { 17 this->hour = hour; 18 this->minute = minute; 19 this->second = second; 20 } 21 else 22 cout<<"Time error!"<<endl; 23 } 24 void Clock::showTime() const 25 { 26 cout<<hour<<":"<<minute<<":"<<second<<endl; 27 } 28 Clock&Clock::operator++() 29 { 30 second++; 31 if(second>=60) 32 { 33 second-=60; 34 minute++; 35 if(minute>=60) 36 { 37 minute-=60; 38 hour = (hour+1)%24; 39 } 40 } 41 return *this; 42 } 43 Clock Clock::operator++(int) 44 { 45 Clock old = *this; 46 ++(*this); 47 return old; 48 } 49 int main() 50 { 51 Clock myclock(23,59,59); 52 cout<<"First time output:"; 53 myclock.showTime(); 54 cout<<"show ++myclock:"; 55 (++myclock).showTime(); 56 cout<<"show myclock++:"; 57 (myclock++).showTime(); 58 return 0; 59 }