OJ第三批——Problem Q: B 虛擬繼承(虛基類)-沙發牀(改錯題)

問題及代碼:

Problem Q: B 虛擬繼承(虛基類)-沙發牀(改錯題)

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 356  Solved: 280
[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:200
length:1.8
width:1.5
height: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; //傢俱重量
};

//******************** begin ********************
//牀類Bed
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();
    }
};
//********************* end ********************

int main()
{
    double weight,length,width,height;
    cin>>weight>>length>>width>>height;

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

    return 0;
}


 

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章