IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 21天学通C++——多态专题 -> 正文阅读

[C++知识库]21天学通C++——多态专题

11.1 多态基础

11.1.1 为何需要多态行为

#include <iostream>
using namespace std;

class Fish
{
public:
	void Swim()
	{
		cout << "Fish swims! " << endl;
	}
};

class Tuna :public Fish
{
public:
	// override Fish :: Swim
	void Swim()
	{
		cout << "Tuna swims! " << endl;
	}
};

void MakeFishSwim(Fish& inputFish)
{
	// calling Fish :: Swim
	inputFish.Swim();
}
int main()
{
	Tuna myDinner;
	// calling Tuna: :Swim
	myDinner.Swim();
	// sending Tuna as Fish
	MakeFishSwim(myDinner);
	return 0;
}

结果如下:
Tuna swims!
Fish swims!
理想情况下,用户希望Tuna对象表现出金枪鱼的行为,即便通过外部函数的Fish参数调用Swim()时亦如此。因此,在MakeFishSwim(Fish& inputFish)函数中调用inputFish.Swim()时,用户希望执行的是Tuna::Swim()。要实现这种多态行为,可将Fish::Swim()声明为虚函数。

11.1.2 使用虚函数实现多态行为

调用语法:

class Base
{
virtual ReturnType FunctionName (Parameter List);
};
class Derived
{
ReturnType FunctionName (Parameter List);
};

通过使用关键字 virtual,可确保编译器调用覆盖版本。也就是说,如果 Swim( )被声明为虚函数,则将参数 myFish(其类型为 Fish&)设置为一个 Tuna 对象时, myFish.Swim( )将执行 Tuna::Swim( )。
示例如下:

#include <iostream>
using namespace std;

class Fish
{
public:
	virtual void Swim()  //加入virtual关键字,声明为虚函数
	{
		cout << "Fish swims! " << endl;
	}
};

class Tuna :public Fish
{
public:
	// override Fish :: Swim
	void Swim()
	{
		cout << "Tuna swims! " << endl;
	}
};

void MakeFishSwim(Fish& inputFish)
{
	// calling Fish :: Swim
	inputFish.Swim();
}
int main()
{
	Tuna myDinner;
	// calling Tuna: :Swim
	myDinner.Swim();
	// sending Tuna as Fish
	MakeFishSwim(myDinner);
	return 0;
}

总结:多态就是将派生类对象视为基类对象,并执行派生类的 Swim( )实现。

11.1.3 为何需要构造虚函数

对于使用new在自由存储区中实例化的派生类对象,如果将其赋给基类指针,并通过该指针调用delete,将不会调用派生类的析构函数。这可能导致资源未释放、内存泄露等问题。
示例如下:

#include <iostream>
using namespace std;
class Fish
{
public:
	Fish()
	{
		cout << "Constructed Fish" << endl;
	}
	~Fish()
	{
		cout << "Destroyed Fish" << endl;
	}
};
class Tuna :public Fish
{
public:
	Tuna()
	{
		cout << "Constructed Tuna" << endl;
	}
	~Tuna()
	{
		cout << "Destroyed Tuna" << endl;
	}
};
void DeleteFishMemory(Fish* pFish)
{
	delete pFish;
}

int main()
{
	cout << "Allocating a Tuna on the free store:" << endl;
	Tuna* pTuna = new Tuna;
	cout << "Deleting the Tuna: "<< endl ;
	DeleteFishMemory(pTuna);
	cout <<" Instantiating a Tuna on the stack:" << endl;
	Tuna myDinner;
	cout << "Automatic destruction as it goes out of scope: " << endl;
	return 0;
}

结果如下:
Allocating a Tuna on the free store:
Constructed Fish
Constructed Tuna
Deleting the Tuna:
Destroyed Fish //仅仅释放了基类对象
Instantiating a Tuna on the stack:
Constructed Fish
Constructed Tuna
Automatic destruction as it goes out of scope:
Destroyed Tuna
Destroyed Fish

要避免上述问题,可将基类中的析构函数声明为虚函数:

#include <iostream>
using namespace std;
class Fish
{
public:
	Fish()
	{
		cout << "Constructed Fish" << endl;
	}
	virtual ~Fish()
	{
		cout << "Destroyed Fish" << endl;
	}
};
class Tuna :public Fish
{
public:
	Tuna()
	{
		cout << "Constructed Tuna" << endl;
	}
	~Tuna()
	{
		cout << "Destroyed Tuna" << endl;
	}
};
void DeleteFishMemory(Fish* pFish)
{
	delete pFish;
}

int main()
{
	cout << "Allocating a Tuna on the free store:" << endl;
	Tuna* pTuna = new Tuna;
	cout << "Deleting the Tuna: "<< endl ;
	DeleteFishMemory(pTuna);
	cout <<" Instantiating a Tuna on the stack:" << endl;
	Tuna myDinner;
	cout << "Automatic destruction as it goes out of scope: " << endl;
	return 0;
}

结果如下:
Allocating a Tuna on the free store:
Constructed Fish
Constructed Tuna
Deleting the Tuna:
Destroyed Tuna
Destroyed Fish
Instantiating a Tuna on the stack:
Constructed Fish
Constructed Tuna
Automatic destruction as it goes out of scope:
Destroyed Tuna
Destroyed Fish

11.1.4 虚函数的工作原理——理解虚函数表

在编译时,编译器将为Base和Derived类都将有自己的虚函数表(VFT)。实例化这些类的对象时,将创建一个隐藏的指针(VFT*),它指向相应的VFT,可将VFT视为一个包含函数指针的静态数组,其中每个指针都指向相应的虚函数实现。
在这里插入图片描述
如上图所示,Derived派生类中除了Func2()外,其它虚函数都有Derived类的本地虚函数实现,Derived 没有覆盖Base::Func2( ),因此相应的函数指针指向 Base 类的 Func2( )实现。

使用sizeof证明虚函数表指针存在

#include <iostream>
using namespace std;
class SimpleClass
{
	int a, b;
public:
	void DoSomething() { }
};
class Base
{
	int a, b;
public:
	virtual void DoSomething() { }
};
int main()
{
	cout << "sizeof (SimpleClass) = " << sizeof(SimpleClass) << endl;
	cout << "sizeof (Base) ="<< sizeof (Base) << endl ;
	return 0;
}

结果如下:
sizeof (SimpleClass) = 8
sizeof (Base) =16
添加关键字virtual带来的影响是,编译器将为Base类生成一个虚函数表,并为其虚函数表指针预留空间。

11.1.5 抽象基类和纯虚函数

抽象基类:不能实例化的基类,这样的基类只有一个用途,那就是从它派生出其他类。要创建抽象基类,可声明纯虚函数。
语法如下:

class AbstractBase
{
public:
	virtual void DoSomething() = 0;
};

改声明告诉编译器,AbstractBase的派生类必须实现方法DoSomething()
示例代码:

#include <iostream>
using namespace std;
class Fish
{
public:
	// define a pure virtual function Swim
	virtual void Swim() = 0;
};
class Tuna :public Fish
{
public:
	void Swim()
	{
		cout << "Tuna swims fast in the sea! " << endl;
	}
};

class Carp :public Fish
{
public:
	void Swim()
	{
		cout << "Carp swims slow in the lake! " << endl;
	}
};

void MakeFishSwim(Fish& inputFish) {
	inputFish.Swim();
}

int main()
{
	// Fish myFish; // Fails, cannot instantiate an ABC
	Carp myLunch;
	Tuna myDinner;
	MakeFishSwim(myLunch);
	MakeFishSwim(myDinner);
	return 0;
}

结果如下:
Carp swims slow in the lake!
Tuna swims fast in the sea!
编译器不允许创建抽象基类(ABC)Fish的实例,但是抽象基类可以作为指针或者引用的类型输入函数。

11.2 使用虚继承解决菱形问题

按照如下关系图实例化一个鸭嘴兽的类:
在这里插入图片描述
示例代码如下:

#include <iostream>
using namespace std;
class Animal
{
public:
	Animal()
	{
		cout << "Animal constructor" << endl;
	}
	// sample member
	int age;
};
class Mammal :public Animal
{
};
class Bird :public Animal
{
};
class Reptile :public Animal
{
};
class Platypus:public Mammal, public Bird, public Reptile
{
public:
	Platypus()
	{
		cout << "Platypus constructor" << endl;
	}
};

int main()
{
	Platypus duckBilledP;
	return 0;
}

结果如下:
Animal constructor
Animal constructor
Animal constructor
Platypus constructor
由于采用了多继承,且鸭嘴兽的全部三个基类都是从Animal类派生而来的,因此自动创建了3个Animal实例。
下面采用虚继承方式解决这个问题:

#include <iostream>
using namespace std;
class Animal
{
public:
	Animal()
	{
		cout << "Animal constructor" << endl;
	}
	// sample member
	int age;
};
class Mammal :public virtual Animal
{
};
class Bird :public virtual Animal
{
};
class Reptile :public virtual Animal
{
};
class Platypus final:public Mammal, public Bird, public Reptile
{
public:
	Platypus()
	{
		cout << "Platypus constructor" << endl;
	}
};

int main()
{
	Platypus duckBilledP;
	duckBilledP.age = 25;
	return 0;
}

结果如下:
Animal constructor
Platypus constructor
从 Animal 类派生 Mammal、Bird 和 Reptile 类时,使用了关键字 virtual,这样 Platypus 继承这些类时,每个 Platypus 实例只包含一个 Animal 实例。同时,使用了关键字 final 以禁止将 Platypus 类用作基类。

11.4 使用final来禁止覆盖函数

class Tuna :public Fish
{
public:
	// override Fish :: Swim
	void Swim() override final
	{
		cout << "Tuna swims! " << endl;
	}
};

class BluefinTuna final : public Tuna
{
public:
	void Swim()   //Error: Swim() was final in Tuna, cannot override
	{

	}
};
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-08-06 10:25:04  更:2022-08-06 10:28:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年3日历 -2024/3/28 19:40:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码