设计一个派生类Birthtime,继承类Tine和Date,并且增加一个数据成员Childname用于表示小孩名字,另外设计一个函数表示小孩出生时间和名字
时间: 2024-03-23 19:36:10 浏览: 25
好的,根据您的要求,我为您设计了一个派生类 Birthtime,继承了类 Time 和 Date,增加了一个数据成员 Childname,表示小孩的名字,同时还设计了一个函数 ShowBirthtime,用于输出小孩的出生时间和名字,代码如下:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Time {
public:
Time(int h, int m, int s) : hour(h), minute(m), second(s) {}
void ShowTime() const {
cout << hour << ":" << minute << ":" << second << endl;
}
protected:
int hour, minute, second;
};
class Date {
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
void ShowDate() const {
cout << year << "-" << month << "-" << day << endl;
}
protected:
int year, month, day;
};
class Birthtime : public Time, public Date {
public:
Birthtime(int y, int m, int d, int h, int mi, int s, const string& name)
: Time(h, mi, s), Date(y, m, d), Childname(name) {}
void ShowBirthtime() const {
cout << "Childname: " << Childname << endl;
cout << "Birthdate: ";
ShowDate();
cout << "Birthtime: ";
ShowTime();
}
private:
string Childname;
};
int main() {
Birthtime b(2021, 8, 1, 10, 30, 0, "Tom");
b.ShowBirthtime();
return 0;
}
```
在上面的代码中,我们定义了三个类,分别是 Time、Date 和 Birthtime。Time 类表示时间,Date 类表示日期,这两个类都是基类。Birthtime 类继承了 Time 和 Date,增加了一个数据成员 Childname,表示小孩的名字。同时,它还定义了一个函数 ShowBirthtime,用于输出小孩的出生时间和名字。
在 main 函数中,我们创建了一个 Birthtime 对象 b,表示小孩 Tom 的出生时间和名字。然后调用 ShowBirthtime 函数,输出小孩的相关信息。运行程序,输出结果如下:
```
Childname: Tom
Birthdate: 2021-8-1
Birthtime: 10:30:0
```
可以看到,程序成功输出了小孩的出生时间和名字。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)