rectangle c++
时间: 2024-06-13 08:08:57 浏览: 88
下面是一个简单的C++矩形类的例子,其中包括计算矩形面积和周长的方法:
```c++
#include <iostream>
using namespace std;
class Rectangle {
public:
double width;
double height;
double getArea() {
return width * height;
}
double getPerimeter() {
return 2 * (width + height);
}
};
int main() {
Rectangle rect;
rect.width = 5.0;
rect.height = 6.0;
cout << "Area of the rectangle is: " << rect.getArea() << endl;
cout << "Perimeter of the rectangle is: " << rect.getPerimeter() << endl;
return 0;
}
```
相关问题
opencv rectangle c++
可以使用 OpenCV 中的 rectangle 函数来绘制矩形。该函数的语法如下:
void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=);
其中,img 表示要绘制矩形的图像,pt1 和 pt2 分别表示矩形的左上角和右下角坐标,color 表示矩形的颜色,thickness 表示矩形边框的粗细,lineType 表示边框的类型,shift 表示坐标点的小数位数。
例如,下面的代码可以在一张图像上绘制一个红色的矩形:
Mat img = imread("test.jpg");
rectangle(img, Point(100, 100), Point(200, 200), Scalar(, , 255), 2);
这个矩形的左上角坐标为 (100, 100),右下角坐标为 (200, 200),颜色为红色,边框粗细为 2。
设计一个矩形类rectangle C++
好的,以下是一个基本的矩形类的C++实现:
```c++
class Rectangle {
private:
double width;
double height;
public:
// 默认构造函数
Rectangle() {
width = 0;
height = 0;
}
// 带参数构造函数
Rectangle(double w, double h) {
width = w;
height = h;
}
// 获取宽度
double getWidth() const {
return width;
}
// 获取高度
double getHeight() const {
return height;
}
// 设置宽度
void setWidth(double w) {
width = w;
}
// 设置高度
void setHeight(double h) {
height = h;
}
// 计算面积
double getArea() const {
return width * height;
}
// 计算周长
double getPerimeter() const {
return 2 * (width + height);
}
// 判断是否为正方形
bool isSquare() const {
return width == height;
}
};
```
这个矩形类具有宽度和高度属性,并提供了一些方法来获取和设置这些属性,以及计算矩形的面积和周长。此外,还提供了一个方法来判断矩形是否为正方形。你可以根据自己的需要修改或扩展这个矩形类。
阅读全文
相关推荐














