m0_73307247 2023-04-22 16:31 采纳率: 14.3%
浏览 13
已结题

继承与多态 相关问题 求解!

导入实习 3 的 Point 类,其定义如下。

#include <iostream>
#include<cmath>
using namespace std;
class Point{
public:
Point( doubl newX=0, double newY=0);
Point(const Point& p);
~Point();
void setValue(double newX, double newY);
double getX( ) const;
double getY() const;
double getDistance( cosnt Point& p2) const;
private:
double x, y;
};

现要定义处理 3 维点的类,而又不能直接修改 Point 类,以 Point 类作为基
类派生得到 Point3D,Point3D 的定义和部分功能已经实现,请补充未完成的部
分成员函数定义。要求构造函数通过初始化列表实现。

class Point3D : public Point{
public:
Point3D(double newX=0, double newY=0, double newZ=0);
double getZ() const;
double getDistance( const Point3D& p)const;
private:
double z;
};

【提示】在 Point3D 的 getDistance 方法中,通过类名前缀可以调用基类的
getDistance,计算出 XY 平面内的距离 dis,再通过 sqrt(disdis+dzdz)即可计算
出 3D 空间内点的距离。
测试的主程序如下:

int main(){
Point p1(3, 4), p2(5,3);
Point3D p1_3D(3,4,6);
Point3D p2_3D(2,6,9);
double dis=p1.getDistance(p2); //计算二维点 p1 和 p2 的距离
cout<<"Distance between p1 and p2: "<<dis<<endl;
dis=p1_3D.getDistance(p2_3D); //计算 3 维点 p1_3D 和 p2_3D 的距离
cout<<"Distance between p1_3D and p2_3D: "<<dis<<endl;
return 0;
}

  • 写回答

1条回答 默认 最新

  • 半调子全栈 2023-04-22 16:33
    关注

    下面是 Point3D 类的成员函数定义:

    class Point3D : public Point {
    public:
        Point3D(double newX = 0, double newY = 0, double newZ = 0) : Point(newX, newY), z(newZ) {}
        double getZ() const { return z; }
        double getDistance(const Point3D& p) const {
            double dis2D = Point::getDistance(p);
            double dis3D = sqrt(dis2D * dis2D + pow(z - p.z, 2));
            return dis3D;
        }
    private:
        double z;
    };
    

    在构造函数中使用了初始化列表来初始化基类的 x 和 y 成员变量,同时初始化自身的 z 成员变量。

    在 getDistance 方法中,首先调用基类 Point 的 getDistance 方法,计算出 2D 平面内的距离 dis2D,然后再利用勾股定理计算出 3D 空间内的距离 dis3D,最终返回 dis3D。

    在主程序中可以直接调用 Point 和 Point3D 的 getDistance 方法,计算出两个点之间的距离。完整的程序代码如下:

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    class Point {
    public:
        Point(double newX = 0, double newY = 0) : x(newX), y(newY) {}
        Point(const Point& p) : x(p.x), y(p.y) {}
        ~Point() {}
        void setValue(double newX, double newY) { x = newX; y = newY; }
        double getX() const { return x; }
        double getY() const { return y; }
        double getDistance(const Point& p2) const {
            double dx = x - p2.x;
            double dy = y - p2.y;
            return sqrt(dx * dx + dy * dy);
        }
    private:
        double x, y;
    };
    
    class Point3D : public Point {
    public:
        Point3D(double newX = 0, double newY = 0, double newZ = 0) : Point(newX, newY), z(newZ) {}
        double getZ() const { return z; }
        double getDistance(const Point3D& p) const {
            double dis2D = Point::getDistance(p);
            double dis3D = sqrt(dis2D * dis2D + pow(z - p.z, 2));
            return dis3D;
        }
    private:
        double z;
    };
    
    int main() {
        Point p1(3, 4), p2(5, 3);
        Point3D p1_3D(3, 4, 6);
        Point3D p2_3D(2, 6, 9);
        double dis = p1.getDistance(p2);
        cout << "Distance between p1 and p2: " << dis << endl;
        dis = p1_3D.getDistance(p2_3D);
        cout << "Distance between p1_3D and p2_3D: " << dis << endl;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月30日
  • 已采纳回答 4月22日
  • 创建了问题 4月22日

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目