【C++ Virtual 有啥用啊?】

1.应用于多态中

代码示例如下

复制代码
#include <iostream>
using namespace std;

class Animal
{
public:
	Animal(){}
	~Animal(){}
	void shout()
	{
		cout << "Animal is shout!" << endl;
	}
};

class Dog:public Animal
{
public:
	Dog():Animal(){}
	~Dog(){}
	void shout()
	{
		cout << "Dog is shout!" << endl;
	}
};

class Cat:public Animal
{
public:
	Cat():Animal(){}
	~Cat(){}
	void shout()
	{
		cout << "Cat is shout!" << endl;
	}
};
int main()
{
   Animal *dog = new Dog();
   Animal *cat = new Cat();
   dog->shout();
   cat->shout();
   return 0;
}

以下是其输出

复制代码
Animal is shout!
Animal is shout!

解释 :子类对象,使用父类指针进行声明,相当于'向上转换',转换后的指针拥有且仅拥有父类属性方法!!!

虽然父类和子类都有各自的shout方法,但是如果这么写,他们一点关系都没有!!!这等同于,父类中拥有方法0,子类1中扩展出了方法1,子类2中扩展出了方法2。

所以,你想说,我不是在子类中重写了shout方法吗?虽然'向上转换了',不应该用我重写后方法吗?抱歉,父类中的shout方法很直男!!!想让他脑瓜儿灵活点,重写请使用virtual
请不要混淆重载(同一个类中)和重写(用于继承关系中)
这里还说了另外两种用法:纯虚函数(抽象类)和虚拟继承(解决菱形继承问题)

修改后的代码示例如下:

复制代码
#include <iostream>
using namespace std;

class Animal
{
public:
	Animal(){}
	~Animal(){}
	virtual void shout()
	{
		cout << "Animal is shout!" << endl;
	}
};

class Dog:public Animal
{
public:
	Dog():Animal(){}
	~Dog(){}
	void shout()
	{
		cout << "Dog is shout!" << endl;
	}
};

class Cat:public Animal
{
public:
	Cat():Animal(){}
	~Cat(){}
	void shout()
	{
		cout << "Cat is shout!" << endl;
	}
};
int main()
{
   Animal *dog = new Dog();
   Animal *cat = new Cat();
   dog->shout();
   cat->shout();
   return 0;
}

输出如下

复制代码
Dog is shout!
Cat is shout!

所以想让父类指针聪明些,重写 请使用virtual。(在父类中声明virtual即可)

virtual隔代都有效

比如现在孙子类狼青继承自Dog,向上转换后的爷类指针和父类指针,也会按照狼青那样叫。

代码示例如下

复制代码
#include <iostream>
using namespace std;

class Animal
{
public:
	Animal(){}
	~Animal(){}
	virtual void shout()
	{
		cout << "Animal is shout!" << endl;
	}
};

class Dog:public Animal
{
public:
	Dog():Animal(){}
	~Dog(){}
	void shout()
	{
		cout << "Dog is shout!" << endl;
	}
};

class Cat:public Animal
{
public:
	Cat():Animal(){}
	~Cat(){}
	void shout()
	{
		cout << "Cat is shout!" << endl;
	}
};
class Langqing:public Dog
{
public:
	Langqing():Dog(){}
	~Langqing(){}
	void shout()
	{
		cout << "Langqing is shout!" << endl;
	}
};
int main()
{
   Animal *dog = new Dog();
   Animal *cat = new Cat();
   dog->shout();
   cat->shout();
   Animal *lq1 = new Langqing();
   lq1->shout();
   Dog *lq2 = new Langqing();
   lq2->shout();
   return 0;
}

输出如下

复制代码
Dog is shout!
Cat is shout!
Langqing is shout!
Langqing is shout!
相关推荐
flyair_China7 分钟前
【云架构】
开发语言·php
Chef_Chen13 分钟前
从0开始学习R语言--Day20-ARIMA与格兰杰因果检验
开发语言·学习·r语言
zh_xuan14 分钟前
c++ std::pair
开发语言·c++
CodeWithMe36 分钟前
【C/C++】EBO空基类优化介绍
开发语言·c++
404.Not Found1 小时前
Day46 Python打卡训练营
开发语言·python
love530love1 小时前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月1 小时前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华21 小时前
go中的接口返回设计思想
开发语言·后端·golang
lifallen1 小时前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰1 小时前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python