6-15 沙发床-虚基类 (10分)
根据给出的Furniture类以及SleeperSofa类,定义SleeperSofa继承时的父类Bed和Sofa
Furniture 类与测试程序的定义如下:
#include<iostream>
using namespace std;
class Furniture
{
protected:
int weight ;
public:
Furniture(){}
void setWeight( int i ){ weight= i ;}
int getWeight() { return weight; }
};
/* 请在这里填写答案 */
class SleeperSofa :public Bed,public Sofa
{
public:
SleeperSofa() {}
void foldout()
{
cout<<"Fold out sleepersofa"<<endl;
}
};
int main()
{
SleeperSofa ss;
ss.setWeight(20);
cout<< ss.getWeight()<<endl;
ss.sleep();
ss.watchTV();
ss.foldout();
return 0;
}
输入样例:
无
输出样例:
在这里给出相应的输出。例如:
20
Sleeping
Watching TV
Fold out sleepersofa
代码实现:
class Bed:virtual public Furniture{//虚基类的基本写法
public:
Bed()//一定要有这个,因为有无参函数,下面也是
{
}
void sleep()
{
cout<<"Sleeping"<<endl;
}
};
class Sofa:virtual public Furniture{
public:
void watchTV()
{
cout<<"Watching TV"<<endl;
}
Sofa()
{
}
};