Experimento C ++ 04 (01) Hora, clase Fecha y sus subclases

El título describe
las clases existentes Hora y fecha. Es necesario diseñar una clase derivada Hora de nacimiento, que es una clase pública derivada de Hora y fecha. Se utiliza un nuevo miembro de datos childName para representar el nombre del niño. El programa principal está diseñado para mostrar el nombre y la fecha de nacimiento del niño. Los datos se ingresan a través del teclado y es necesario determinar si el año, mes, día, hora, minuto y segundo ingresados ​​son válidos. Consulte la pregunta 4.21 del libro de texto para los miembros de Time and Date.
Ingrese
los datos que
describen el
objeto de clase Birthtime Genere la descripción del objeto de clase Birthtime: el nombre y la fecha de nacimiento del niño
Ejemplo de entrada
Zhao Wuji
2017 8 35
2017 8 25
25 45 68 (Orden: hora, minuto, segundo)
23 45 23
Salida ejemplo
¡Error de entrada de fecha! ¡Vuelva a ingresar los datos! (Puntuación china) ¡La
entrada de tiempo es incorrecta! ¡Vuelva a ingresar los datos!
Nombre: Zhao Wuji
Fecha de nacimiento: 25 de agosto de 2017
Hora de nacimiento: 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;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_44179485/article/details/105890574
Recomendado
Clasificación