多继承(基类 A b:a c:a d:public a,public b)

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

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

public:
	Furniture()
	{
		cout << "Furniture create" << endl;
	}

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

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

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

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

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

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

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

	void watch_TV()
	{
		cout << "Sofa watch_TV" <<endl;
	}
};

class Sleep_Sofa:public Bed,public Sofa
{
public:
	Sleep_Sofa()
	{
		cout << "Sleep_Sofa create" << endl;
	}

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

	void fold_out()
	{
		cout << "Sleep_Sofa fold_out" <<endl;
	}
};

int main()
{
	Sleep_Sofa sf;
	//sf.set_weight(100);  //error:不明确
	sf.Sofa::set_weight(100);

	cout << sf.Sofa::get_weight() << endl;

	/*Furniture create
	Bed create
	Furniture create
	Sofa create
	Sleep_Sofa create
	Furniture set_weight
	Furniture get_weight
	100*/

	return 0;
}
#include "stdafx.h"
#include <iostream>

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

public:
	Furniture()
	{
		cout << "Furniture create" << endl;
	}

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

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

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

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

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

class Sofa: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
{
public:
	Sleep_Sofa()
	{
		cout << "Sleep_Sofa create" << endl;
	}

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

	void fold_out()
	{
		cout << "Sleep_Sofa fold_out" <<endl;
	}
};

int main()
{
	Sleep_Sofa sf;
	//sf.set_weight(100);  //error:不明确
	sf.Sofa::set_weight(100);

	cout << sf.Sofa::get_weight() << endl;

	/*
	Furniture create
	Bed create
	Furniture create
	Sofa create
	Sleep_Sofa create
	Sofa set_weight
	Sofa get_weight
	100
	*/

	Furniture fn;
	fn.set_weight(1);
	cout << fn.get_weight() << endl;
	/*
	Furniture create
	Furniture set_weight
	Furniture get_weight
	1
	*/
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值