定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序, 要求包括定义时间对象,设置时间,输出该对象提供的时间。并请将类定义作为界面,用 多文件结构实现之。
//=================================== //Time.h //=================================== #include<iostream> //----------------------------------- class Time{ int hour, minute, second; public: void Time::set(int h, int m, int s); frinend std::ostream& operator<<(std::ostream& out, const Time& t); };//---------------------------------
//=================================== //Time.cpp //=================================== #include"Time.h" #include<iostream> #include<iomanip> using namespace std; //----------------------------------- void Time::set(int h, int m, int s){ hour=h, minute=m, second=s; }//---------------------------------- ostream& operator<<(ostream& out, const Time& t){ return out<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute<<":"<<setw(2)<<t.second<<setfill(' '); }//----------------------------------
//=================================== //EX0804.cpp //使用时间类 //=================================== #include"time.h" #include<iostream> using namespace std; //----------------------------------- int main() { Time t; t.set(5,15,32); cout<<t<<"\n"; }//==================================
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题