定义点类,包括x.y坐标和相关成员函数
时间: 2023-05-16 20:06:10 浏览: 69
好的,以下是定义点类的代码:
```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 distanceTo(const Point& other) const {
double dx = x_ - other.x_;
double dy = y_ - other.y_;
return sqrt(dx * dx + dy * dy);
}
private:
double x_;
double y_;
};
```
这个类包括了x和y坐标,以及计算两个点之间距离的成员函数。
相关问题
点类point数据成员包括坐标x,坐标y,成员函数包括构造函数,返回坐标x的函数get()h和返回坐标y的函数grty
好的,这是一个关于类的问题,我来给你解答。
根据题意,我们可以定义一个名为`Point`的类,包含私有数据成员`x`和`y`,以及公有成员函数`Point()`(构造函数)、`getX()`(返回`x`的函数)和`getY()`(返回`y`的函数)。具体实现如下:
```c++
class Point {
private:
int x;
int y;
public:
Point(int x, int y) {
this->x = x;
this->y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
};
```
在这个类中,构造函数`Point()`接收两个参数,用于初始化`x`和`y`;`getX()`和`getY()`分别返回`x`和`y`的值。
使用该类创建一个`Point`对象并获取其坐标信息的示例代码如下:
```c++
Point p(1, 2);
cout << p.getX() << ", " << p.getY() << endl; // 输出: 1, 2
```
希望我的回答能够帮到你。
用C加加编写声明一个点类Point,包含了坐标x,y属性.定义构造函数,成员函数getX()得到x,getY()得到y,求出该点到原点的距离.在主函数中定义Point类对象并输出其到原点的距离
```cpp
#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
double x, y;
public:
Point(double x, double y) {
this->x = x;
this->y = y;
}
double getX() {
return x;
}
double getY() {
return y;
}
double distanceToOrigin() {
return sqrt(x * x + y * y);
}
};
int main() {
Point p(3, 4);
cout << "The distance from point (" << p.getX() << ", " << p.getY() << ") to origin is " << p.distanceToOrigin() << endl;
return 0;
}
```