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
This says a Sphere is a Point and starts with everything a Point has, including an X, Y, and Z coordinate.
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.