123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include <iostream>
- #include <cmath>
- // 定义二维平面坐标点类
- class Point {
- protected:
- double x, y;
- public:
- // 构造函数1:创建原点(0, 0)
- Point() : x(0), y(0) {}
- // 构造函数2:带参数
- Point(double x, double y) : x(x), y(y) {}
- double getx() const {
- return x;
- }
- double gety() const {
- return y;
- }
- double dist(const Point& p) const {
- return std::sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
- }
- };
- // 定义三维空间坐标点类,继承自二维平面坐标点类
- class Point3D : public Point {
- private:
- double z;
- public:
- // 构造函数1:创建原点(0, 0, 0)
- Point3D() : Point(), z(0) {}
- // 构造函数2:带参数
- Point3D(double x, double y, double z) : Point(x, y), z(z) {}
- double getz() const {
- return z;
- }
- double dist(const Point3D& p) const {
- return std::sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y) + (z - p.z) * (z - p.z));
- }
- };
- int main() {
- // 创建A点和B点
- Point3D A(0, 0, 0);
- Point3D B(4, 5.6, 7.8);
- // 计算A点和B点之间的距离并输出
- double distance = A.dist(B);
- std::cout << "|A-B|=" << distance << std::endl;
- return 0;
- }
|