简体   繁体   中英

How do I properly access inherited methods and constructors in c++?

I have a Sphere class which inherits a Point object for the center. When I create a Sphere object through the Sphere constructor, it always initializes the center to 0,0,0, but it will correctly set the radius.

Accessing the Sphere's setCenter() method also has no impact. The only way I can effectively change the X, Y, Z co-ordinates of the Sphere's center Point is to call the Point's setX() etc. methods.

I apologize if this is a blatantly obvious answer, but I'm new to C++ and struggling with the transition. If I've left out any important information, please don't hesitate to let me know. Here is the relevant code:

MAIN

#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;

}

Point.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.

Point.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 */

Sphere.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.

Sphere.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 */

The Output

X = 3
Y = 0
Z = 0
class Sphere: public Point {

This says a Sphere is a Point and starts with everything a Point has, including an X, Y, and Z coordinate.

private:
   Point center;      // center of sphere

This says a Sphere has a Point, called center . This Point that a Sphere has also has an X, Y, and Z coordinate, as all Points do.

So a Sphere both is a Point and has a Point, each with an X, Y, and Z coordinate. This can't be what you wanted, and your code fails when it sets one of these two Points and then gets the other.. Pick one model and stick to it.

If you need to treat a Sphere like a Point polymorphically, then remove center -- in this model, a Sphere is a Point that also has a radius. If you don't need to treat a Sphere like a Point, then don't inherit from Point -- in this model, a Sphere is not a point but has a Point and a radius.

Your Point 2 and 3 parameter constructors do nothing with the inputs. Change them to

Point::Point(int X, int Y) : x(X), y(Y), z(0) { }

Point::Point(int X, int Y, int Z) : x(X), y(X), z(Z) { }

If center is a Point data member of Sphere , then you should prefer initialization in the constructor initialization list rather than assignment in the body of the constructor:

Sphere::Sphere(int X, int Y, int Z, int R) : center(X,Y,Z), radius(R) { }

I edited a colon into the statement, but need at least 6 characters.

You don't show how Point and Sphere are related, but I guess that Sphere inherits from Point ...

class Point
{
    ...
};

class Sphere : public Point
{
    ...
};

If you want to call eg the base constructor you do it in the initializer list:

Sphere::Sphere(int X, int Y, int Z, int R)
    : Point(X, Y, Z), radius(R)
{
}

As for other functions, if they are in the base class you can use them as if they were member of the child class:

void Sphere::setCenter(int X, int Y, int Z)
{
    setX(X);
    setY(Y);
    setZ(Z);
}

Since Sphere also is a Point (due to the inheritance) you don't need the center member variable.

As others have said, you should clarify exactly how Sphere is related to Point first (either Sphere has-a Point ie member, or Sphere is-a Point ie inheritance).

Your exact problem however is this:

  • Sphere's setCenter() method updates Sphere::center
  • Sphere uses Point's getX/Y/Z(), and these use the x/y/z in Point

That is, setCenter() updates things that have nothing to do with getX/Y/Z().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM