问题及代码:
#include <iostream>
using namespace std;
class Date
{
protected:
int Year,Month,Day;
public:
Date():Year(1994),Month(1),Day(1){};
Date(int,int,int);
void SetDate(int,int,int);
void PrintDate();
};
Date::Date(int y,int m,int d):Year(y),Month(m),Day(d){};
void Date::SetDate(int y,int m,int d)
{
Year=y;
Month=m;
Day=d;
}
void Date::PrintDate()
{
cout<<Year<<"年"<<Month<<"月"<<Day<<"日 ";
}
class Time
{
protected:
int Hour,Minute,Second;
public:
Time():Hour(0),Minute(0),Second(0){};
Time(int,int,int);
void SetTime(int,int,int);
void PrintTime();
};
Time::Time(int h,int m,int s):Hour(h),Minute(m),Second(s){};
void Time::SetTime(int h,int m,int s)
{
Hour=h;
Minute=m;
Second=s;
}
void Time::PrintTime()
{
cout<<Hour<<':'<<Minute<<':'<<Second;
}
class TimeDate:public Time,public Date
{
public:
TimeDate():Date(),Time(){};
TimeDate(int y,int M,int d,int h,int m,int s):Date(y,M,d),Time(h,m,s){};
void PrintDate_Time();
};
void TimeDate::PrintDate_Time()
{
PrintDate();
PrintTime();
}
int main()
{
Date d0;
d0.SetDate(2015,5,27);
d0.PrintDate();
Time t0;
t0.SetTime(8,25,0);
t0.PrintTime();
cout<<endl;
cout<<"----------------------------"<<endl;
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;
}
运行结果:
学习小结:
写完了,写的挺快的。
"-----"这一堆减号上面是测试Date和Time类的
写的时候感觉这些类很不完善,因为设置时间没有判断会不会不符合格式
做下一个!