问题及代码:
/*copyright(c)2016.烟台大学计算机学院 * All rights reserved, * 文件名称:text.Cpp * 作者:吴敬超 * 完成日期:2016年5月11日 * 版本号:codeblock * * 问题描述: 日期时间类 * 输入描述: * 程序输出: 输出结果 */ #include<iostream> #include<cmath> using namespace std; class Time { public: Time(int =0,int m=0,int s=0); ~Time(); void SetTime(int h,int m,int s); void PrintTime(); protected: int hour; int minute; int second; }; Time::Time(int h,int m,int s):hour(h),minute(m),second(s){} Time::~Time(){} void Time::SetTime(int h,int m,int s) { hour=h; minute=m; second=s; } void Time::PrintTime() { cout<<hour<<":"<<minute<<":"<<second; } class Date { public: Date(int y=0,int m=0,int d=0); ~Date(); void SetDate(int y,int m,int d); void PrintDate(); protected: int year; int month; int day; }; Date::Date(int y,int m,int d):year(y),month(m),day(d){} Date::~Date(){} void Date::SetDate(int y,int m,int d) { year=y; month=m; day=d; } void Date::PrintDate() { cout<<year<<"-"<<month<<"-"<<day; } class TimeDate:public Date,public Time { public: TimeDate():Date(),Time() {}; TimeDate(int y,int m1,int d,int h,int m2,int s); ~TimeDate(); void PrintDate_Time(); }; TimeDate::TimeDate(int y,int m1,int d,int h,int m2,int s):Date(y,m1,d),Time(h,m2,s){} TimeDate::~TimeDate(){} void TimeDate::PrintDate_Time() { PrintDate(); cout<<" "; PrintTime(); } int main() { TimeDate dt_a,dt_b(2010,4,16,9,30,0); cout<<"dt_a: "; dt_a.PrintDate_Time(); cout<<endl; cout<<"dt_b: "; dt_b.PrintDate_Time(); dt_a.SetTime(20,00,00); dt_a.SetDate(2008,8,7); cout<<endl; cout<<"dt_after uptate: "; dt_a.PrintDate_Time(); return 0; }
运行结果: