Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9256573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:58:33+00:00 2026-06-18T11:58:33+00:00

I have a Sphere class which inherits a Point object for the center. When

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:58:34+00:00Added an answer on June 18, 2026 at 11:58 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
I have a base class called Geometry from which there exists a subclass Sphere
I have a class called Object : class Object { public: Vector pos; float
I have below a code that will plot a sphere, it's proportions are defined
I have such hierarchy: class Sphere; class Cube; class SpherePair; class Entity {}; class
I have a base class, Primitive , from which I derive several other classes--
I have a one problem,I have many Sphere's in my scene with PyOpenGL, but
I have the following drawScene function which draws a sphere. The coords of X,Y
I have defined 2 points on the surface of a sphere using spherical coordinates.
I am rendering a scene in which I have two spheres. I am revolving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.