c++ 继承 + 构造、析构函数

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值