题目描述
已有类 Time和Date,要求设计一个派生类Birthtime,它是Time和Date的公有派生类,新增一个数据成员childName用于表示小孩的名字,设计主程序显示一个小孩的名字和出生日期时间。数据通过键盘输入,需要判断输入的年月日时分秒是否有效。Time与Date的成员见教材上习题4.21。
输入描述
Birthtime类对象的数据
输出描述
Birthtime类对象--小孩的姓名及出生日期时间
输入样例
赵无忌
2017 8 35
2017 8 25
25 45 68(顺序:时分秒)
23 45 23
输出样例
日期输入错误!请重新输入数据!(中文标点)
时间输入错误!请重新输入数据!
姓名:赵无忌
出生年月:2017年8月25日
出生时间:23时45分23秒
#include <iostream>
#include <string>
using namespace std;
class Time
{
public:
Time(int h=0, int m = 0, int s = 0)
{
hours = h;
minutes = m;
seconds = s;
};
void display()
{
cout << "出生时间:" << hours << "时" << minutes << "分" << seconds << "秒" << endl;
}
protected:
int hours, minutes, seconds;
};
class Date
{
public:
Date(int m = 0, int d = 0, int y = 0)
{
year = m;
month = d;
day = y;
}
void display()
{
cout << "出生年月:" << year << "年" << month << "月" << day << "日" << endl;
}
protected:
int month,day,year;
};
class Birthtime :public Time, public Date
{
public:
Birthtime(string a=" ", int b = 0, int c = 0, int d = 0, int e = 0, int f = 0, int g = 0):Time(e,f,g),Date(b,c,d)
{
childName = a;
}
void output()
{
cout << "姓名:" << childName <<endl;
Date::display();
Time::display();
}
protected:
string childName;
};
int main()
{
int year, month, day, hours, minutes, seconds;
string childName;
cin >> childName;
here1:
cin >> year >> month >> day;
if (month < 0 || month > 12 || day < 0 || day > 31)
{
cout << "日期输入错误!请重新输入数据!"<<endl;
goto here1;
}
if (month == 2 || month == 4 || month == 6 || month == 9 || month == 11 )
{
if (day > 30)
{
cout << "日期输入错误!请重新输入数据!" << endl;
goto here1;
}
}
if (!(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
{
if(month == 2)
if (day > 28)
{
cout << "日期输入错误!请重新输入数据!" << endl;
goto here1;
}
}
here2:
cin >> hours >> minutes >> seconds;
if (hours < 0 || hours > 24 || minutes < 0 || minutes > 60|| seconds < 0|| seconds > 60)
{
cout << "时间输入错误!请重新输入数据!" << endl;
goto here2;
}
Birthtime A(childName,year, month, day, hours, minutes, seconds);
A.output();
return 0;
}