设计三个类Time、Date、Birthtime,
其中,Time类能够记录setTime(…)和显示时分show();
Date类能够记录setDate(…)和显示年月日show();
类Birthtime派生自Time和Date,并增加一个数据成员Childname用于表示小孩的名字,自行设计合理的成员函数。
同时设计主程序显示一个小孩的出生时间和名字。
代码如下:
#include<iostream>
using namespace std;
class Time
{
public:
int hour, minute, second;
public:
void setTime(int h, int m, int s) { hour = h; minute = m; second = s; }
void show() { cout << hour << ":" << minute << ":" << second; }
};
class Date
{
public:
int year, month, day;
public:
void setDate(int y, int m, int d) { year = y; month = m; day = d; }
void show() { cout << year << "-" << month << "-" << day; }
};
class Birthtime :public Time, public Date
{
public:
char Childname[30];
public:
void setChildname(const char* name)
{
strcpy(Childname, name);
}
void show()
{
cout << Childname << " ";
Date::show();
cout << " ";
Time::show();
}
};
int main()
{
Birthtime b;
b.setChildname("ZhangSan");
b.setDate(2012, 4, 20);
b.setTime(10, 8, 9);
b.show();
return 0;
}
代码如下:有帮助的话采纳一下哦!
#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;
}