求助,用C++写
用C++类来写 #include <cmath.h>class Point
{
double m_x,m_y;
Point(double x,double y)
{
m_x=x,m_y=y;
}
double Distance(const Point &p)
{
return sqrt(fabs(m_x-p.m_x)*fabs(m_x-p.m_x)+fabs(m_y-p.m_y)*fabs(m_y-p.m_y));
}
}; jhq999 发表于 2022-9-23 09:39
public: // <----------------------------------------- #include <iostream>
#include <cmath>
class Point {
public:
double m_x, m_y; // 坐标 x、y
Point(double, double); // 构造函数
double distance(const Point&); // 成员函数:返回两点之间距离
};
Point::Point(double x, double y) : m_x(x), m_y(y) {} // 构造函数
double Point::distance(const Point& p) // 成员函数:返回两点之间距离
{
return sqrt(
fabs(this->m_x - p.m_x) * fabs(this->m_x - p.m_x) +
fabs(this->m_y - p.m_y) * fabs(this->m_y - p.m_y));
}
using std::cout, std::endl;
int main(void) {
Point A(3, 4), B(2, 2);
cout << "两点之间的距离是:" << A.distance(B) << endl;
return 0;
} 傻眼貓咪 发表于 2022-9-23 19:18
public: //
{:5_104:}
页:
[1]