可以看看我的c++程序吗

我想知道这样子写对不对😭
构造一个矩形类Rectangle,数据成员为矩形的左下角与右上角的坐标,并利用成员函数实现对矩形周长与面积的计算。

img

头一次看到在构造方法里面写输入的,原来还可以这样吗
构造方法别这样写吧,我帮你改一下

#include <iostream>
using namespace std;

class Rectangle {
private:
    double x1, y1,x2, y2;
public:
    //无参构造
    Rectangle() {
        x1 = 0;
        y1 = 0;
        x2 = 2;
        y2 = 2;
    }

    //有参构造
    Rectangle(double x1, double y1, double x2, double y2) {
        this->x1 = x1;
        this->y1 = y1;
        this->x2 = x2;
        this->y2 = y2;
    }

    void perimeter() {
        cout << "矩形的周长为:" << 2 * (x2 - x1) + 2 * (y2 - y1) << endl;
    }

    void area() {
        cout << "矩形的面积为:" << (x2 - x1) * (y2 - y1) << endl;
    }
};

int main()
{
    Rectangle A;
    A.area();
    A.perimeter();

    Rectangle B(0, 0, 6, 6);
    B.perimeter();
    B.area();
    
    return 0;
}

这样你创建对象的时候可以不传参,也可以传参创造一个自己想要的矩形

程序执行结果如下

img