问题及代码:
/*。
*Copyright(c)2014,烟台大学计算机学院
*All right reserved,
*文件名:test.cpp
*作者:liu_feng_zi_
*完成日期:2015年6月25日
*版本号:v1.0
*问题描述:虚基类————沙发床
*输入描述:
*程序输出:
*/
#include <iostream>
using namespace std;
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:"<<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;
}
运行结果: