回覆列表
  • 1 # 發發發狗

    #include<iostream>

    #include<string>

    using namespace std;

    class Shape {

    public:

    Shape() {}

    virtual ~Shape(){}

    virtual float getArea() const { return 0; }

    };

    //長方形

    class Rectangle :public Shape {

    public:

    Rectangle(float Length, float Width) :length(Length), width(Width) {}

    float getArea() const { return length*width; }

    float getLength() const { return length; }

    float getWidth() const { return width; }

    void setLength(float Length) { length = Length; }

    void setWidth(float Width) { width = Width; }

    private:

    float length;

    float width;

    };

    //圓形

    class Circle :public Shape {

    public:

    Circle(float Radius) :radius(Radius){}

    /*Circle() {}*/

    float getArea() const { return radius*radius*3.14; }

    float getRadius() const { return radius; }

    void setRadius(float Radius) { radius = Radius; }

    private:

    float radius;

    };

    //正方形

    class Sqare :public Rectangle {

    public:

    Sqare(float Side) :Rectangle(Side, Side){}

    void setSide(float Side) //設定邊長

    {

    setLength(Side);

    setWidth(Side);

    }

    float getSide() const //獲取邊長

    {

    return getWidth();

    }

    float getArea() const { return Rectangle::getArea(); }

    };

    void main()

    {

    Shape *p;

    p = new Circle(5);

    cout << "area of circle:" << p->getArea()<<endl;

    p = new Rectangle(4, 8);

    cout << "area of Rec:" << p->getArea() << endl;

    }

  • 中秋節和大豐收的關聯?
  • 《帶爸爸去留學》的劇情,如果現實中有人給別人養了十九年孩子,知道後他該怎麼辦?