c++类训练
# include <iostream> # include <string> # include <iomanip> using namespace std; class Time { private: int hour; int minute; int second; public: void set_time() { cin>>hour>>minute>>second; } void show_time() { if(hour<=0 || hour >12) hour=12; if(minute<0 || minute >60) minute=0; if(second<0 || second>60) second=0; cout<<setw(2)<<setfill('0')<<hour<<':'; //setw设置域宽,setfill是指定你用什么字符填充。 cout<<setw(2)<<setfill('0')<<minute<<':'; cout<<setw(2)<<setfill('0')<<second<<endl; // cout<<hour<<':'<<minute<<':'<<second<<endl; } }; int main() { Time t1; t1.set_time(); t1.show_time(); return 0; }