多继承2升级

本文通过C++代码示例介绍了多继承与虚继承的概念及应用,探讨了如何解决基类中成员函数被重复定义的问题,并展示了虚继承如何避免基类的多次实例化。

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

#include "stdafx.h"
#include <iostream>

using namespace std;
//基类 A   b:a  c:a  d:public a,public b
class Furniture
{
	int weight;

public:
	Furniture()  //不允许使用virtual 
	{
		cout << "Furniture create" << endl;
	}

	virtual ~Furniture()
	{
		cout << "Furniture delete" << endl;
	}

	virtual void set_weight(int w)
	{
		weight = w;
		cout << "Furniture set_weight" <<endl;
	}
	virtual int get_weight()
	{
		cout << "Furniture get_weight" <<endl;
		return weight;
	}
};

class Bed:virtual public Furniture
{
	int weight;
public:
	Bed()
	{
		cout << "Bed create" << endl;
	}

	~Bed()
	{
		cout << "Bed delete" << endl;
	}

	void sleep()
	{
		cout << "Bed sleep" <<endl;
	}

	void set_weight(int w)
	{
		weight = w;
		cout << "Bed set_weight" <<endl;
	}
	int get_weight()
	{
		cout << "Bed get_weight" <<endl;
		return weight;
	}
};

class Sofa:virtual public Furniture
{
	int weight;
public:
	Sofa()
	{
		cout << "Sofa create" << endl;
	}

	~Sofa()
	{
		cout << "Sofa delete" << endl;
	}

	void watch_TV()
	{
		cout << "Sofa watch_TV" <<endl;
	}
	void set_weight(int w)
	{
		weight = w;
		cout << "Sofa set_weight" <<endl;
	}
	int get_weight()
	{
		cout << "Sofa get_weight" <<endl;
		return weight;
	}
};

class Sleep_Sofa:public Bed,public Sofa  //两个父类Bed与Sofa都重载了基类的两个函数set_weight/get_weight 会报Furniture::set_weight的重写不明确 解决办法只有本类重写这两个函数
{
	int weight;
public:
	Sleep_Sofa()
	{
		cout << "Sleep_Sofa create" << endl;
	}

	~Sleep_Sofa()
	{
		cout << "Sleep_Sofa delete" << endl;
	}

	void fold_out()
	{
		cout << "Sleep_Sofa fold_out" <<endl;
	}
	void set_weight(int w)
	{
		weight = w;
		cout << "Sleep_Sofa set_weight" <<endl;
	}
	int get_weight()
	{
		cout << "Sleep_Sofa get_weight" <<endl;
		return weight;
	}

};
//多继承要加虚继承 虚析构 重写函数要变为虚函数
//缺点:基类会多次创建(加虚继承)   
//基类A指向d子类的时候会出现基类A不明确的问题  (加虚继承)  
//子类d调用a基类成员的时候会出现不明确调用的问题,不知道调用b指向的基类成员还是c只想的基类成员,当然这个可以通过限定作用域来解决,即在它前加上对应的一个类名((加虚继承可以解决编译报错,但是要得到我们想要的结果,还需要在被重写的函数前加virtual,当然如果不重写就不用加)
//在析构的时候只析构了父类,然后报错(需要加虚析构)
int main()
{
	//----------------------------------加了虚继承与虚析构、虚函数-----------------------------------------
	Furniture *fn = new Sleep_Sofa;

	fn->set_weight(100);
	cout << fn->get_weight() << endl;
	delete fn;  //没有虚析构 调用Furniture delete之后报错
	fn = nullptr;
	/*
		只有Sofa重载了基类函数、调用Sofa的成员函数
		Furniture create
		Bed create
		Sofa create
		Sleep_Sofa create
		Sofa set_weight
		Sofa get_weight
		100
		Sleep_Sofa delete
		Sofa delete
		Bed delete
		Furniture delete
	*/
	/*
	b c d三个类都重写了基类的成员(被重写的成员函数 只需要a基类声明它为virtual就行,后边重写的类都不用加virtual,但是也是virtual)
	Furniture create
	Bed create
	Sofa create
	Sleep_Sofa create
	Sleep_Sofa set_weight
	Sleep_Sofa get_weight
	100
	Sleep_Sofa delete
	Sofa delete
	Bed delete
	Furniture delete
	*/

	//-------------------加了虚继承(只需要b、c加虚继承)与虚析构(只需要a基类加虚析构)------------------------
	/*Furniture *fn = new Sleep_Sofa;

	fn->set_weight(100);
	cout << fn->get_weight() << endl;
	delete fn;  //没有虚析构 调用Furniture delete之后报错
	fn = nullptr;*/

	/*
	只需要a基类加虚析构
	Furniture create
	Bed create
	Sofa create
	Sleep_Sofa create
	Furniture set_weight
	Furniture get_weight
	100
	Sleep_Sofa delete
	Sofa delete
	Bed delete
	Furniture delete
	*/

	//-------------------------加了虚继承与虚析构---------------------------------------
	/*Furniture *fn = new Sofa;

	fn->set_weight(100);
	cout << fn->get_weight() << endl;
	delete fn;  //没有虚析构 调用Furniture delete之后报错
	fn = nullptr;*/
	/*
	加了虚析构之后
	Furniture create
	Sofa create
	Furniture set_weight
	Furniture get_weight
	100
	Sofa delete
	Furniture delete
	*/


	//-------------------------没加虚继承与只加了虚继承---------------------------------------
	//Sofa sf;

	/*Sleep_Sofa ss;
	Furniture fn = ss;  //error 基类Furniture不明确  加了虚继承就可以了
	//Furniture fn = sf;  //这里不再创建 Furniture  只加虚继承这里的输出还是一样
	fn.set_weight(1);
	cout << fn.get_weight() << endl;*/

	/* 只是虚继承 fn指向sf的输出
	Furniture create
	Sofa create
	Furniture set_weight
	Furniture get_weight
	1
	*/

	/*只是虚继承 fn指向ss的输出
	Furniture create
	Bed create
	Sofa create
	Sleep_Sofa create
	Furniture set_weight
	Furniture get_weight
	1
	*/
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值