C++實驗:時間和日期類


描述

 

用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 CTime,public CDate
{
    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;
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM