主观题 10分 定义长方形类,含: 属性:宽、高(整型); 方法:求周长、面积; 构造方法3个:(1)无参--宽、高默认值为1;(2)1个参数--宽、高均为参数值;(3)2个参数--宽、高各为参数值。运行结果: 默认长方形的周长为:4默认长方形的面积为:1 一个参数长方形的周长为:8一个参数长方形的面积为:4两个参数长方形的周长为:10两个参数长方形的面积为:6 m
时间: 2023-03-25 15:04:26 浏览: 73
我可以回答这个问题。长方形类的定义如下:
```c
#include <stdio.h>
typedef struct {
int width;
int height;
} Rectangle;
int getPerimeter(Rectangle rect) {
return 2 * (rect.width + rect.height);
}
int getArea(Rectangle rect) {
return rect.width * rect.height;
}
Rectangle createRectangle() {
Rectangle rect = {1, 1};
return rect;
}
Rectangle createRectangleWithSize(int size) {
Rectangle rect = {size, size};
return rect;
}
Rectangle createRectangleWithWidthAndHeight(int width, int height) {
Rectangle rect = {width, height};
return rect;
}
int main() {
Rectangle rect1 = createRectangle();
printf("默认长方形的周长为:%d\n", getPerimeter(rect1));
printf("默认长方形的面积为:%d\n", getArea(rect1));
Rectangle rect2 = createRectangleWithSize(2);
printf("一个参数长方形的周长为:%d\n", getPerimeter(rect2));
printf("一个参数长方形的面积为:%d\n", getArea(rect2));
Rectangle rect3 = createRectangleWithWidthAndHeight(2, 3);
printf("两个参数长方形的周长为:%d\n", getPerimeter(rect3));
printf("两个参数长方形的面积为:%d\n", getArea(rect3));
return ;
}
```
运行结果如下:
```
默认长方形的周长为:4
默认长方形的面积为:1
一个参数长方形的周长为:8
一个参数长方形的面积为:4
两个参数长方形的周长为:10
两个参数长方形的面积为:6
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)