9.12但是刚学c++不太懂怎么完成,不知道怎么用数组传入成员函数算出length,书上也没有代码参考,不知道该怎么弄,以下是我写的,在这基础上怎么改
#include
using namespace std;
class Rectangle
{
public:
Rectangle()
: length(1)
, width(1)
{
}
~Rectangle() {}
float perimeter()
{
return 2 * (length + width);
}
float area()
{
return length * width;
}
void setLength(float l)
{
if ((l >= 0.0) && (l <= 20.0))
{
length = l;
}
else
{
cout << "矩形的长不符合要求" << std::endl;
}
}
void setwidth(float w)
{
if (w >= 0.0 && w <= 20.0)
{
width = w;
}
else
{
cout << "矩形的宽不符合要求" << std::endl;
}
}
float getLength()
{
return length;
}
float getwidth()
{
return width;
}
private:
float length;
float width;
};
int main()
{
Rectangle rect;
cout << "矩形的长:" << rect.getLength() << endl
<< "矩形的宽:" << rect.getwidth() << endl
<< "矩形的周长:" << rect.perimeter() << endl
<< "矩形的面积:" << rect.area() << endl;
float a;
cout << "请输入矩形的长:";
cin >> a;
rect.setLength(a);
cout << "请输入矩形的宽:";
cin >> a;
rect.setwidth(a);
cout << "矩形的长:" << rect.getLength() <<endl
<< "矩形的宽:" << rect.getwidth() << endl
<< "矩形的周长:" << rect.perimeter() << endl
<< "矩形的面积:" << rect.area() << endl;
}