iDevGames Forums
C++ design question - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: C++ design question (/thread-4313.html)



C++ design question - WhatMeWorry - Apr 27, 2006 09:15 AM

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.


C++ design question - Nick - Apr 27, 2006 09:54 AM

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.


C++ design question - Blacktiger - Apr 27, 2006 10:43 AM

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.


C++ design question - WhatMeWorry - Apr 27, 2006 11:01 AM

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
}



C++ design question - Malarkey - Apr 27, 2006 12:01 PM

Yes.

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