虚拟继承

//---------main.cpp------------
#include <iostream>
#include <string>

//多重继承

using namespace std;
class Furniture
{
protected:
	int weight;

public:
	Furniture(){}                     //构造函数
	void setWeight(int i){weight = i;}
	int getWeight(){return weight;}

};
class Bed : public Furniture
{
public:
	Bed(){weight=0;}//构造函数
	void sleep(){std::cout<<"Sleeping ..."<<endl;}
};

class Sofa : public Furniture
{
public:
	Sofa(){weight = 0;}//构造函数
	void watchTV(){std::cout<<"Watch TV ..."<<endl;}
};
class SleeperSofa : public Bed,public Sofa
{
public:
	SleeperSofa(){}//构造函数
	void foldOut(){std::cout<<"Fold out the sofa"<<endl;}
};
int main(int argc,char **argv)
{
	SleeperSofa ss;
	ss.Sofa::setWeight(20);
	std::cout<<"ss.Sofa::getWeight() is "<<ss.Sofa::getWeight()<<endl;
	ss.Bed::setWeight(10);
	std::cout<<"ss.Bed::getWeight()is "<<ss.Bed::getWeight()<<endl;

	/*--上面SleeperSofa的weight没有实现公用,没有实现沙发床的功能--*/
<span style="white-space:pre">	</span>//ss.setWeight(20);                                           //错
        //std::cout<<"SleeperSofa weight is "<<ss.getWeight()<<endl;   //错
	return 0;
}

ss.Sofa::getWeight() is 20
ss.Bed::getWeight()is 10
请按任意键继续. . .

                                        Furniture   -- >weight

                                       /               \

                                     /                   \

                                 Bed                Sofa

                                     \                   /                                                                                                                \                 /

                                      SleeperSofa


虚拟继承virtual

SleeperSofa只需对应一个Furniture对象,所以我们也希望它只含有一个Furniture副本。且该副本又要共享Bed和Sofa的成员函数和数据成员

</pre><pre name="code" class="cpp">//---------main.cpp------------
#include <iostream>
#include <string>

//多重继承

using namespace std;
class Furniture
{
protected:
	int weight;

public:
	Furniture(){}                     //构造函数
	void setWeight(int i){weight = i;}
	int getWeight(){return weight;}

};
class Bed : virtual public Furniture
{
public:
	Bed(){weight=0;}//构造函数
	void sleep(){std::cout<<"Sleeping ..."<<endl;}
};

class Sofa : virtual public Furniture
{
public:
	Sofa(){weight = 0;}//构造函数
	void watchTV(){std::cout<<"Watch TV ..."<<endl;}
};
class SleeperSofa : public Bed,public Sofa
{
public:
	SleeperSofa(){}//构造函数
	void foldOut(){std::cout<<"Fold out the sofa"<<endl;}
};
int main(int argc,char **argv)
{
	SleeperSofa ss;
	//ss.Sofa::setWeight(20);
	//std::cout<<"ss.Sofa::getWeight() is "<<ss.Sofa::getWeight()<<endl;
	//ss.Bed::setWeight(10);
	//std::cout<<"ss.Bed::getWeight()is "<<ss.Bed::getWeight()<<endl;
	ss.setWeight(20);
	std::cout<<"Sleeper weight is  "<<ss.getWeight()<<endl;

	/*--上面SleeperSofa的weight实现公用了,实现沙发床的功能--*/

	return 0;
}

Sleeper weight is  20
请按任意键继续. . .

含有多继承的构造函数,按以下顺序被调用:

(1)任何虚拟基类的构造函数按照他们被继承的顺序构造。

(2)任何非虚拟基类的构造函数按照他们被继承的顺序构造。

(3)任何成员对象的构造函数按照他们的声明顺序构造。

(4)类自己的构造函数。

   若一个类中,含有虚拟继承,则构造首先从虚拟基类开始。

构造一个 SleeperSofa ss;

 其对象构造的顺序是:

(1)Furniture对象;

(2)Bed对象,不管构造函数的初始化列表是否把Bed排在Sofa后面

(3)Sofa对象;

(4)SleeperSofa对象。


1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值