自定义一个日期时间类DateTimeType,它含有类DateType与类TimeType的类对象作为其数据成员,并具有所列的其他几个成员函数。而后编制主函数,说明DateTimeType的类对象,并对其成员函数以及二对象成员所属类的公有成员函数进行使用。
class DateTimeType { //自定义的日期时间类 DateTimeType
DateType date; //类 DateType 的类对象 date 作为其数据成员
TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员
public:
DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0);
//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值
DateType& GetDate(){ return date; } //返回本类的私有数据对象 data
TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time
void IncrementSecond(int s); //增加若干秒,注意“进位”问题
void PrintDateTime(); //屏幕输出日期时间对象的有关数据
};
注意,每一个DateTimeType类对象中总包含有一个DateType类对象(对象成员)以及一个TimeType类对象(对象成员),编制与实现本程序时,也必须包含DateType与TimeType自定义类(类型)。
之所以设置了公有的类成员函数GetDate与GetTime,是为类外如主函数处使用该类的私有数据成员date与time提供方便(否则的话,类外无法直接访问该类的私有数据成员)。另外,两成员函数返回的都为引用,为的是可将返回对象当作一个独立变量来使用(如可以继续作左值等)。例如,假设编制了如下形式的主函数:
void main() {
DateTimeType dttm1(1999,12,31,23,59,59), dttm2;
(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数
cout<<endl;
dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime
dttm2.PrintDateTime();
dttm1.IncrementSecond(30) ; //调用本派生类成员函数
dttm1.PrintDateTime();
}
样例输出 Copy
1999-12-31 1999-12-31 23:59:59 1-1-1 0:0:0 2000-1-1 0:0:29
代码如下
#include<iostream>
using namespace std;
class DateType
{
public:
int year,month,day;
public:
DateType(int y0=1,int m0=1,int d0=1)
{
year=y0; month=m0; day=d0;
}
void PrintDate()
{
cout<<year<<"-"<<month<<"-"<<day;
}
};
class TimeType
{
public:
int hr,mi,se;
public:
TimeType(int hr0=0, int mi0=0, int se0=0)
{
hr=hr0; mi=mi0; se=se0;
}
void PrintTime()
{
cout<<hr<<":"<<mi<<":"<<se;
}
};
class DateTimeType
{ //自定义的日期时间类 DateTimeType
DateType date; //类 DateType 的类对象 date 作为其数据成员
TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员
public:
DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0);
//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值
DateType& GetDate(){ return date; } //返回本类的私有数据对象 data
TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time
void IncrementSecond(int s); //增加若干秒,注意“进位”问题
void PrintDateTime(); //屏幕输出日期时间对象的有关数据
};
DateTimeType::DateTimeType(int y0, int m0, int d0, int hr0, int mi0, int se0):date(y0,m0,d0),time(hr0,mi0,se0)
{
}//切记有默认值参数的构造函数声明后在外面定义不能再写上默认值否则报错
void DateTimeType::PrintDateTime()
{
DateType d=GetDate();
TimeType t=GetTime();
d.PrintDate(); cout<<" "; t.PrintTime();
}
void DateTimeType::IncrementSecond(int s)
{
time.se+=s;
while(time.se>=60)
{
time.se-=60; time.mi+=1;
}
while(time.mi>=60)
{
time.mi-=60; time.hr+=1;
}
while(time.hr>=24)
{
time.hr-=24; date.day++;
}
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(date.year%4==0&&date.year%100!=0||date.year%400==0) m[2]=29;
while(date.day>m[date.month])
{
date.day-=m[date.month]; date.month++;
}
while(date.month>12)
{
date.month-=12; date.year++;
}
}
int main() {
DateTimeType dttm1(1999,12,31,23,59,59), dttm2;
(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数
cout<<endl;
dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime
cout<<endl;
dttm2.PrintDateTime();
cout<<endl;
dttm1.IncrementSecond(30) ; //调用本派生类成员函数
dttm1.PrintDateTime();
}