用c++实现:设计并测试一个矩形类(Rectangle),属性为矩形的左下与右上角的坐标,矩形水平放置。操作为计算矩形周长与面积,直接发我完整程序
时间: 2024-06-16 11:04:40 浏览: 19
当然,下面是用C++实现的矩形类的完整程序:
```cpp
#include <iostream>
class Rectangle {
private:
int x1, y1; // 左下角坐标
int x2, y2; // 右上角坐标
public:
Rectangle(int x1, int y1, int x2, int y2) {
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
}
int getLength() {
return 2 * (x2 - x1 + y2 - y1);
}
int getArea() {
return (x2 - x1) * (y2 - y1);
}
};
int main() {
// 创建一个矩形对象
Rectangle rect(0, 0, 5, 3);
// 计算矩形的周长和面积
int length = rect.getLength();
int area = rect.getArea();
// 输出结果
std::cout << "矩形的周长为:" << length << std::endl;
std::cout << "矩形的面积为:" << area << std::endl;
return 0;
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)