C++ design question

Member
Posts: 321
Joined: 2004.10
Post: #1
I’ve been reading up on C++ inheritance and I’m trying to put theory into practice. But I think that I’m trying to put a square peg into a round hole. Here’s a very forced example but I hope it might be instructional. I’ve got the following base and derived class.

Code:
class HorizontallyChallengedUnit
{
public:
   move();
protected:
   double x;    //  x and y position of the game piece.  Move on plane.
   double y;
}

class HorizontallyVariableUnit: public HorizontallyChallengedUnit
{
public:
   move();  // redefine move() to take into consideration the z axis
protected:
   double z;  // these derived game pieces can move in the z dimension as well
}

// OK, here’s the game pieces.

HorizontallyChallengedUnit   ship;
HorizontallyChallengedUnit   tank;

HorizontallyVariableUnit   aircraft;
HorizontallyVariableUnit   submarine;


Here’s some notes from a book I’m currently reading.

“The basic idea is that we can often think of one class as being like another, except for some extensions” –Accelerated C++

“The derived class can add members of it’s own or redefine members from the base class, but it cannot delete any of the base class’ members” -- Accelerated C++


It seems to meet the criteria of inheritance, but in practice is this a pretty silly example?

However if I just put x,y,z within the base class, then don’t I now have to keep track of what kind of unit each piece is so that z can or cannot be updated?

Just genuinely curious and wanted to know other's opinions.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #2
Personally, for one variable, I would include the Z into the base class. Technically you did the example correct, but I wouldn't suggest using a derived class for something like the Z axis or anything real similar to data you already have. A good example of when I would suggest using a derived class would be to create an object class that stores visual information. You then make a derived class that would be a physics object which adds things like mass and such to it.

I'm not sure what the "right" way is (if there is such a thing) but that's my opinion on the matter.
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #3
Personally, I would do something like this:
Code:
class Unit
{
  protected:
  double x;
  double y;
  double z;

public:
  virtual void move()
  { /* Update X, Y, and Z */ }
}

class HorizontallyChallengedUnit: public Unit
{
public:
  virtual void move()
  { /* Update X and Y only */ }
}
That way you have less code.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #4
Just thought of this variation. Unit is now an Abstract class, right?


Code:
class Unit
{
protected:
  double x, y, z;
public:
  virtual void move() = 0;  // pure virtual  

}

class HorizontallyChallengedUnit: public Unit
{
public:
  virtual void move()  // Update X and Y only
}


class HorizontallyVariableUnit: public Unit
{
public:
  virtual void move()  // Update X and Y and Z
}
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #5
Yes.

I do a lot of that in my own projects as well.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Post Reply