首页 > 试题广场 >

定义一个时间类Time,能提供和设置由时、分、秒组成的时间,

[问答题]

定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序, 要求包括定义时间对象,设置时间,输出该对象提供的时间。并请将类定义作为界面,用 多文件结构实现之。

推荐
Time.h对时间类体进行定义。类的定义中如需要一些相关的头文件支持,则要一并添上。时间代码如下:
//===================================
//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中,其代码如下:
//===================================
//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";
}//==================================

发表于 2018-04-18 20:35:04 回复(4)
没用
发表于 2020-11-10 18:00:59 回复(0)