Hi guys,
I’ve got what is probably a very basic question about inheritance, but I’m relatively new to this so please bear with me.
So, say I have a class called MoveableObject for all the movable objects in my game. I then derive PlayerObject and EnemyObject from MoveableObject.
MoveableObject has Update(float dTime) as a member function, with basic code common to most moveable objects. Pseudocode for example…
1 2 3 4
|
If (MovingUp) { vPosition.y -= speed * dTime };
If (MovingDown) { vPosition.y += speed * dTime };
If (MovingLeft) { vPosition.x -= speed * dTime };
If (MovingRight) { vPosition.x += speed * dTime };
|
Now, let’s say PlayerObject can only move on the X axis. PlayerObject inherits Update() from MoveableObject and this should work because MoveableObject’s Update() function accounts for PlayerObject’s type of movement.
But, let’s say EnemyObject’s movement is calculated by a flocking algorithm, and so its Update() function should look something like this…
|
vPosition += vVelocity * dTime;
|
How does this work? Can the inherited Update() function be overloaded in EnemyObject’s .cpp file, or would I have to either create EnemyObject as a separate base class instead of deriving it from MoveableObject, or alternatively write a second update function unique to it – e.g. EnemyUpdate() – and call that instead?
Thanks.