2023 4 24

#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;
}

 

posted @ 2023-04-24 22:02  徐星凯  阅读(26)  评论(0)    收藏  举报