Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

? asked in 電腦與網際網路程式設計 · 2 decades ago

急~~~C++程式設計藝術(想了很久,不知道怎麼作答)

建立一個具有 length 和 width 屬性的 Rectangle 類別,這會將預設值設定為1。請提供成員涵式,讓它計算矩形的周長和面積。此外,它也提供處理 length 和width 屬性的set 和get涵式。set 涵式應該要要能夠驗證 length 和 width,這都是介於0.0到20.0之間的浮點數。

Update:

謝謝了,但是我是新手,不曉得有更簡單的方式嗎?可以完整的成現出來嗎(會跑出結果的)?感恩了…

3 Answers

Rating
  • 2 decades ago
    Favorite Answer

    class Rectangle

    {

    public:

    Rectangle()

    {

    length = width = 1.0;

    }

    bool setLength(double l)

    {

    if((l < 0.0)||)(l>20.0)) return false;

    length = l;

    return true;

    }

    bool setWidth(double w)

    {

    if((w < 0.0)||)(w>20.0)) return false;

    width = w;

    return true;

    }

    double getLength() { return length; }

    double getWidth() { return width; }

    double area()

    {

    return length * width;

    }

    double getCircumference()

    {

    return (length + width) * 2.0;

    }

    protected:

    double length, width;

    }

    2006-05-22 14:55:25 補充:

    #include <iostream>using namespace std;int main(){double w, l;cin >> w;cin >> l;Rectangle r;r.setLength(5.0);r.setWidth(4.0);cout << r.getCircumference();cout << r.getArea();return 0;}

    2006-05-22 14:56:35 補充:

    如果給了class也接不起來完整的程式,就算作業交了,考試還是不會過吧。

    2006-05-22 14:58:43 補充:

    我是老師的話就上知識家,看到有照抄的就當掉。

  • 2 decades ago

    這題似乎很多人要考...

  • 2 decades ago

    class Rectangle{

    public:

    Rectangle(); //constructor

    float getPerimeter();

    float getArea();

    void setLength(float len);

    float getLength();

    void setWidth(float wid);

    float getWidth();

    private:

    float length;

    float width;

    };

    Rectangle::Rectange() : length(1), width(1)

    {}

    float getPerimeter()

    {

    return (2*width + 2*length);

    }

    float getArea();

    {

    return ( width *length);

    }

    void setLength(float len)

    {

    length = len;

    }

    float getLength();

    {

    return length;

    }

    void setWidth(float wid)

    {

    width = wid;

    }

    float getWidth()

    {

    return width;

    }

    中間再加上檢查值在0-20就好了...大概就這樣吧

Still have questions? Get your answers by asking now.