定义点POINT 类,在该类中定义点的坐标,定义线做类,可通过线类中的成员函数计算该类中二点间的距离

1个回答

  • #include #include using namespace std; class Line; // 预声明线类 class Point { public:Point(int x,int y) { m_x = x; m_y = y; } private:int m_x; int m_y; friend class Line; // 声明Line类为Point的友元类 }; class Line { public:Line(int x = 0,int y = 0) :m_ptOri(x,y) ,m_ptEnd(x,y) { } void SetPoints(const Point &ptOri,const Point &ptEnd) { m_ptOri.m_x = ptOri.m_x; m_ptOri.m_y = ptOri.m_y; m_ptEnd.m_x = ptEnd.m_x; m_ptEnd.m_y = ptEnd.m_y; } double GetLength() { // 使用使用勾股定理求斜边长度 return sqrt(pow((double)abs(m_ptOri.m_x - m_ptEnd.m_x),2) + pow((double)abs(m_ptOri.m_y - m_ptEnd.m_y),2)); } private:friend class Point; Point m_ptOri; Point m_ptEnd; }; void main() { Line l1; l1.SetPoints(Point(15,45),Point(18,49)); cout