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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值