C ++実験04(01)時間、日付クラスとそのサブクラス

タイトルは、
既存のクラスTime andDateを説明します。TimeandDateのパブリック派生クラスである派生クラスBirthtimeを設計する必要があります。新しいデータメンバーchildNameは、子の名前を表すために使用されます。メインプログラムは次のように設計されています。子供の名前と生年月日を表示します。データはキーボードから入力され、入力された年、月、日、時、分、秒が有効かどうかを判断する必要があります。時間と日付のメンバーについては、教科書の質問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;
}

おすすめ

転載: blog.csdn.net/weixin_44179485/article/details/105890574