OJ第六批——Problem J: B 虚拟继承(虚基类)-沙发床(改错题)
来源:互联网 发布:七友软件 编辑:程序博客网 时间:2024/06/11 16:20
问题及代码:
Problem J: B 虚拟继承(虚基类)-沙发床(改错题)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 442 Solved: 353
[Submit][Status][Web Board]
Description
有一种特殊的床,既能当床(Bed)用又能当沙发(Sofa)用,所以叫沙发床(SleeperSofa)。
同时床和沙发又是一种特殊的家具(Furniture),具有一切家具的特性。
利用虚拟继承(虚基类)建立一个类的多重继承,沙发床继承了床和沙发的特性。
下面的程序中,在begin到end部分存在语法错误。请改正错误,使程序按下面输入输出的规定运行。
注意:只提交修改过的begin到end部分的代码。
#include <iostream>
using namespace std;
//家具类Furniture
class Furniture
{
public:
Furniture(double w)
{ weight=w; }
void display()
{
cout<<"weight:"<<weight<<endl;
}
protected:
double weight; //家具重量
};
//******************** begin ********************
//床类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();
}
};
//********************* end ********************
int main()
{
double weight,length,width,height;
cin>>weight>>length>>width>>height;
SleeperSofa ss(weight,length,width,height);
ss.display();
return 0;
}
Input
依次输入沙发床的重量、长、宽、高
Output
依次输出沙发床的重量、长、宽、高
Sample Input
200 1.8 1.5 1.2
Sample Output
weight:200length:1.8width:1.5height:1.2
HINT
#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:virtual 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:virtual 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):Furniture(we) ,Bed(we,l,wi),Sofa(we,h) { } void display() { cout<<"weight:"<<Furniture::weight<<endl; 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; }
- OJ第六批——Problem J: B 虚拟继承(虚基类)-沙发床(改错题)
- 嘻唰唰第六批Problem J: B 虚拟继承(虚基类)-沙发床(改错题)
- OJ第三批——Problem Q: B 虚拟继承(虚基类)-沙发床(改错题)
- OJ嘻唰唰之改错题(6)-虚拟继承(虚基类)沙发床
- YTU 2622: B 虚拟继承(虚基类)-沙发床(改错题)
- [YTU]_2622(B 虚拟继承(虚基类)-沙发床(改错题))
- OJ第六批——Problem B: A改错题--植物与花
- 【OJ】---B---C++继承(改错题)
- YTU-OJ-Problem J: 熟悉题型——改错题(商品销售)
- OJ第三批——Problem L: C++继承(改错题)
- OJ第六批——Problem I: B 继承 圆到圆柱体
- oj C++继承(改错题)
- oj-2-继承(改错)
- YTU-OJ-Problem B: A改错题--植物与花
- OJ嘻唰唰之改错题(4)-C++继承
- OJ第二批——Problem I:改错题:类中私有成员的访问
- OJ第三批——Problem J:C++复数运算符重载(+与<<)
- OJ2560继承(改错题)
- 新手导航
- UASCO Combination Lock 解析 and C 语言实现
- 理解Node.js的事件循环(代码是异步单线程,内部实现用的还是进程和线程,基于池化的线程实现异步)
- 类和对象(二)
- 嘻唰唰第六批Problem G: B 求类中数据成员的最大值-类模板
- OJ第六批——Problem J: B 虚拟继承(虚基类)-沙发床(改错题)
- 第三方框架-01.MJExtension基本使用
- OJ第六批——Problem K: B 抽象类-形状
- afxcmn2.inl line35
- 嘻唰唰第六批Problem H: B 友元类-计算两点间距离
- OJ第六批——Problem L: B 构造函数和析构函数
- 嘻唰唰第六批Problem I: B 继承 圆到圆柱体
- Ultra-QuickSort(POJ 2299)---15.7.21
- 1.编程实现“油耗计算机程序”。要求:输入加油金额、公里数、汽油价格,单击“计算”按钮,显示百公里油耗。界面如图: