虚拟继承

#include <iostream>
using namespace std;

class Furniture
{
public:
	Furniture(){}
	void SetWeight(unsigned i)
	{
		weight = i;
	}
	unsigned GetWeight()
	{
		return weight;
	}
protected:
	unsigned weight;
};


class Bed : virtual public Furniture
{
public:
	Bed(){}
	void Sleep()
	{
		cout<<"sleeping..."<<endl;
	}
};

class Sofa : virtual public Furniture
{
public:
	Sofa(){}
	void WatchTV()
	{
		cout<<"watching TV..."<<endl;
	}
};

class SleepSofa : public Bed, public Sofa
{
public:
	SleepSofa(){}
	void FoldOut()
	{
		cout<<"Fold out the sofa."<<endl;
	}
};


int main()
{
	SleepSofa ss;
	ss.WatchTV();
	ss.FoldOut();
	ss.Sleep();
	ss.SetWeight(20);
	cout<<ss.GetWeight()<<endl;
	return 0;
}
posted @ 2010-10-14 13:56  Cranny  阅读(270)  评论(0编辑  收藏  举报