#include<iostream>
#include<string>
using namespace std;
//class Date; //提前申明类
//class Time; //提前申明类
class Time
{
public:
Time(){hour=0;min=0;sec=0;} //默认赋初始值
Time(int h,int m,int s):hour(h),min(m),sec(s){} //构造函数赋初始值
void display(Time &);
~Time(){} //析构函数
protected:
int hour;
int min;
int sec;
};
void Time::display(Time &T)
{
cout<<"The time is:\n";
cout<<T.hour<<":"<<T.min<<":"<<T.sec<<endl;
}
class Date:protected Time
{
public:
Date(int y,int m,int d,int h,int min,int s):year(y),month(m),date(d),Time(h,min,s){}
void display(Date &);
~Date(){} //析构函数
private:
int year;
int month;
int date;
};
void Date::display(Date &d)
{
cout<<"The date is :\n";
cout<<d.year<<"/"<<d.month<<"/"<<d.date<<endl;
Time::display(d); //调用父类的函数成员
}
int main()
{
Date d1(2012,10,8,20,56,30);
d1.display(d1);
return 0;
}
c++ 继承 + 构造、析构函数
最新推荐文章于 2023-11-06 13:20:51 发布