俩概念
首先记住俩概念,地址的早绑定和晚绑定
- 静态多态的函数地址早绑定 - 编译阶段确定函数地址
- 动态多态的函数地址晚绑定 - 运行阶段确定函数地址
(我的理解:多态是基于继承的,一种动态的调用函数的方式)
(加了virtual的函数是虚函数,虚函数在编译阶段不能够确定他的调用的,或者说就是编译阶段地址没有绑定?)
例子
#include <iostream>
using namespace std;
class Animal
{
public:
void speak()
{
cout << "动物在说话" << endl;
}
//virtual void speak()
//{
// cout << "动物在说话" << endl;
//}
};
class Cat :public Animal
{
public:
void speak()
{
cout << "小猫在说话" << endl;
}
};
class Dog :public Animal
{
public:
void speak()
{
cout << "小狗在说话" << endl;
}
};
void DoSpeak(Animal& animal)
{
animal.speak();
}
void test01()
{
Cat cat;
DoSpeak(cat);
cat.speak();
cat.Animal::speak();
cout << "--------------" << endl;
Dog dog;
DoSpeak(dog);
cout << "--------------" << endl;
Animal s;
DoSpeak(s);
cout << sizeof(s) << endl; //空类的大小是1
}
int main() {
test01();
system("pause");
return 0;
}
在这里,还没有使用到多态,就是正常的继承的思想。
然后把父类的animal的speak函数加上virtual变成虚函数
函数前面加上virtual关键字,变成虚函数,那么编译器在编译的时候就不能确定函数调用了
多态要满足
如何实现
那么这种多态是怎么实现的
首先可以看一下类的所占的字节数。
空类所占字节数为1.(如果你有成员变量占得字节数就不一样了)
在C++里,类内的成员变量和成员函数是分开储存的。
当把类中的speak改为虚函数时
我的电脑是64位操作系统,所以指针是8个字节。
关于指针的大小https://blog.csdn.net/weixin_45990850/article/details/125831300
(所以说多态本质上应该还是指针的一种调用)
在Animal类里
这里的指针是vfptr,vfptr:虚函数(表)指针
v - virtual
f - function
ptr - pointer
vfptr会指向一个vftable(虚函数表)
表内部会记录一个虚函数地址(在这里我设置成speak是虚函数,也就是说这个表里记录了speak的地址 &Animal::speakVirtual)
当Animal被cat继承,Animal的虚函数被重写,cat的虚函数表的内容会变成**&cat::speakVirtual**
void DoSpeak(Animal& animal)
{
animal.speak();
}
Cat cat;
DoSpeak(cat);
------------等同于----------------
Animal &animal = cat;
amimal.speak();
多态下,就是说我给cat,我就在cat里的虚函数表找地址,给dog就在dog的虚函数表里找。
以下是视频里的截图
关于打开vscode的个工具
https://learn.microsoft.com/zh-cn/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022
https://blog.csdn.net/weixin_45729624/article/details/116402717
Animal类里的speak是 virtual虚函数
子类cat继承但未发生重写时
子类继承且重写speak
纯虚函数和抽象类
大多数情况下,父类的函数都是一定会去重写的,父类里虚函数的实现就没意义。所以可以把父类里的虚函数改成纯虚函数。
virtual void func() = 0;
当类中有了纯虚函数,这个类也称为抽象楼
注意:
抽象类无法实例化对象
子类必须重写抽象类中的纯虚函数,否则改子类也属于抽象类
几个黑马的例子
class Base
{
public:
//纯虚函数
//类中只要有一个纯虚函数就称为抽象类
//抽象类无法实例化对象
//子类必须重写父类中的纯虚函数,否则也属于抽象类
virtual void func() = 0;
};
class Son :public Base
{
public:
virtual void func()
{
cout << "func调用" << endl;
};
};
void test01()
{
Base * base = NULL;
//base = new Base; // 错误,抽象类无法实例化对象
base = new Son;
base->func();
delete base;//记得销毁
}
int main() {
test01();
system("pause");
return 0;
}
//抽象制作饮品
class AbstractDrinking {
public:
//烧水
virtual void Boil() = 0;
//冲泡
virtual void Brew() = 0;
//倒入杯中
virtual void PourInCup() = 0;
//加入辅料
virtual void PutSomething() = 0;
//规定流程
void MakeDrink() {
Boil();
Brew();
PourInCup();
PutSomething();
}
};
//制作咖啡
class Coffee : public AbstractDrinking {
public:
//烧水
virtual void Boil() {
cout << "煮农夫山泉!" << endl;
}
//冲泡
virtual void Brew() {
cout << "冲泡咖啡!" << endl;
}
//倒入杯中
virtual void PourInCup() {
cout << "将咖啡倒入杯中!" << endl;
}
//加入辅料
virtual void PutSomething() {
cout << "加入牛奶!" << endl;
}
};
//制作茶水
class Tea : public AbstractDrinking {
public:
//烧水
virtual void Boil() {
cout << "煮自来水!" << endl;
}
//冲泡
virtual void Brew() {
cout << "冲泡茶叶!" << endl;
}
//倒入杯中
virtual void PourInCup() {
cout << "将茶水倒入杯中!" << endl;
}
//加入辅料
virtual void PutSomething() {
cout << "加入枸杞!" << endl;
}
};
//业务函数
void DoWork(AbstractDrinking* drink) {
drink->MakeDrink();
delete drink;
}
void test01() {
DoWork(new Coffee);
cout << "--------------" << endl;
DoWork(new Tea);
}
int main() {
test01();
system("pause");
return 0;
}
虚析构和纯虚析构
多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
#include <iostream>
using namespace std;
class Animal {
public:
Animal()
{
cout << "Animal 构造函数调用!" << endl;
}
virtual void Speak() = 0;
~Animal()
{
cout << "Animal 析构函数调用!" << endl;
}
};
class Cat : public Animal {
public:
string* m_Name;
public:
Cat(string name)
{
cout << "Cat构造函数调用!" << endl;
m_Name = new string(name);
}
virtual void Speak()
{
cout << *m_Name << "小猫在说话!" << endl;
}
~Cat()
{
cout << "Cat析构函数调用!" << endl;
if (this->m_Name != NULL) {
delete m_Name;
m_Name = NULL;
}
}
};
void test01()
{
Animal* animal = new Cat("Tom");
animal->Speak();
delete animal;
}
int main() {
test01();
system("pause");
return 0;
}
要解决那样的情况,就可以把父类的析构函数改为虚析构或者纯虚析构
虚析构
纯虚析构