在平面直角坐标系中,点类Point包括两个数据成员:x(横坐标),y(纵坐标);若干成员函数,要求设计成员函数计算两点间距离。设计main()函数,以点(0,0)和(3,4)作为测试数据,测试程序。 注:以点(0,0)和(3,4)作为测试数据,求出它们之间的距离。
时间: 2024-06-01 12:14:11 浏览: 16
答案:两点间距离为5。
代码如下:
#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
double x; // 横坐标
double y; // 纵坐标
public:
Point(double x, double y) {
this->x = x;
this->y = y;
}
double distanceTo(Point p) {
double dx = p.x - x;
double dy = p.y - y;
return sqrt(dx * dx + dy * dy);
}
};
int main() {
Point p1(0, 0);
Point p2(3, 4);
double distance = p1.distanceTo(p2);
cout << "两点间距离为:" << distance << endl;
return 0;
}
相关问题
在平面直角坐标系中,点类Point包括两个数据成员:x(横坐标),y(纵坐标);若干成员函数,要求使用成员函数计算两点间距离。设计main()函数,以点(0,0)和(3,4)作为测试数据,测试程序。
好的,我明白了。首先,让我依次回答您的问题。
对于您的问题,实现两点间距离的方法有很多种,这里我们可以采用勾股定理,即:
distance = sqrt((x2-x1)^2 + (y2-y1)^2)
具体实现可以在类Point中添加一个成员函数double getDistance(Point p),可以像下面这样实现:
```
#include <cmath>
#include <iostream>
using namespace std;
class Point {
public:
// 构造函数
Point(double a = 0, double b = 0) : x(a), y(b) {}
// 计算两点间距离
double getDistance(Point p) {
double dx = x - p.x;
double dy = y - p.y;
return sqrt(dx * dx + dy * dy);
}
private:
double x, y;
};
int main() {
Point p1(0,0), p2(3,4);
double dist = p1.getDistance(p2);
cout << "Distance: " << dist << endl;
return 0;
}
```
以上代码中,类Point中的getDistance()函数用于计算两点间距离,实现方式是勾股定理。在主函数中,定义了两个点P1和P2,分别为(0,0)和(3,4),通过P1调用getDistance()函数,传入P2,计算出两点间距离并输出即可。
希望能够帮助您解决问题,如果还有其他问题,欢迎随时提出。
定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数。 定义一个函数Distance(), 用于求两点之间的距离。
好的,这是一个很基础的面向对象编程问题。下面是一个可能的实现:
```cpp
class Point {
public:
Point(double x_, double y_): x(x_), y(y_) {}
double getX() const {
return x;
}
double getY() const {
return y;
}
void setX(double x_) {
x = x_;
}
void setY(double y_) {
y = y_;
}
double distance(const Point& p_other) const {
return sqrt(pow(x - p_other.x, 2) + pow(y - p_other.y, 2));
}
private:
double x;
double y;
};
```
这个类中有两个私有数据成员 `x` 和 `y`,分别表示点的 x 和 y 坐标。有两个公有的 getter 和 setter 函数可以获取和修改这两个数据成员。此外,还有一对成员函数 `distance`,用于计算当前点与另一个点之间的欧几里得距离。
比如,如果要计算从 p1 到 p2 的距离,可以这么写:
```cpp
Point p1(0, 0);
Point p2(3, 4);
double dist = p1.distance(p2); // dist = 5
```
这只是一个简单的实现,还有很多可以改进的地方,比如把 `distance` 函数写成一个全局函数或者使用运算符重载等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)