抽象类中定义纯虚函数

本文详细解析了C++中纯虚函数及虚析构函数的概念与使用方法,包括如何正确定义纯虚函数使其所在的类成为抽象类,以及如何为纯虚析构函数提供定义以确保派生类能够正确地被销毁。此外还探讨了构造函数和析构函数中调用虚函数的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先看一段代码:

class Instrument
{
public:
virtual	void play()const=0//非法的定义在抽象类中定义纯虚函数
	{
		cout<<"Instrument Play\n";
	}
};

class Wind:public Instrument
{
	void play( )const
	{
		cout<<"Wind Play\n";
	}
};

void main()
{
	Wind s;
	Instrument &p = s;
	p.play();
}

以下是纯虚函数在抽象类中的定义:

#include <iostream>
#include <string>
using namespace std;

class Instrument
{
public:
	virtual void play()const =0;//纯虚函数    这个抽象类的虚函数指针是空的
                              //不可以内联实现纯虚函数,但是可以在类外部实现
};
//在基类作为抽象类的视乎为其的纯虚函数提供定义是可以的,这样可以使一些公共代码可以在一些或者所有的派生类中都能调用
void Instrument::play()const
{
	cout<<"Instrument play()\n";
}
/*以下方式也可以
inline void Instrument::play()const
{
	cout<<"Instrument play()\n";
}*/


class Wind:public Instrument
{
	void play()const
	{
		Instrument::play();
		cout<<"Wind Play()\n";
	}
};

void main()
{
	Wind s;
	Instrument &p = s;
	p.play();
}

下来要说的是要在使用一个纯虚析构函数的时候要提供定义:(这最好是一个准则,会在后边的避免很多错误

#include <iostream>
#include <string>
using namespace std;



class pet
{
public:
	virtual ~pet()=0;
};
pet::~pet()
{
	cout<<"~pet()\n";
}
class Dog:public pet
{
public:
	~Dog()
	{
		cout<<"~Dog()\n";
	}
};

void main()
{
	pet* p = new Dog;
	delete p;
}

再看一个在构造函数和析构函数中调用虚函数:

#include <iostream>
#include <string>
using namespace std;

class base1
{
public:
	base1()//构造函数左括号之前对象已经被编译器建立了,下面只是输出执行信息
	{    f();//在构造函数中调用不管是不是虚构函数都是执行早绑定,因为此时类型建立的对象信息还不完全,无法进行动态绑定,所以调用的是本本地的函数
		cout<<"base1::base()\n";
	}
	virtual void f()
	{
		cout<<"base1::f()\n";
	}	
	virtual ~base1()
	{
		f();//这里对象的信息全但是此时还是会调用本地函数,因为编译器认为这些信息不可靠
		cout<<"~base1()\n";
		
	}

};
class Derived1:public base1
{
public:
	Derived1()
	{
		cout<<"Derived1::Derived1()\n";
	}
	void f()
	{
		cout<<"Derived1::f()\n";
	}
	~Derived1()
	{
		cout<<"~Derived1()\n";
	}
};


void main()
{
	base1 *p=new Derived1;
	delete p;
	void*p = new Derived1;
	delete p;//删除void*指针因为编译器不知道类型所以不会调用析构函数只是释放空间
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值