1. 程式人生 > >【TOJ 5247】C++實驗:時間和日期類

【TOJ 5247】C++實驗:時間和日期類

OS 日期類 一行 ID 表示 pac 日期 style pub

描述

用C++實現日期類CDate和時間類CTime,並在次基礎上利用多繼承實現日期時間類CDateTime,使其能輸出樣例信息。

主函數裏的代碼已經給出,請補充完整,提交時請勿包含已經給出的代碼。

int main()
{
	int y, m, d, hh, mm, ss;
	while(cin>>y>>m>>d>>hh>>mm>>ss)
	{
		CDateTime dt(y,m,d,hh,mm,ss);
		dt.Print();
		((CDate)dt).Print();
		((CTime)dt).Print();
	}
	return 0;
}

輸入

輸入數據有多組,每組占一行,每行為6個正整數,表示一個日期時間中的年、月、日、小時、分鐘、秒。

日期信息保證有效。

輸出

按樣例格式輸出日期時間、日期、時間等信息。

樣例輸入

2000 3 1 12 9 9

樣例輸出

2000-3-1 12:9:9
2000-3-1
12:9:9

#include<iostream>
using namespace std;
class CDate{
    protected:
        int y,m,d;
    public:
        CDate();
        CDate(int y,int m,int d):y(y),m(m),d(d){} 
        
void Print() { cout<<y<<"-"<<m<<"-"<<d<<endl; } }; class CTime{ protected: int hh,mm,ss; public: CTime(); CTime(int hh,int mm,int ss):hh(hh),mm(mm),ss(ss){} void Print() { cout<<hh<<"
:"<<mm<<":"<<ss<<endl; } }; class CDateTime:public CDate,public CTime{ public: CDateTime(); CDateTime(int y,int m,int d,int hh,int mm,int ss):CDate(y,m,d),CTime(hh,mm,ss){} void Print() { cout<<CDate::y<<"-"<<CDate::m<<"-"<<CDate::d<<" "<<CTime::hh<<":"<<CTime::mm<<":"<<CTime::ss<<endl; } }; int main() { int y, m, d, hh, mm, ss; while(cin>>y>>m>>d>>hh>>mm>>ss) { CDateTime dt(y,m,d,hh,mm,ss); dt.Print(); ((CDate)dt).Print(); ((CTime)dt).Print(); } return 0; }

【TOJ 5247】C++實驗:時間和日期類