Time、 Date类及其子类

来源:互联网 发布:iphone铃声制作软件 编辑:程序博客网 时间:2024/06/05 10:46

已有类 Time和Date,要求设计一个派生类Birthtime,它是Time和Date的公有派生类,新增一个数据成员childName用于表示小孩的名字,设计主程序显示一个小孩的名字和出生日期时间。数据通过键盘输入,需要判断输入的年月日时分秒是否有效。Time与Date的成员见教材上习题4.21。

#include<iostream>#include<string>using namespace std;class Time{protected:    int hours, minutes, seconds;public:    Time(int h, int mi, int s)    {        hours = h;        minutes = mi;        seconds = s;    }    void display()    {        cout << "出生时间:" << hours << "时" << minutes << "分";        cout<<seconds << "秒" << endl;    }};class Date{protected:    int month, day, year;public:    Date(int y, int mo, int d)    {        month = mo;        day = d;        year = y;    }    void display()    {        cout << "出生年月:" << year << "年" << month << "月";        cout<< day << "日" << endl;    }};class Birthtime :public Date,public Time{    string Childname;public:    void printname()    {        cout << "姓名:" << Childname << endl;    }    Birthtime(string na,int y, int mo, int d, int h, int mi, int s) :Time(h, mi, s),Date(y,mo,d){        Childname = na;    }};int main()            {    int y, mo, d, h, mi, s,i=0;    string na;cin >> na;    for (; i == 0;)    {        cin >> y >> mo >> d;        if (mo > 12 || mo < 1)        {            cout << "日期输入错误!请重新输入数据!\n"; continue;        }        if (d > 31 || d < 1)        {            cout << "日期输入错误!请重新输入数据!\n"; continue;        }        if (mo == 4 || mo == 6 || mo == 9 || mo == 11)        {            if (d > 30)            {                cout << "日期输入错误!请重新输入数据!\n"; continue;            }        }        if (mo == 2)        {            if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))            {                if (d > 29)                {                    cout << "日期输入错误!请重新输入数据!\n"; continue;                }            }            else            {                if (d > 28)                {                    cout << "日期输入错误!请重新输入数据!\n"; continue;                }            }        }        i = 1;    }    i = 0;    for (; i == 0;)    {        cin >> h >> mi >> s;        if (h >= 24 || mi >= 60 || s >= 60)        {            cout << "时间输入错误!请重新输入数据!\n"; continue;        }        if (h < 0 || mi < 0 || s < 0)        {            cout << "时间输入错误!请重新输入数据!\n"; continue;        }        i = 1;    }    Birthtime A(na,y, mo, d, h, mi, s);    A.printname();    A.Date::display();    A.Time::display();    system("pause");> return 0;}
原创粉丝点击