设有类CTime和CDate分别用于描述时间和日期,另外有CDateTime类描日期和时间,请为三个类给出具体的实现代码,并在main函数中测试。#include#includeusingnamespace...
设有类CTime 和CDate分别用
于描述时间和日期,另外有CDateTime类描日期和时间,请为三个类给出具体的实
现代码,并在 main函数中测试。
#include
#include
using namespace std;
class CTime
{
int m_hour,m_mintue,m_second;
public:
CTime(int hour=0,int minute=0,int second=0);
//如果时间合法,则赋值,否则赋值0时0 分 0秒
int SetTime(int hour=0,int minute=0,int second=0);
//如果时间合法,则赋值并返回1,否则不赋值,并返回0
int getHour();
int getMinute();
int getSecond();
void ShowTime(bool flag);
//flag为True则以24小时制显示时间,否则以 AM PM的方式显示
//自己考虑是否需要添加其他成员函数
};
class CDate
{
int m_year,m_month,m_day;
public:
CDate(int year=2000,int month=1,int day=1);
//如果日期合法,则赋值,否则赋值2000年1 月1日
int SetDate(int year=2000,int month=1,int day=1);
//如果日期合法,则赋值并返回1,否则不赋值,并返回0
int GetYear();
int GetMonth();
int GetDay();
void ShowDate(bool flag);
//flag 为 TRUE,则以中文的方式显示日期,否则以 MM/DD/YYYY的方式显示
日期
//自己考虑是否需要添加其他成员函数
};
class CDateTime
{
CTime m_time;
CDate m_date;
public:
//添加必要的构造函数
//int SetDateTime(...); 自己设计参数,考虑该函数的返回值加以表示全部正确、
日期不对、时间不对等情况
//void ShowDateTime(...); 自己设计参数,考虑可以中文日期和西文日期格式,
以及24小时和AM PM都可以控制
//添加自己认为必要的其他成员函数
};
在主函数中验证上述类的功能,要求的主函数如下所示,不允许进行修改:
void main()
{
CDateTime d1(2014, 5, 2, 27, 12, 5);
d1.ShowDateTime(false, true); //第1个参数表示以英文方式显示日期, 第2个
参数表示以24小时制方式显示时间
CDateTime d2;
d2.ShowDateTime(true, false); //第1个参数表示以中文方式显示日期, 第2个
参数表示以AM PM的方式显示时间
int iStatus;
iStatus=d2.SetDateTime(2014, 5, 2, 21, 55, 5);
switch(iStatus)
{
case 1:
cout<
break;
case -1:
cout<
break;
case -2:
cout<
break;
}
d2.ShowDateTime(false, false); //第1个参数表示以英文方式显示日期, 第2个
参数表示以AM PM的方式显示时间
cout<
cout<
}
展开