类的继承说明

代码为

#include <iostream.h>


class animal
{
public:
	void eat()
	{
		cout<<"animal eat"<<endl;
	}
	void sleep()
	{
		cout<<"aninmal sleep"<<endl;
	}
	void breathe()
	{
		cout<<"animal breathe"<<endl;
	}
	//构造函数和析构函数
	animal()
	{
		cout << "anmial construct"<<endl;
	}
	~animal()
	{
		cout<<"animal destruct"<<endl;
	}
};


class fish : public animal
{
public:
	fish()
	{
		cout<<"fish construct"<<endl;
	}
	~fish()
	{
		cout<<"fish destruct"<<endl;
	}
protected:
private:
};

void main()
{
	animal an;
	an.eat();
	fish fh;
	fh.sleep();
}




结果为






从运行的结果可以看出类的继承过程

(1)首先创建一个animal类

(2)创建的过程中构造函数调用了anima();

(3)然后an调用eat()函数

(4)创建一个fish类,fish的构造函数

(5)fish的sleep()函数

(6)释放资源的时候注意

(a)首先执行fish的析构函数

(b)因为fish继承了animal类所以倒数第二个是结束fish类所执行的animal类的析构函数

(c)最后释放创建的animal类 an对象,执行析构函数





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值