c++输入矩形坐标实现一个矩形类求面积周长

#include
#include <math.h>
#include

using namespace std;

class Rectangle{

public:
//在下面的空格声明 Rectangle 类的成员函数
qcodep

private:
//左下角坐标
double _x1;
double _y1;

//右上角坐标
double _x2;
double _y2;

//宽度和高度
double _width;
double _height;

};

//在下面的空格实现 Rectangle 类的成员函数

qcodep

int main(){

double x1, x2, y1, y2;
cin >>x1>>y1>>x2>>y2;
Rectangle r1(x1,y1,x2,y2);

Rectangle r2 = r1;

//在下面的空格按题目要求输出结果

qcodep

return 0;

}

img


#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

class Rectangle
{
public:
    //在下面的空格声明 Rectangle 类的成员函数
    // qcodep

    Rectangle(double, double, double, double);
    void operator=(const Rectangle &rect);

    double area();
    double girth();

private:
    //左下角坐标
    double _x1;
    double _y1;

    //右上角坐标
    double _x2;
    double _y2;

    //宽度和高度
    double _width;
    double _height;
};

//在下面的空格实现 Rectangle 类的成员函数
// qcodep
Rectangle::Rectangle(double x1, double y1, double x2, double y2)
{
    _x1 = x1;
    _y1 = y1;
    _x2 = x2;
    _y2 = y2;
    _width = _x2 - _x1;
    _height = _y2 - _y1;
}

void Rectangle::operator=(const Rectangle &rect)
{
    _x1 = rect._x1;
    _y1 = rect._y1;
    _x2 = rect._x2;
    _y2 = rect._y2;
    _width = rect._width;
    _height = rect._height;
}

double Rectangle::area()
{
    return _width * _height;
}

double Rectangle::girth()
{
    return 2 * (_width + _height);
}

int main()
{
    double x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    Rectangle r1(x1, y1, x2, y2);
    Rectangle r2 = r1;
    //在下面的空格按题目要求输出结果

    // qcodep
    cout << fixed << setprecision(2);
    cout << r1.girth() << endl;
    cout << r1.area() << endl;

    cout << r1.girth() + r2.girth() << endl;
    cout << r1.area() + r2.area() << endl;

    return 0;
}