#include <iostream>
using namespace std;
class Date
{
public:
Date(int y,int m,int d):Year(y),Month(m),Day(d) {}
void SetDate(int y,int m,int d);
void PrintDate();
protected:
int Year;
int Month;
int Day;
};
class Time
{
public:
Time(int h,int m,int s):Hour(h),Minute(m),Second(s) {}
void SetTime(int h,int m,int s);
void PrintTime();
protected:
int Hour;
int Minute;
int Second;
};
class DateTime:public Date,public Time
{
public:
DateTime(int year=0,int month=0,int day=0,int hour=0,int minute=0,int second=0):Date(year,month,day),Time(hour,minute,second) {}
void PrintDate_Time();
};
void Date::SetDate(int y,int m,int d)
{
Year=y;
Month=m;
Day=d;
}
void Date::PrintDate()
{
cout<<"当前日期为:"<<Year<<"年 "<<Month<<"月 "<<Day<<"日"<<endl;
}
void Time::SetTime(int h,int m,int s)
{
Hour=h;
Minute=m;
Second=s;
}
void Time::PrintTime()
{
cout<<"当前时间为:"<<Hour<<"时 "<<Minute<<"分 "<<Second<<"秒"<<endl;
}
void DateTime::PrintDate_Time()
{
PrintDate();
PrintTime();
}
int main()
{
DateTime 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;
}
12.3派生类 时间日期
最新推荐文章于 2023-05-02 21:53:05 发布