• 多继承
    在这里插入图片描述
  • 实现这样的例子
    在这里插入图片描述
  • 测试程序
#include <stdio.h>
#include <iostream>

using namespace std;

class Sofa{
public:
	void watchTV(void){
		cout<<"watch TV"<<endl;
	}
};

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

class Sofabed:public Sofa,public Bed{

};

int main()
{
	Sofabed s;
	s.sleep();
	s.watchTV();
	system("pause");
	return 0;
}
  • testdemo2
    在这里插入图片描述
#include <stdio.h>
#include <iostream>

using namespace std;
class Furniture{
private:
	int weight;
public:
	void setWeight(int w){this->weight = w;}
	int getWeight(void) const {return weight;}
};
class Sofa:virtual public Furniture{
public:
	void watchTV(void){
		cout<<"watch TV"<<endl;
	}
};

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

class Sofabed:public Sofa,public Bed{

};

int main()
{
	Sofabed s;
	s.sleep();
	s.watchTV();

	s.setWeight(100);

	system("pause");
	return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐