用C加加编写声明一个点类Point,包含了坐标x,y属性.定义构造函数,成员函数getX()得到x,getY()得到y,求出该点到原点的距离.在主函数中定义Point类对象并输出其到原点的距离
时间: 2024-05-13 16:21:01 浏览: 184
c++语言程序设计课件_C语言教程_C++程序设计--类与对象_
```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;
}
```
阅读全文