#include<iostream> using namespace std; class clock{ public: clock(int hour=0,int minture=0,int second=0); void showtime() const; clock & operator++(); clock operator++(int); private: int hour,minture,second; }; clock::clock(int hour,int minture,int second){ if(0<=hour&&hour<24&&0<=minture&&minture<60&&0<=second&&second<60){ this->hour=hour; this->minture=minture; this->second=second; } else cout<<"time error"<<endl; } void clock::showtime() const{ cout<<hour<<":"<<minture<<":"<<second<<endl; } clock & clock::operator++(){ second++; if(second>=60){ second-=60; minture++; if(minture>=60){ minture-=60; hour=(hour+1)%24; } } return *this; } clock clock::operator++(int){ clock old=*this; ++(*this); return old; } int main(){ clock myclock(23,59,59); cout<<"first time output:"; myclock.showtime(); cout<<"show myclock++:"; (myclock++).showtime(); cout<<"show ++myclock:"; (++myclock).showtime(); return 0; }
标签:24,hour,clock,int,minture,++,second,2023 From: https://www.cnblogs.com/xuxingkai/p/17351120.html