描述 定义私有数据成员宽(width)和长(length)。 定义公有成员函数calcircle(定义在类体外),可以实现宽长的参数传递,计算矩形的周长,在函数体内实现宽长和周长的显示,并在主程序中进行宽长的赋值和周长显示的调用。 定义公有成员函数calarea(定义在类体外),可以实现宽长的参数传递,计算矩形的面积,在函数体内实现宽长和周长的显示,并在主程序中进行宽长的赋值和显示面积的调用。 输入 输入宽 输入长 输出 输出宽 输出长 输出周长 输出宽 输出长 输出面积
时间: 2023-05-21 17:04:51 浏览: 140
下面是对应的 C++ 代码:
```cpp
#include <iostream>
using namespace std;
class Rectangle {
private:
double width;
double length;
public:
void calcircle(double w, double l) {
width = w;
length = l;
double perimeter = 2 * (width + length);
cout << "Width: " << width << endl;
cout << "Length: " << length << endl;
cout << "Perimeter: " << perimeter << endl;
}
void calarea(double w, double l) {
width = w;
length = l;
double area = width * length;
cout << "Width: " << width << endl;
cout << "Length: " << length << endl;
cout << "Area: " << area << endl;
}
};
int main() {
Rectangle rect;
double width, length;
cout << "Please enter width: ";
cin >> width;
cout << "Please enter length: ";
cin >> length;
rect.calcircle(width, length);
rect.calarea(width, length);
return 0;
}
```
输入:
```
3.5
4.2
```
输出:
```
Width: 3.5
Length: 4.2
Perimeter: 15.4
Width: 3.5
Length: 4.2
Area: 14.7
```
阅读全文