日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

[YTU]_2622(B 虚拟继承(虚基类)-沙发床(改错题))

發(fā)布時(shí)間:2025/4/16 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [YTU]_2622(B 虚拟继承(虚基类)-沙发床(改错题)) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

?有一種特殊的床,既能當(dāng)床(Bed)用又能當(dāng)沙發(fā)(Sofa)用,所以叫沙發(fā)床(SleeperSofa)。

同時(shí)床和沙發(fā)又是一種特殊的家具(Furniture),具有一切家具的特性。


利用虛擬繼承(虛基類)建立一個(gè)類的多重繼承,沙發(fā)床繼承了床和沙發(fā)的特性。


下面的程序中,在begin到end部分存在語(yǔ)法錯(cuò)誤。請(qǐng)改正錯(cuò)誤,使程序按下面輸入輸出的規(guī)定運(yùn)行。

注意:只提交修改過的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; //床的長(zhǎng)
double width; //床的寬
};

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

//沙發(fā)床
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;
}

輸入

依次輸入沙發(fā)床的重量、長(zhǎng)、寬、高 ?

輸出

依次輸出沙發(fā)床的重量、長(zhǎng)、寬、高

樣例輸入

200 1.8 1.5 1.2

樣例輸出

weight:200 length:1.8 width:1.5 height:1。。。。 #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; }; class Sofa:virtual public Furniture { public:Sofa(double w,double h):Furniture(w),height(h){}void display(){cout<<"height:"<<height<<endl;} private: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; }

總結(jié)

以上是生活随笔為你收集整理的[YTU]_2622(B 虚拟继承(虚基类)-沙发床(改错题))的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。