predawn123 2022-03-22 20:34 采纳率: 100%
浏览 421
已结题

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

  • 写回答

1条回答 默认 最新

  • 浪客 2022-03-23 14:43
    关注
    
    #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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月28日
  • 已采纳回答 4月20日
  • 创建了问题 3月22日