• YTU 2622: B 虚拟继承(虚基类)-沙发床(改错题)


    2622: B 虚拟继承(虚基类)-沙发床(改错题)

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 487  解决: 393

    题目描述

     有一种特殊的床,既能当床(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<<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;
    }

    输入

    依次输入沙发床的重量、长、宽、高  

    输出

    依次输出沙发床的重量、长、宽、高

    样例输入

    200 1.8 1.5 1.2

    样例输出

    weight:200
    length:1.8
    1.5
    height:1.2
    

    提示

    改错思路有多种,只要程序能运行出正确结果,怎样改错都可以

    迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

    #include <iostream>
    using namespace std;
    //家具类Furniture
    class Furniture
    {
    public:
        Furniture(double w)
        {
            weight=w;
        }
        void display()
        {
            cout<<"weight:"<<weight<<endl;
        }
    protected:
        double weight; //家具重量
    };
    //床类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<<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 Furniture
    {
    public:
        SleeperSofa(double we,double l,double wi,double h):Bed(we,l,wi),Sofa(we,h),Furniture(we) { }
        void display()
        {
            cout<<"weight:"<<weight<<endl;
            Bed::display();
            Sofa::display();
        }
    protected:
        double weight=200;
    };
    int main()
    {
        double weight,length,width,height;
        cin>>weight>>length>>width>>height;
        SleeperSofa ss(weight,length,width,height);
        ss.display();
        return 0;
    }
    

  • 相关阅读:
    16个能帮你找到网页设计灵感的网站
    Charts 图表插件
    jquery获取text,areatext,radio,checkbox,select值(转)
    原型开发、模型构建和设计反馈在线工具
    AutoComplete 自动完成插件(2)
    以蓝色为主题的网站设计灵感
    25个必须要学会的jQuery幻灯片插件教程(中)
    25个必须要学会的jQuery幻灯片插件教程(上)
    极具创意和趣味性的网站404错误页面设计集合
    用CSS制作横向菜单
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989519.html
Copyright © 2020-2023  润新知