【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!
相关推荐
guygg886 分钟前
基于matlab的FIR滤波器
开发语言·算法·matlab
双叶83622 分钟前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
源代码•宸36 分钟前
C++高频知识点(二)
开发语言·c++·经验分享
你怎么知道我是队长1 小时前
python-input内置函数
开发语言·python
jyan_敬言2 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
慕y2742 小时前
Java学习第十六部分——JUnit框架
java·开发语言·学习
liulilittle2 小时前
SNIProxy 轻量级匿名CDN代理架构与实现
开发语言·网络·c++·网关·架构·cdn·通信
Shartin3 小时前
CPT208-Human-Centric Computing: Prototype Design Optimization原型设计优化
开发语言·javascript·原型模式
dme.3 小时前
Javascript之DOM操作
开发语言·javascript·爬虫·python·ecmascript
teeeeeeemo3 小时前
回调函数 vs Promise vs async/await区别
开发语言·前端·javascript·笔记