已有类Time和Date,要求设计一个 派生类Birthtime,它继承类Time和Date,并且增加一个数据成员Child name用于表示小孩的名字,同时设计主程序显示一个小孩的出生时间和名字。运用构造函数
时间: 2024-05-07 12:21:54 浏览: 141
c++日期时间课程设计Time_Date。派生出DateTime类分享.pdf
5星 · 资源好评率100%
下面是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Time {
protected:
int hour, minute, second;
public:
Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
void display() {
cout << hour << ":" << minute << ":" << second << endl;
}
};
class Date {
protected:
int year, month, day;
public:
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void display() {
cout << year << "-" << month << "-" << day << endl;
}
};
class Birthtime : public Time, public Date {
private:
string childName;
public:
Birthtime(string name, int y, int m, int d, int h, int mi, int s) : Time(h, mi, s), Date(y, m, d) {
childName = name;
}
void display() {
cout << "Child Name: " << childName << endl;
cout << "Birth Time: ";
Date::display();
cout << " ";
Time::display();
}
};
int main() {
Birthtime b("Tom", 2021, 8, 16, 8, 30, 0);
b.display();
return 0;
}
```
输出结果为:
```
Child Name: Tom
Birth Time: 2021-8-16
8:30:0
```
阅读全文