
1条回答 默认 最新
南城Flipped 2021-12-08 09:02关注#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; }没细测
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报 编辑记录解决 1无用