zoukankan      html  css  js  c++  java
  • [YTU]_2916(Shape系列-2)

    Description

    小聪不喜欢小强的Shape类,声称用Shape类做出的形状不真实,于是小聪创建了Rectangle类,并且决定用该类做两个矩形出来,送给好朋友小亮。Rectangle类有整型的数据成员color(小强的Shape类中的color可以继续使用,无需新定义),浮点型的数据成员widthheight,求面积的成员函数area()。但是小聪没有为Rectangle类写构造函数和成员函数,请帮助小聪完成Rectangle类。

    小强写的文件头和Shape类:

    #include<iostream>

    using namespace std;

    class Shape
    {
    public:
    Shape();
    Shape(int c);
    int getcolor();
    double area();
    protected:
    int color;
    };
    Shape::Shape()
    {
    color=0;
    }
    Shape::Shape(int c)
    {
    color=c;
    }
    int Shape::getcolor()
    {
            return color;
    }
    double Shape::area()
    {
           return 10000;
    }
    小聪的测试函数:
    int main()
    {
    Rectangle rr=Rectangle(1,2,3);
    cout<<"Rectangle color:"<<rr.getcolor()<<endl
    <<"Rectangle "<<rr.getwidth()<<endl
    <<"Rectangle height:"<<rr.getheight()<<endl
    <<"Rectangle area:"<<rr.area()<<endl
    <<"Rectangle price:"<<rr.price()<<endl;
    return 0;
    }
    提示:不用提交全部程序,只提交补充部分。

    Input

    Output

    输出小聪创建的矩形的相关数据

    Sample Output

    Rectangle color:1Rectangle 2Rectangle height:3Rectangle area:6Rectangle price:6
    #include<iostream>
    using namespace std;
    class Shape
    {
    public: 
        Shape();
        Shape(int c);
        int getcolor();
        double area();
    protected:
        int color;
    };
    Shape::Shape()
    {
        color=0;
    }
    Shape::Shape(int c)
    {
        color=c;
    }
    int Shape::getcolor()
    {
        return color;
    }
    double Shape::area()
    {
        return 10000;
    }
    class Rectangle:public Shape
    {
    public:
        Rectangle(){}
        Rectangle(int c,int w,int h):Shape(c),width(w),height(h){}
        int getwidth();
        int getheight();
        int area();
        int price();
    private:
        int width,height;
    };
     
    int Rectangle::getwidth()
    {return width;}
    int Rectangle::getheight()
    {return height;}
    int Rectangle::area()
    {return width*height;}
    int Rectangle::price()
    {return width+height+color;}
    int main()
     
    {
        Rectangle rr=Rectangle(1,2,3);
        cout<<"Rectangle color:"<<rr.getcolor()<<endl
            <<"Rectangle "<<rr.getwidth()<<endl
            <<"Rectangle height:"<<rr.getheight()<<endl
            <<"Rectangle area:"<<rr.area()<<endl
            <<"Rectangle price:"<<rr.price()<<endl;
        return 0;
    }
    

  • 相关阅读:
    JAVASCRIPT高程笔记-------JSON与AJAX
    JAVASCRIPT高程笔记-------第十章 DOM对象
    JAVASCRIPT高程笔记-------第八章 浏览器BOM对象
    JAVASCRIPT高程笔记-------第 七章 函数表达式
    JAVASCRIPT高程笔记-------第六章 面向对象的程序设计
    JAVASCRIPT高程笔记-------第五章 引用类型
    javascript高程笔记-------第四章 变量、作用域和内存问题
    redis 从0 到 1 键值相关命令 服务器相关命令
    SnpHub搭建(三) | 手动处理数据后的配置文件填写
    SnpHub搭建 | 数据处理中可能出现的问题
  • 原文地址:https://www.cnblogs.com/sxy201658506207/p/7586313.html
Copyright © 2011-2022 走看看