基础上定义构造成员函数
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 class Time 6 { 7 public: 8 Time() 9 { 10 hour=0; 11 minute=0; 12 sec=0; 13 } 14 void set_time(); 15 void show_time(); 16 private: 17 int hour; 18 int minute; 19 int sec; 20 }; 21 22 void Time::set_time() 23 { 24 cin>>hour; 25 cin>>minute; 26 cin>>sec; 27 } 28 29 void Time::show_time() 30 { 31 cout<<hour<<":"<<minute<<":"<<sec<<endl; 32 } 33 34 int main(int argc, char** argv) { 35 Time t1; 36 t1.set_time(); 37 t1.show_time(); 38 Time t2; 39 t2.show_time(); 40 return 0; 41 }