c++ - 如何在 C++ 中正确访问继承的方法和构造函数?

标签 c++ inheritance constructor

我有一个 Sphere 类,它继承了一个 Point 对象作为中心。当我通过 Sphere 构造函数创建 Sphere 对象时,它始终将中心初始化为 0,0,0,但它会正确设置半径。

访问 Sphere 的 setCenter() 方法也没有影响。我可以有效地更改球体中心点的 X、Y、Z 坐标的唯一方法是调用点的 setX() 等方法。

如果这是一个明显的答案,我深表歉意,但我是 C++ 的新手,正在为过渡而苦苦挣扎。如果我遗漏了任何重要信息,请随时告诉我。相关代码如下:

#include <iostream>
#include <fstream>
#include "MovingSphere.h"

using namespace std;

int main() {

    ifstream inFile("sphere.txt");
    int X, Y, Z, R, DX, DY, DZ;
    if (inFile) {

        inFile >> X >> Y >> Z >> R
            >> DX >> DY >> DZ;
    }
    else {
        cerr << "\neRR: Cannot find input file.\n\n";
    }

    // create a sphere
    Sphere sphereInstance(X, Y, Z, R);
    sphereInstance.setCenter(1, 2, 3);
    sphereInstance.setX(3);

    cout << endl <<
        "X = " << sphereInstance.getX() << endl <<
        "Y = " << sphereInstance.getY() << endl <<
        "Z = " << sphereInstance.getZ() << endl;

return 0;

}

点.cpp

#include "Point.h"

Point::Point() : x(0), y(0), z(0) {   // default constructor (point at 0,0,0)
}

Point::Point(int X, int Y) : x(), y(), z(0) {      // constructor for 2d point
}

Point::Point(int X, int Y, int Z) : x(), y(), z() { // constructor for 3d point
}

// set the points X coordinate (mutator)
void Point::setX(int newX) {
    x = newX;
}
... etc.

点.h

#ifndef POINT_H
#define POINT_H

class Point {

public:
    Point ();                         // default constructor (0,0,0);
    Point(int X, int Y);              // constructor for 2d point
    Point(int X, int Y, int Z);       // constructor for 3d point

    void setX(int newX);              // declaration

    int getX() const;                 // declaration
etc...


private:

    int x, y, z;  // coordinates of point

};

#endif  /* POINT_H */

球体.cpp

#define _USE_MATH_DEFINES
#include <math.h>
#include "Sphere.h"

Sphere::Sphere() : 
    center(), radius(0) {// default constructor (a point at 0,0,0)
}

Sphere::Sphere(int X, int Y, int Z, int R) {     // sphere constructor
    center = Point(X, Y, Z);
    radius = R;
}

// set the sphere's center (mutator)
void Sphere::setCenter(int X, int Y, int Z) {
    center.setX(X);
    center.setY(Y);
    center.setZ(Z);
}
... etc.

球体.h

#ifndef SPHERE_H
#define SPHERE_H

#include "Point.h"

class Sphere: public Point {

public:
    Sphere();          // default constructor (a point at 0,0,0)
    Sphere (int X, int Y, int Z, int R); // sphere constructor

    void setRadius(int newRadius); // declaration
    void setCenter(int X, int Y, int Z); // declaration

    int getRadius() const;         // declaration

private:
    Point center;      // center of sphere
    int radius;        // radius of sphere

};

#endif  /* SPHERE_H */

输出

X = 3
Y = 0
Z = 0

最佳答案

class Sphere: public Point {

这表示一个球体一个点并且从一个点的所有内容开始,包括 X、Y 和 Z 坐标。

private:
   Point center;      // center of sphere

这表示球体一个点,称为中心。与所有点一样,球体具有的这个点也具有 X、Y 和 Z 坐标。

因此,球体既是点又具有点,每个点都具有 X、Y 和 Z 坐标。这可能不是您想要的,当您设置这两个点之一然后获取另一个时,您的代码会失败。选择一个模型并坚持下去。

如果您需要像处理点一样多态地处理球体,则删除 center——在此模型中,球体是也具有半径的点。如果您不需要像对待点一样对待球体,则不要从点继承——在此模型中,球体不是点,而是具有点和半径。

关于c++ - 如何在 C++ 中正确访问继承的方法和构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14726919/

相关文章:

c++ - 应该使用什么优雅的方法回调设计?

actionscript-3 - 覆盖继承的 getter/setter

java - 如何限制子类修改抽象类中方法的范围?

java - 使用 JPA 和 Hibernate 时如何避免父类(super class)查询中的多态性

c++ - 指定不绑定(bind)临时对象的 `const int&`

c++ - CLion 编译时卡住

C++ 异常与错误代理

c++ - 有效地初始化 const std::vector 类成员

c++ - 如何在C++中调用基类的参数化构造方法?

javascript - 为什么有些对象在 IE 中没有构造函数?