用c++设计一个Point类,表示二维坐标系中的任意一点,然后在此基础上设计一个矩形Rectangle,

编程入门 行业动态 更新时间:2024-10-12 18:18:46

用c++设计一个Point类,表示二维<a href=https://www.elefans.com/category/jswz/34/1768665.html style=坐标系中的任意一点,然后在此基础上设计一个矩形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,

本文发布于:2024-03-07 01:20:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1716474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:坐标系   矩形   在此基础上   Point   Rectangle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!