多重继承与派生类成员标识

本文介绍了C++中的不同继承类型,包括单一继承、多重继承、菱形继承以及虚基类的概念。通过示例代码展示了如何创建这些继承结构,并讨论了菱形继承可能导致的问题及虚基类的解决方案。此外,还探讨了不同继承结构对对象大小的影响。
摘要由CSDN通过智能技术生成

单一继承

一个派生类只有一个直接基类的情况称为单一继承

多重继承或多继承

由多个基类共同派生出新的派生类,这样的继承结构称为多重继承

class Chair
{
protected:
	float money;
public:
	Chair(float ft=122):money(ft){}
	float Money() { return money; }
};

class Bed
{
protected:
	float money;
public:
	Bed(float ft=1200):money(ft){}
	float Money() { return money; }
};

//躺椅
class DeckChair :public Bed, public Chair
{
public:
	DeckChair(){}
	DeckChair(float bf, float cf) :Chair(cf), Bed(bf)
	{
		Bed::money = 3000;
	}
	void total()
	{
		float total = Bed::money + Chair::money;
		cout << total << endl;
	}
};
int main()
{
	DeckChair dc(1000, 10);//初始化顺序和继承顺序有关,和初始化列表无关
	dc.total();
	//dc.Money();
	dc.Bed::Money();
	dc.Chair::Money();
	return 0;
}

菱形继承

菱形继承是多重继承一种特殊情况
在这里插入图片描述


class person
{
private:
	int _idPerson;
public:
	person(int id) :_idPerson(id) { cout << "create Person" << endl; }
};
class Student :public person
{
private:
	int _snum;
public:
	Student(int s,int id):person(id),_snum(s){}
};
class Gstudent :public Student //研究生
{
private:
	int _gsnum;
public:
	Gstudent(int g,int s,int id):Student(s,id),_gsnum(g){}
};

class Employee :public person  //职工
{
private:
	int _enum;
public:
	Employee(int e,int id):person(id),_enum(e){}
};

class EGStudent :public Gstudent, public Employee //在职研究生
{
private:
	int _egsnum;
public:
	EGStudent(int es, int g, int s, int sid, int e, int eid)
		:Gstudent(g, s, sid), Employee(e, eid), _egsnum(es)
	{

	}
};

int main()
{
	EGStudent egs(1, 2, 3, 4, 5, 6);
	person per(1);
	per = (Gstudent)egs;
	per = (Employee)egs;
	return 0;
}

在这里插入图片描述

虚基类

在这里插入图片描述

class person
{
protected:
	int _idPerson;
public:
	person(int id) :_idPerson(id) { cout << "create Person" << endl; }
};
class Student :virtual public person  //虚继承人
{
private:
	int _snum;
public:
	Student(int s, int id) :person(id), _snum(s) { cout << "create student" << endl; }
};
class Gstudent :public Student //研究生
{
private:
	int _gsnum;
public:
	Gstudent(int g, int s, int id) :Student(s, id), _gsnum(g), person(id) { cout << "cteate Gstudent" << endl; }
};

class Employee :virtual public person  //虚继承人
{
private:
	int _enum;
public:
	Employee(int e, int id) :person(id), _enum(e) { cout << "create Employee" << endl; }
};

class EGStudent :public Gstudent, public Employee
{
private:
	int _egsnum;
public:
	EGStudent(int es, int g, int s, int sid, int e, int eid)
		:Gstudent(g, s, sid),
		Employee(e, eid),
		_egsnum(es),
		person(eid)
	{
		cout << "create EDGtudent" << endl;
	}
	void fun()
	{
		Gstudent::_idPerson = 100;
		Employee::_idPerson = 200;
		person::_idPerson = 300;
	}
};

int main()
{
	EGStudent egs(1, 2, 3, 4, 5, 6);
	person per(1);
	per=egs;
	person *p=nullptr;
	p=&egs;
	return 0;
}

运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

菱形继承的大小

class A
{};
class B :public A 
{};
class C : public A
{};
class D :public B, public C
{};
int main()
{
	D d;
	cout << sizeof(d) << endl;
}

在这里插入图片描述

class A
{};
class B :virtual public A 
{};
class C : public A
{};
class D :virtual public B, public C
{};
int main()
{
	D d;
	cout << sizeof(d) << endl;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡蓝色的经典

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值