3.设计并测试一个矩形类(Rectangle),属性为矩形的左下角与右上角的坐标,矩形水平放置。操作为计算矩形的周长和面积,、构造函数、析构函数复制构造函数,并完成测试,并在主函数中对应的语句中注释调用了哪个函数。
时间: 2024-05-06 12:21:32 浏览: 91
没问题,我可以回答这个问题。以下是矩形类(Rectangle)的设计和测试:
```cpp
#include <iostream>
using namespace std;
class Rectangle {
private:
int x1, y1, x2, y2;
public:
Rectangle(int x1=0, int y1=0, int x2=0, int y2=0) {
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
}
Rectangle(const Rectangle& r) {
x1 = r.x1;
y1 = r.y1;
x2 = r.x2;
y2 = r.y2;
}
~Rectangle() {}
int getWidth() const {
return abs(x2-x1);
}
int getHeight() const {
return abs(y2-y1);
}
int getPerimeter() const {
return 2 * (getWidth() + getHeight());
}
int getArea() const {
return getWidth() * getHeight();
}
};
int main() {
Rectangle r1(0, 0, 2, 2);
cout << "width: " << r1.getWidth() << endl;
cout << "height: " << r1.getHeight() << endl;
cout << "perimeter: " << r1.getPerimeter() << endl;
cout << "area: " << r1.getArea() << endl;
Rectangle r2(r1);
cout << "width: " << r2.getWidth() << endl;
cout << "height: " << r2.getHeight() << endl;
cout << "perimeter: " << r2.getPerimeter() << endl;
cout << "area: " << r2.getArea() << endl;
return 0;
}
```
注释调用的函数如下:
* 第7行:默认构造函数
* 第13行:复制构造函数
* 第15行:析构函数
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)