qq_20511987 2016-05-19 14:13 采纳率: 33.3%
浏览 1146
已采纳

关于多重继承和虚基类的问题

以下是源代码。
为什么输出的sphere的球心是(0,0,3)而不是(1,2,3)?

 #include <iostream>
using namespace std;

class Point2D
{
protected:
    double x;
    double y;
public:
    Point2D() { x = 0; y = 0; }
    Point2D(double a, double b)
    {
        x = a;
        y = b;
    }
    virtual void show()
    {
        cout << "The 2D point is:(" << x << ',' << y << ')'<<endl;
    }
};

class Circle:virtual public Point2D
{
protected:
    double radius;
public:
    Circle() { radius = 0; }
    Circle(double a, double b, double c) :Point2D(a, b)
    {
        radius = c;
    }
    virtual void show() 
    { 
        cout << "The center of circle is:(" << x << ',' << y << ')' << endl;
        cout << "The radius of circle is:" << radius << endl;
    }
};

class Point3D :virtual public Point2D
{
protected:
    double z;
public:
    Point3D(double a, double b, double c) :Point2D(a, b)
    {
        z = c;
    }
    virtual void show()
    {
        cout << "The 3D point is:(" << x << ',' << y << ',' << z << ')' << endl;
    }
};

class Sphere :public Circle, public Point3D
{
public:
    Sphere(double a, double b, double c, double r) :Point3D(a, b, c)
    {
        radius = r;
    }
    virtual void show()
    {
        cout << "The center of sphere is:(" << x << ',' << y << ','<<z<<')' << endl;
        cout << "The radius of sphere is:" << radius << endl;
    }
};

int main()
{
    Point2D p1(1, 1);
    Circle c1(1, 2, 2);
    Point3D p2(1.5, 2.5, 3.5);
    Sphere s1(1, 2, 3, 4);
    //p1 = c1;
    Point2D* pointer = &p1;
    pointer->show();
    pointer = &c1;
    pointer->show();
    pointer=&p2;
    pointer->show();
    pointer = &s1;
    pointer->show();
    system("pause");
    return 0;
}
  • 写回答

2条回答 默认 最新

  • Valtava 2016-05-19 14:54
    关注

    这里Point2D是Circle和Point3D的父类,会早于它们初始化,由于你没有显式的指定构造函数参数,因此这里Point2D直接默认构造成为(0,0),此时Point2D的构造已经完成,因此Point3D的构造函数中
    Point3D(double a, double b, double c) : Point2D(a,b)
    这部分会被跳过,于是x和y便保留了默认构造时候的(0,0)。
    你可以显式的初始化所有基类

    class Sphere :public Circle, public Point3D
    {
    public:
        Sphere(double a, double b, double c, double r)
            : Point2D(a, b)
            , Circle(a, b, r) 
            , Point3D(a, b, c)
        {
            radius = r;
        }
        virtual void show()
        {
            cout << "The center of sphere is:(" << x << ',' << y << ',' << z << ')' << endl;
            cout << "The radius of sphere is:" << radius << endl;
        }
    };
    
    

    也可以在Circle和Point3D的构造函数中,基类的初始化隐去,与radius或者z一起显式的赋值(只不过这个时候构造已经完成了,你只是在修改值而不是初始化),效果应该是一样的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧