C++:虚函数,纯虚函数以及虚析构函数

一、虚函数就是在类中声明,并有函数定义的,可以在其派生类中对其进行重新定义的成员函数,和虚基类类似的是在函数第一次声明时要有关键字virtual。同时,在派生类内,对基类虚函数的重新定义要求函数类型,函数名,形参个数和类型顺序都要与基类虚函数保持一致,否则自动视为其他普通成员函数。格式为:

virtual 【函数类型】【函数名】(参数列表);

二、纯虚函数除了没有函数定义,其他方面要求与虚函数一致。格式为:

virtual 【函数类型】【函数名】(参数列表)=0;

此外,拥有至少一个纯虚函数的类,我们叫做抽象类。

三、虚析构函数的应用:当我们把一块派生类大小的内存赋予指向基类对象的指针,在使用结束后调用析构函数时,程序只会调用基类的析构函数。如果我们想要先调用派生类析构函数再调用基类析构函数,我们就要在基类和派生类中分别定义虚析构函数。

四、代码示例:

首先演示对虚函数,纯虚函数的应用,由于差异不大,本例只使用纯虚函数。

include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Base//抽象类
{
protected:
	double r;
public:
	Base(int R) {
		r = R;
	}
	virtual void square() = 0;//纯虚函数
	
};
class circle :public Base {
public:
	circle(int R) :Base(R)
	{}
	virtual void square() {
		cout << "The square of th circle is:" << 3.1415927 * r * r<<endl;
	}
	
};
class triangle :public Base{
private:
	double h;
public:
	triangle(int R, int H) :Base(R) {
		h = H;
	}
	virtual void square() {
		cout << "The square of the Triangle is:" << 0.5 * h * r<<endl;
	}
	
};
int main() {
	Base* ptr;
	circle c(4);
	triangle t(5, 8);
	c.square();//用对象调用对象重载的虚函数成员
	t.square();
	ptr = &c;//用基类的指针指向派生类并调用虚函数,可以调用派生类重定义的虚函数,如果不是虚函数则无法调用
	ptr->square();
	ptr = &t;
	ptr->square();

运行结果:

The square of th circle is:50.2655
The square of the Triangle is:20
The square of th circle is:50.2655
The square of the Triangle is:20

可见,使用虚函数,能让被赋予派生类地址的指向基类对象的指针访问派生类的成员函数,否则该指针只能调用派生类从基类继承的函数,这新定义的函数是不能被调用的 。

其次,当我给基类和一个派生类定义了虚析构函数,不给另一个派生类定义虚析构函数,会怎么样呢?

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Base //抽象类
{
protected:
	double r;
public:
	Base(int R) {
		r = R;
	}
	virtual void square() = 0;//纯虚函数
	virtual ~Base() {
		cout << "调用Base的析构函数" << endl;
	}
};
class circle :public Base {
public:
	circle(int R) :Base(R)
	{}
	virtual void square() {
		cout << "The square of th circle is:" << 3.1415927 * r * r<<endl;
	}
	virtual ~circle() {
		cout << "调用circle的析构函数" << endl;
	}
};
class triangle :public Base{
private:
	double h;
public:
	triangle(int R, int H) :Base(R) {
		h = H;
	}
	virtual void square() {
		cout << "The square of the Triangle is:" << 0.5 * h * r<<endl;
	}
	//不定义该类的虚析构函数,用来作对比
};
int main() {
	Base* ptr;
	//下面演示分配一块派生类大小的内存给指向基类的指针并初始化,然后再消除掉这块内存,重点看这个过程析构函数的调用
	ptr = new circle(4);
	delete ptr;
	ptr = new triangle(5, 8);
	delete ptr;
}

运行结果:

调用circle的析构函数
调用Base的析构函数
调用Base的析构函数

发现了吗,没有调用派生类triangle的析构函数,因为我没有定义它的虚析构函数。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值