按照下列要求编程:

来源:百度知道 编辑:UC知道 时间:2024/05/20 03:29:50
按照下列要求编程:
(1)定义一个描述矩形的类Rectangle,包括的数据成员有宽(width)和长(length);
(2)计算矩形周长;
(3)计算矩形面积;
(4)改变矩形大小。
通过实例验证其正确性(给定矩形的长和宽,求出周长和面积并显示出来)。
(使用类和对象)
谢了啊,各位!!!!!!!!

#include <iostream>

using namespace std;

class Rectangle{
double width;
double length;
public:
Rectangle(double w = 0, double l = 0)
{
width = w;
length = l;
}
double Circle()
{
return (width + length) * 2;
}
double Area()
{
return width * length;
}
void Change(double w, double l)
{
width = w;
length = l;
}
};

int main()
{
Rectangle obj(1,1);
double width, length;

cout << obj.Circle() << endl;
cout << obj.Area() << endl;

cout << "change Rectangle:\n";
cout << "input new width: ";
cin >> width;
cout << "input new length: ";
cin >> length;

obj.Change(width, length);