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++程式設計藝術(想了很久，不知道怎麼作答)

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.