C++ 虚函数 多态,以及销毁的时候的问题

一:C++ 虚函数定义

        在其他语言中实现多态的基础就是基类中存在抽象方法,然后子类去实现抽象方法从而实现了多态,但是在C++中实现多态遇到了一些问题,首先咱们先看一下虚函数的定义:

/*
	抽象基类
*/
class Creature {
private:
	string _skill;
public:
	string type;
	string name;
	int age;

	Creature() {
		cout << "a default creature is created.." << name << age << type << endl;
	};

	Creature(const string& t,const string& n,const int a): type(t),name(n),age(a) {
		cout << "creature was created" << endl;
		cout << "type of " << type << " creature was created and it's name is " << name << " age is " << age << endl;
	}

	~Creature() {
		cout << name << " creatrue was deleted" << endl;
	}
	/* 子类必须继承 纯虚函数不能指向子类对象 */
	virtual void eat() = 0;
};

上面的的存虚函数,子类必须实现这个方法

/* 子类必须继承 纯虚函数不能指向子类对象 */
virtual void eat() = 0;

子类中实现:


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值