已有类Time和Date,要求设计一个派生类Birthtime,它继承类Time和Date,并且增加一个数据成员Childname,用于表示小孩的名字,同时设计主程序,输入2个小孩的姓名、出生日期,并将这两个小孩的姓名、出生日期输出。
类定义.png
输入格式:
一共两行,每行为一个孩子的信息。该行分别为:姓名、年、月、日、小时、分钟、秒
输出格式:
小孩的姓名:出生年月,出生时间
输入样例:
在这里给出一组输入。例如:
Mary 2020 1 1 23 12 30
Michael 2019 1 21 14 5 56
输出样例:
在这里给出相应的输出。例如:
Mary: 2020-1-1 23:12:30
Michael: 2019-1-21 14:5:56
#include<iostream>
#include<string>
using namespace std;
class Date {
public:
Date(int M, int d, int y) {
month = M;
day = d;
year = y;
}
virtual void display() {
cout << year << "-" << month << "-" << day<<" ";
}
protected:
int month, day, year;
};
class Time {
public:
Time(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
void display() {
cout << hours << ":"<<minutes << ":" << seconds;
}
protected:
int hours, minutes, seconds;
};
class Birthtime :public Date, public Time {
protected:
string Childname;
public:
Birthtime(int M=0, int d=0, int y=0, int h=0, int m=0, int s=0, string C="a") :Date(M, d, y), Time(h, m, s) {
Childname = C;
}
void setBirthtime(string C, int y, int M, int d, int h, int m, int s) {
Childname = C;
hours = h;
minutes = m;
seconds = s;
month = M;
day = d;
year = y;
}
void display() {
cout << Childname << ":"<<" ";
Date::display();
Time::display();
cout<<"\n";
}
};
int main() {
Birthtime p1,p2;
string C1,C2;
int y1, M1, d1, h1, m1, s1;
int y2, M2, d2, h2, m2, s2;
cin>>C1>> y1>>M1>>d1>> h1>> m1>> s1;
cin >> C2 >> y2 >> M2 >> d2 >> h2 >> m2 >> s2;
p1.setBirthtime(C1, y1, M1, d1, h1, m1, s1);
p2.setBirthtime(C2, y2, M2, d2, h2, m2, s2);
p1.display();
p2.display();
}