OJ嘻唰唰之改错题（6）-虚拟继承（虚基类）沙发床

问题及代码：
/*
*All  rights reserved.
*文件名称：test.cpp
*作者：陈银丽
*完成日期：2015年7月7日
*版本号：v1.0
*
*问题描述：虚拟继承（虚基类）—沙发床.有一种特殊的床，既能当床(Bed)用又能当沙发(Sofa)用，所以叫沙发床(SleeperSofa)。同时床和沙发

*输入描述：存在错误的代码
//床类Bed
class Bed: public Furniture
{
public:
Bed(double we,double l,double wi):Furniture(we),length(l),width(wi){}
void display()
{
cout<<"length:"<<length<<endl;
cout<<"width:"<<width<<endl;
}
protected:
double length; //床的长
double width; //床的宽
};

//沙发类Sofa
class Sofa: public Furniture
{ public:
Sofa(double w,double h):Furniture(w),height(h){}
void display()
{
cout<<"height:"<<height<<endl;
}
protected:
double height; //沙发的高度
};

//沙发床
class SleeperSofa:public Bed, public Sofa
{public:
SleeperSofa(double we,double l,double wi,double h):Bed(we,l,wi),Sofa(we,h){ }
void display()
{
cout<<"weight:"<<weight<<endl;
Bed::display();
Sofa::display();
}
};
*程序输出：
*/

#include <iostream>
using namespace std;

//家具类Furniture
class Furniture
{
public:
Furniture(double w)
{ weight=w; }
void display()
{
cout<<"weight:"<<weight<<endl;
}
protected:
double weight; //家具重量
};

class Bed: public Furniture
{
public:
Bed(double we,double l,double wi):Furniture(we),length(l),width(wi){}
void display()
{
Furniture::display();
cout<<"length:"<<length<<endl;
cout<<"width:"<<width<<endl;
}
protected:
double length; //床的长
double width; //床的宽
};

//沙发类Sofa
class Sofa: public Furniture
{ public:
Sofa(double w,double h):Furniture(w),height(h){}
void display()
{
cout<<"height:"<<height<<endl;
}
protected:
double height; //沙发的高度
};

//沙发床
class SleeperSofa:public Bed, public Sofa
{public:
SleeperSofa(double we,double l,double wi,double h):Bed(we,l,wi),Sofa(we,h){ }
void display()
{
Bed::display();
Sofa::display();
}
};
int main()
{
double weight,length,width,height;
cin>>weight>>length>>width>>height;

SleeperSofa ss(weight,length,width,height);
ss.display();

return 0;
}




嘻唰唰第六批Problem J: B 虚拟继承（虚基类）-沙发床（改错题）

2015-07-21 18:50:24

OJ嘻唰唰 类模板

2015-06-15 15:13:16

OJ 嘻唰唰抽象基类

2015-07-03 14:33:41

OJ嘻唰唰之改错题（4）-C++继承

2015-07-06 18:43:42

OJ嘻唰唰之改错题（1）-商品销售类

2015-07-04 15:43:19

OJ嘻唰唰之改错题（2）-体检情况分析

2015-07-04 16:09:07

OJ嘻唰唰之改错题（3）植物与花

2015-07-05 19:30:49

OJ嘻唰唰 Shape系列-4

2015-07-02 10:24:29

OJ嘻唰唰第二批之成绩统计

2015-06-19 19:49:05