C++定义坐标并求矩形周长面积

设计一个点类Point,数据成员为x轴坐标和y轴坐标,构造函数初始化数据成员;
然后利用点的类定义长方形类Rect,其中包含2个点p1,p2,分别表示长方形的左上角和右下角的顶点,在长方形类的构造函数中初始化p1,p2,并在类中定义计算长方形面积的函数getArea。
在main函数中,从键盘输入2个点的横纵坐标,创建两个点,然后创建长方形对象,调用函数计算长方形的面积和周长并输出结果。

One possible implementation of this class is shown below:
// Rectangle.hclass Rectangle { private: double width; // width of the rectangle
double height; // height of the rectangle
double x; // x-coordinate of the top-left corner
double y; // y-coordinate of the top-left corner
public: // default constructor that creates a unit square at (0, 0)
Rectangle(); // constructor that creates a rectangle with given width, height and coordinates
Rectangle(double w, double h, double x1, double y1); // accessor methods for data fields
double getWidth() const; double getHeight() const; double getX() const; double getY() const; // mutator methods for data fields
void setWidth(double w); void setHeight(double h); void setX(double x1); void setY(double y1); // method that returns the area of the rectangle
double getArea() const; // method that returns the perimeter of the rectangle
double getPerimeter() const;
};// Rectangle.cpp#include "Rectangle.h"// default constructor that creates a unit square at (0, 0)Rectangle::Rectangle() {
width = 1;
height = 1;
x = 0;
y = 0;
}// constructor that creates a rectangle with given width, height and coordinatesRectangle::Rectangle(double w, double h, double x1, double y1) {
width = w;
height = h;
x = x1;
y = y1;
}// accessor methods for data fieldsdouble Rectangle::getWidth() const { return width;
}double Rectangle::getHeight() const { return height;
}double Rectangle::getX() const { return x;
}double Rectangle::getY() const { return y;
}// mutator methods for data fieldsvoid Rectangle::setWidth(double w) {
width = w;
}void Rectangle::setHeight(double h) {
height = h;
}void Rectangle::setX(double x1) {
x = x1;
}void Rectangle::setY(double y1) {
y = y1;
}// method that returns the area of the rectangledouble Rectangle::getArea() const { return width * height;
}// method that returns the perimeter of the rectangledouble Rectangle::getPerimeter() const { return (width + height) *2;
}

You can use this class to create objects representing rectangles and call their methods to get their properties. For example:
#include <iostream>#include "Rectangle.h"using namespace std;int main(){ // create a rectangle object with width=3, height=4 and coordinates=(2,-5)
Rectangle r(3.0 ,4.0 ,2.0 ,-5.0); // print its area and perimeter
cout << "The area of r is " << r.getArea() << endl;
cout << "The perimeter of r is " << r.getPerimeter() << endl; return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2022-12-29
#include <iostream>
using namespace std;
// 定义点类
class Point {
public:
// 构造函数,用于初始化数据成员
Point(int x, int y) : x(x), y(y) {}
// 数据成员,表示点的横纵坐标
int x, y;
};
// 定义长方形类
class Rect {
public:
// 构造函数,用于初始化点对象
Rect(Point p1, Point p2) : p1(p1), p2(p2) {}
// 计算长方形面积的函数
int getArea() const {
// 计算长方形的宽和高
int width = p2.x - p1.x;
int height = p2.y - p1.y;
// 返回
// 返回长方形的面积
return width * height;
}
private:
// 数据成员,表示长方形的两个顶点
Point p1, p2;
};
int main()
{
// 从键盘输入两个点的横纵坐标
int x1, y1, x2, y2;
cout << "请输入第一个点的横纵坐标:";
cin >> x1 >> y1;
cout << "请输入第二个点的横纵坐标:";
cin >> x2 >> y2;
// 创建两个点对象
Point p1(x1, y1);
Point p2(x2, y2);
// 创建长方形对象
Rect rect(p1, p2);
// 调用函数计算长方形的面积并输出结果
cout << "长方形的面积是:" << rect.getArea() << endl;
return 0;
}
这个程序只是一个简单的例子,实际编写代码时需要注意许多细节,例如数据成员的访问控制、构造函数的重载等。
相似回答