#include<iostream>
#include<cmath>
using namespace std;
class Point {
float x;
float y;
public:
Point() //显示定义一个默认构造函数
{
x = 0;
y = 0;
}
Point(float x, float y) {
this->x = x;
this->y = y;
}
float getx() {
return x;
}
float gety() {
return y;
}
};
class Rectangle {
Point TopLeft;
Point RightBottom;
public:
Rectangle() {
TopLeft = Point();
RightBottom = Point();
}
Rectangle(float tlx, float tly, float rbx, float rby) {
TopLeft = Point(tlx, tly);
RightBottom = Point(rbx, rby);
}
double Area() {
return abs((TopLeft.getx() - RightBottom.getx()) * (TopLeft.gety() - RightBottom.gety()));
}
double Perimeter() {
return (abs(TopLeft.getx() - RightBottom.getx()) * 2 + abs((TopLeft.gety() - RightBottom.gety())* 2));
}
};
int main() {
float tlx, tly, rbx, rby;
Rectangle rectangle;
cout << "请输入左上点x坐标:";
cin >> tlx;
cout << "请输入左上点y坐标:";
cin >> tly;
cout << "请输入右上点x坐标:";
cin >> rbx;
cout << "请输入右上点y坐标:";
cin >> rby;
rectangle = Rectangle(tlx, tly, rbx, rby);
cout << "周长:" << rectangle.Perimeter() << "面积:" << rectangle.Area() << endl;
}
C++定义点和矩形求矩形面积周长
最新推荐文章于 2022-05-06 12:09:58 发布
该程序定义了一个`Point`类表示点,并创建了`Rectangle`类来表示矩形,包含矩形的两个顶点。`Rectangle`类提供了计算面积和周长的方法。用户输入矩形的四个顶点坐标,程序输出矩形的周长和面积。
摘要由CSDN通过智能技术生成