坐标系中的任意一点,然后在此基础上设计一个矩形Rectangle,"/>
用c++设计一个Point类,表示二维坐标系中的任意一点,然后在此基础上设计一个矩形Rectangle,
- Rectangle类使用Point类的两个坐标点作为矩形的对角坐标,并可以输出矩形4个顶点的坐标值和矩形面积。
#include<iostream>
#include<cmath>
using namespace std;
class Point;
class Rectangle
{
private:double x1, y1, x2, y2, s;
public:Rectangle();void peak1(Point&);void peak2(Point&);void area();
};
class Point
{
private:double x1, y1;
public:Point();Point(double m_x1, double m_y1) :x1(m_x1), y1(m_y1){}friend void Rectangle::peak1(Point&);friend void Rectangle::peak2(Point&);
};
Point::Point()
{x1 = y1 = 0;
}
Rectangle::Rectangle()
{s = x1 = x2 = y1 = y2 = 0;
}
void Rectangle::peak1(Point& t1)
{x1 = t1.x1; y1 = t1.y1;
}
void Rectangle::peak2(Point& t2)
{x2 = t2.x1;y2 = t2.y1;cout << "该矩形的四个顶点坐标是:" << endl;cout << "(" << x1 << "," << y1 << ")";cout << "、 (" << x2 << "," << y2 << ")";cout << "、 (" << x1 << "," << y2 << ")";cout << "、 (" << x2 << "," << y1 << ")" << endl;}void Rectangle::area()
{s = fabs(x2 - x1) * fabs(y2 - y1);cout << "矩形面积是:" << s << endl;
}
void main()
{double x1, y1, x2, y2;cout << "请输入两个坐标:";cin >> x1 >> y1 >> x2 >> y2;Point a(x1, y1);Point b(x2, y2);Rectangle c;c.peak1(a);c.peak2(b);c.area();
}
更多推荐
用c++设计一个Point类,表示二维坐标系中的任意一点,然后在此基础上设计一个矩形Rectangle,
发布评论