• YTU 2920: Shape系列-7


    2921: Shape系列-7

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 156  解决: 129

    题目描述

    小强做的Shape类在本次的测试中出了点状况,发现原来是其中的area函数的问题,请大家根据题意,帮助小强完成改动后的Shape类。
    小强写的各种类
    class Rectangle:public Shape 

    public: 
     Rectangle(int c,double w,double h); 
     double getwidth(); 
     double getheight(); 
     double area(); 
     double price(); 
    protected: 
     double height; 
     double width; 
    }; 
    Rectangle::Rectangle(int c,double w,double h):Shape(c) 

     width=w; 
     height=h; 

    double Rectangle::getwidth() 

     return width; 

    double Rectangle::getheight() 

     return height; 

    double Rectangle::area() 

     return height*width; 

    double Rectangle::price() 

     return height*width*color; 
    }   
    class Circle:public Shape 

    public: 
     Circle(int c,double r); 
     double getradius(); 
     double area(); 
     double price(); 
    protected: 
     double radius; 
    };   
    Circle::Circle(int c,double r):Shape(c) 

     radius=r; 

    double Circle::getradius() 

     return radius; 

    double Circle::area() 

     return PI*radius*radius; 

    double Circle::price() 

     return PI*radius*radius*color; 
    }
    class Triangle:  public Shape
    {
    public:
     Triangle(int c,double b,double h);
     double area();
    protected:
     double base,height;
    };
    Triangle::Triangle(int c,double b,double h):Shape(c)
    {
     base=b;height=h;
    }
    double Triangle::area()
    {
     return 0.5*base*height;
    }
    int main() 

     Shape *pt[3];
     Rectangle rr(1,1,1);
     Circle cc(2,1);
     Triangle tt(2,1,3);
     pt[0]=&rr;
     pt[1]=&cc;
     pt[2]=&tt;
     cout<<"Rectangle area:"<<pt[0]->area()<<endl;
     cout<<"Circle area:"<<pt[1]->area()<<endl;
     cout<<"Triangle area:"<<pt[2]->area()<<endl;
    return 0;  
    }
    提示:不用提交全部程序,只提交补充部分(包括头文件和π的定义)。

    输入

    输出

    小强测试的各个类面积的数据

    样例输出

    Rectangle area:1
    Circle area:3.14
    Triangle area:1.5

    im0qianqian_站在回忆的河边看着摇晃的渡船终年无声地摆渡,它们就这样安静地画下黄昏画下清晨......可怜

    #include<iostream>
    using namespace std;
    #define PI 3.14
    class Shape
    {
    public:
        Shape(int c):color(c) {}
        virtual double area()=0;
        int color;
    };
    class Rectangle:public Shape
    {
    public:
        Rectangle(int c,double w,double h);
        double getwidth();
        double getheight();
        double area();
        double price();
    protected:
        double height;
        double width;
    };
    Rectangle::Rectangle(int c,double w,double h):Shape(c)
    {
        width=w;
        height=h;
    }
    double Rectangle::getwidth()
    {
        return width;
    }
    double Rectangle::getheight()
    {
        return height;
    }
    double Rectangle::area()
    {
        return height*width;
    }
    double Rectangle::price()
    {
        return height*width*color;
    }
    class Circle:public Shape
    {
    public:
        Circle(int c,double r);
        double getradius();
        double area();
        double price();
    protected:
        double radius;
    };
    Circle::Circle(int c,double r):Shape(c)
    {
        radius=r;
    }
    double Circle::getradius()
    {
        return radius;
    }
    double Circle::area()
    {
        return PI*radius*radius;
    }
    double Circle::price()
    {
        return PI*radius*radius*color;
    }
    class Triangle:  public Shape
    {
    public:
        Triangle(int c,double b,double h);
        double area();
    protected:
        double base,height;
    };
    Triangle::Triangle(int c,double b,double h):Shape(c)
    {
        base=b;
        height=h;
    }
    double Triangle::area()
    {
        return 0.5*base*height;
    }
    int main()
    {
        Shape *pt[3];
        Rectangle rr(1,1,1);
        Circle cc(2,1);
        Triangle tt(2,1,3);
        pt[0]=&rr;
        pt[1]=&cc;
        pt[2]=&tt;
        cout<<"Rectangle area:"<<pt[0]->area()<<endl;
        cout<<"Circle area:"<<pt[1]->area()<<endl;
        cout<<"Triangle area:"<<pt[2]->area()<<endl;
        return 0;
    }
    


  • 相关阅读:
    [JZOJ4648] 【NOIP2016提高A组模拟7.17】锦标赛
    [JZOJ4684] 【GDOI2017模拟8.11】卡牌游戏
    [JZOJ4649] 【NOIP2016提高A组模拟7.17】项链
    [JZOJ4682] 【GDOI2017模拟8.11】生物学家
    [JZOJ4639] 【NOIP2016提高组A组7.16】Angel Beats!
    收藏一个bit模板使用实例
    51 Nod 1627瞬间移动(插板法!)
    收藏一个有效的求组合数的模板
    51 Nod 1486 大大走格子
    51nod 1120 机器人走方格V3
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989652.html
Copyright © 2020-2023  润新知