用C++语言设计一个程序,写出满足以下要求的完整代码。1.定义私有数据成员宽(width)和长(length)。 2.定义公有函数set设置宽长(定义在类体外),可以实现对宽长的设置,在主程序中设置宽长的量。 3.定义公有函数show显示长宽(定义在类体外),可以实现对宽长的显示,在主程序中显示宽长。 4.定义公有成员函数calcircle(定义在类体外,无参数传递),计算矩形的周长,在参数体内实现周长的显示,并实现主程序的调用。 5.定义公有成员函数calarea(定义在类体外,无参数传递),计算矩形的面积,在参数体内实现周长的显示,并实现主程序的调用。
时间: 2023-05-13 13:07:24 浏览: 67
以下是满足要求的完整代码:
#include <stdio.h>
typedef struct {
int width;
int length;
} Rectangle;
void set(Rectangle *rect, int width, int length) {
rect->width = width;
rect->length = length;
}
void show(Rectangle *rect) {
printf("Width: %d\nLength: %d\n", rect->width, rect->length);
}
void calcircle(Rectangle *rect) {
int perimeter = 2 * (rect->width + rect->length);
printf("Perimeter: %d\n", perimeter);
}
void calarea(Rectangle *rect) {
int area = rect->width * rect->length;
printf("Area: %d\n", area);
}
int main() {
Rectangle rect;
set(&rect, 5, 10);
show(&rect);
calcircle(&rect);
calarea(&rect);
return 0;
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)