用c++代码写一个定义一个点类point包含坐标x,y等属性,求出该点到原点的距离,且求距离的函数不能在类中定义
时间: 2023-04-10 14:04:47 浏览: 89
以下是C知道的回答:
可以使用以下代码来定义一个点类point:
```
typedef struct {
double x;
double y;
} point;
```
要计算该点到原点的距离,可以使用以下代码:
```
#include <math.h>
double distance(point p) {
return sqrt(p.x * p.x + p.y * p.y);
}
```
在这个函数中,我们使用了数学库中的sqrt函数来计算平方根,以求出距离。注意,这个函数并没有在类中定义,而是在类外定义的。
希望这个回答能够帮到你!
相关问题
设计一个二维点类,从二维点类派生出三维点类设计访问相关属性的方法以及计算点到坐标原点距离的方法(c++)
好的,以下是设计的代码示例(使用C++语言):
```c++
#include<iostream>
#include<cmath>
using namespace std;
class Point2D {
protected:
double x;
double y;
public:
Point2D(double x, double y) {
this->x = x;
this->y = y;
}
double get_x() {
return x;
}
double get_y() {
return y;
}
double distance_to_origin() {
return sqrt(x * x + y * y);
}
};
class Point3D : public Point2D {
private:
double z;
public:
Point3D(double x, double y, double z) : Point2D(x, y) {
this->z = z;
}
double get_z() {
return z;
}
double distance_to_origin() {
return sqrt(x * x + y * y + z * z);
}
};
int main() {
Point2D p2d(3.0, 4.0);
Point3D p3d(3.0, 4.0, 5.0);
cout << "2D point: (" << p2d.get_x() << ", " << p2d.get_y() << ")" << endl;
cout << "Distance to origin: " << p2d.distance_to_origin() << endl;
cout << "3D point: (" << p3d.get_x() << ", " << p3d.get_y() << ", " << p3d.get_z() << ")" << endl;
cout << "Distance to origin: " << p3d.distance_to_origin() << endl;
return 0;
}
```
以上代码定义了一个二维点类`Point2D`和一个三维点类`Point3D`,其中三维点类`Point3D`继承了二维点类`Point2D`的属性和方法。`Point2D`类有`x`和`y`两个属性,可以通过`get_x()`和`get_y()`方法获取这两个属性的值。`distance_to_origin()`方法可以计算该点到坐标原点的距离。
`Point3D`类除了继承了`Point2D`类的属性和方法外,还有一个`z`属性,可以通过`get_z()`方法获取该属性的值。覆盖了父类的`distance_to_origin()`方法,可以计算该点到坐标原点的距离。
这样设计的好处是,我们可以根据需要方便地扩展出更多的点类,而且不同类的实例可以调用相同的方法名,实现了代码的重用。
声明一个结构体point,包含三个数据成员x、y、z,表示一个点在三维空间的坐标。在主函数中定义一个point类型的结构体变量,并该点到原点的距离。
以下是声明point结构体并计算点到原点距离的代码示例:
```c++
#include <iostream>
#include <cmath>
using namespace std;
struct point {
double x;
double y;
double z;
};
int main() {
point p;
p.x = 3.0;
p.y = 4.0;
p.z = 5.0;
double distance = sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
cout << "The distance between the point (" << p.x << ", " << p.y << ", " << p.z << ") and the origin is " << distance << endl;
return 0;
}
```
在上述代码中,我们定义了一个point结构体,并在主函数中创建了一个point类型的变量p,并为其x、y、z三个成员赋值。接着,我们使用sqrt函数计算了该点到原点的距离,并将结果输出到控制台。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.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)