虚函数与多态性


#include "stdafx.h"
#include <iostream>
using namespace std;
class animal
{
public:
	void eat(){ cout << "animal eat" << endl; }
	void sleep(){ cout << "animal sleep" << endl; }
	void breathe(){ cout << "animal breathe" << endl; }
};

class fish :public animal
{
public:
	void breathe(){ cout << "fish bubble" << endl; }
};

void fn(animal *pAn)
{
	pAn->breathe();
}
int _tmain(int argc, _TCHAR* argv[])
{
	animal *pAn;
	fish fh;
	pAn = &fh;
	fn(pAn);
	return 0;
}
输出结果:</span><pre name="code" class="cpp"><span style="font-size:18px;">animal breathe</span>
分析:因为在fish类的对象fh的地址赋给pAn时,C++编译器进行了类型转换,此时C++编译器认为变量pAn保存就是Animal
对象的地址。当在fn函数中执行pAn->breathe()时,调用的当然就是animal对象的breathe的函数。
下图为fish对象内存模型:

当我们构造fish类的对象时,首先要调用animal类的构造函数去构造animal类的对象,然后才调用fish类的构造函数完成
自身部分的构造,从而拼接出一个完整的fish对象。当我们将fish类的对象转换为animal类型时,该对象就被认为是原对
象整个内存模型的上半部分。即“animal的对象所占内存”。当我们利用类型转换后的对象指针去调用它的方法时,自然
也就是调用它所在的内存中的方法。
 

现在我们在animal类的breathe()方法上加上virtual关键字。

#include "stdafx.h"
#include <iostream>
using namespace std;
class animal
{
public:
	void eat(){ cout << "animal eat" << endl; }
	void sleep(){ cout << "animal sleep" << endl; }
	virtual void breathe(){ cout << "animal breathe" << endl; }//添加virtual关键字,成为此函数为虚函数
};

class fish :public animal
{
public:
	void breathe(){ cout << "fish bubble" << endl; }
};

 void fn(animal *pAn)
{
	pAn->breathe();
}
int _tmain(int argc, _TCHAR* argv[])
{
	animal *pAn;
	fish fh;
	pAn = &fh;
	fn(pAn);
	return 0;
}

输出结果:
fish bubble
分析:这就是C++的多态性。在C++编译器在编译的时候,发现animal类的breathe()函数是虚函数,此时C++就会采用
迟绑定技术。也就是编译时并不确定具体调用的函数,而是在运行时,根据对象的类型类确认调用是哪一个函数,这种
能力就叫做C++的多态性。


C++的多态性的概括:在基类的函数上加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型
来调用相应的函数。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值