cpp虚函数,基类命名指向子类实例必备佳选

Posted 飞凡可期

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cpp虚函数,基类命名指向子类实例必备佳选相关的知识,希望对你有一定的参考价值。

简单虚函数

//============================================================================
// Name        : p1022VirtualDeconstructor.cpp
// Author      : perfey
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class Base

public:
	Base()cout << "Conctructing Base!"<<endl;

	virtual ~Base()cout << "Deconstructing Base!"<<endl;//多一个空格,virtual和~之间
;

class Derive: public Base

public:
	  Derive()cout << "Constructing Derive!"<<endl;
      ~ Derive()cout << "Deconstructing Derive!"<<endl;
     /*
      * 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
      *
      * */
;


int main() 
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

	Base *bptr = new Derive(); //父类指针指向之类实例;
	delete bptr;

	return 0;


结果

Conctructing Base!
Constructing Derive!
Deconstructing Derive!
Deconstructing Base!

(如果没有 tilde~前面那个virtual修饰,那么不会有Deconstructing Derive!这一句)

继续传递,继承下去

//============================================================================
// Name        : p1022VirtualDeconstructor.cpp
// Author      : perfey
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class Base

public:
	Base()cout << "Conctructing Base!"<<endl;

	virtual ~Base()cout << "Deconstructing Base!"<<endl;//多一个空格,virtual和~之间
;

class Derive: public Base

public:
	  Derive()cout << "Constructing Derive!"<<endl;
      ~ Derive()cout << "Deconstructing Derive!"<<endl;
     /*
      * 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
      *
      * */
;

class Derive2: public Derive

public:
	  Derive2()cout << "Constructing DeriveSon!"<<endl;
      ~ Derive2()cout << "Deconstructing DeriveSon!"<<endl;
     /*
      * 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
      *
      * */
;


int main() 
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

	Base *bptr = new Derive2(); //父类指针指向之类实例;
	delete bptr;

	return 0;


结果

!!!Hello World!!!
Conctructing Base!
Constructing Derive!
Constructing DeriveSon!
Deconstructing DeriveSon!
Deconstructing Derive!
Deconstructing Base!

可以看到每一个子类都继承虚函数,都调用了Deconstructor这是预期的效果。

继续花样

//============================================================================
// Name        : p1022VirtualDeconstructor.cpp
// Author      : perfey
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class Base

public:
	Base()cout << "Conctructing Base!"<<endl;

	virtual ~Base()cout << "Deconstructing Base!"<<endl;//多一个空格,virtual和~之间

    virtual void Eat() = 0; //纯需函数,无需在基类中实现,奇怪吧,类似interface中概念
;

class Derive: public Base

public:
	  Derive()cout << "Constructing Derive!"<<endl;

       void Eat()override
    		  
    	  cout <<" 我吃生肉,茹毛饮血!!"<<endl;
              

      ~ Derive()cout << "Deconstructing Derive!"<<endl;
;

/*
 * 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
 *
 * */

class Derive2: public Derive

public:
	  Derive2()cout << "Constructing DeriveSon!"<<endl;
      ~ Derive2()cout << "Deconstructing DeriveSon!"<<endl;
     void Eat() 
    	  cout <<" 我今晚大口吃肉!"<<endl;
      
;


int main() 
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

	Base *bptr = new Derive(); //父类指针指向之类实例;
	bptr->Eat();
	bptr = new Derive2();
	bptr->Eat();

	delete bptr;

	return 0;


纯虚函数来结果

!!!Hello World!!!
Conctructing Base!
Constructing Derive!
我吃生肉,茹毛饮血!!
Conctructing Base!
Constructing Derive!
Constructing DeriveSon!
我今晚大口吃肉!
Deconstructing DeriveSon!
Deconstructing Derive!
Deconstructing Base!

以上是关于cpp虚函数,基类命名指向子类实例必备佳选的主要内容,如果未能解决你的问题,请参考以下文章

cpp虚函数,基类命名指向子类实例必备佳选

简单工厂类,有继承虚函数重写基类指针指向子类对象会发生多态

C++的虚函数表

C++的虚函数表

对虚函数进行重载是啥意思?

多态(day10)