设计三个类Time、Date、Birthtime,
其中,Time类能够记录setTime(…)和显示时分show();
Date类能够记录setDate(…)和显示年月日show();
类Birthtime派生自Time和Date,并增加一个数据成员Childname用于表示小孩的名字,自行设计合理的成员函数。
同时设计主程序显示一个小孩的出生时间和名字。
设计三个类Time、Date、Birthtime,
其中,Time类能够记录setTime(…)和显示时分show();
Date类能够记录setDate(…)和显示年月日show();
类Birthtime派生自Time和Date,并增加一个数据成员Childname用于表示小孩的名字,自行设计合理的成员函数。
同时设计主程序显示一个小孩的出生时间和名字。
代码如下:
#include<iostream>
using namespace std;
class Time
{
public:
int hour, minute, second;
public:
void setTime(int h, int m, int s) { hour = h; minute = m; second = s; }
void show() { cout << hour << ":" << minute << ":" << second; }
};
class Date
{
public:
int year, month, day;
public:
void setDate(int y, int m, int d) { year = y; month = m; day = d; }
void show() { cout << year << "-" << month << "-" << day; }
};
class Birthtime :public Time, public Date
{
public:
char Childname[30];
public:
void setChildname(const char* name)
{
strcpy(Childname, name);
}
void show()
{
cout << Childname << " ";
Date::show();
cout << " ";
Time::show();
}
};
int main()
{
Birthtime b;
b.setChildname("ZhangSan");
b.setDate(2012, 4, 20);
b.setTime(10, 8, 9);
b.show();
return 0;
}