用類來實現時間輸入輸出 實參聲明 標準類的方法 ...
用類來實現時間輸入輸出
#include <iostream> using namespace std; class Time{ public: int hour; int minute; int sec; }; void set_time(Time&t){ cin>>t.hour; cin>>t.minute; cin>>t.sec; } void show_time(Time&t){ cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } int main(){ Time t1; Time t2; set_time(t1); show_time(t1); set_time(t2); show_time(t2); return 0; }
實參聲明
#include <iostream> using namespace std; class Time{ public: int hour; int minute; int sec; }; void set_time(Time&t,int hour,int minute,int sec){ t.hour=hour; t.minute=minute; t.sec=sec; } void show_time(Time&t){ cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } int main(){ void set_time(Time&,int hour=0,int nimute=0,int sec=0); void show_time(Time&); Time t1; Time t2; set_time(t1,12,23,34); show_time(t1); set_time(t2); show_time(t2); return 0; }
標準類的方法
#include <iostream> using namespace std; class Time{ private: int hour; int minute; int sec; public: void set_time(){ cin>>hour; cin>>minute;m cin>>sec; } void show_time(){ cout<<hour<<":"<<minute<<":"<<sec<<endl; } }; int main(){ Time t1; Time t2; t1.set_time(); t1.show_time(); t2.set_time(); t2.show_time(); return 0; }