Help with inheritance, derived classes and their functions

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.
Can the inherited Update() function be overloaded in EnemyObject’s .cpp file

You could make Update() a virtual function within MoveableObject and give it class appropriate implementation in each of the derived classes
Thanks for the reply gunnerfunner.


You could make Update() a virtual function within MoveableObject and give it class appropriate implementation in each of the derived classes


Okay cool. I've just read up on that and it seems like quite a powerful feature. I'm a little confused as to how to implement it though as all the explanations I read talk about using pointers to the base class to access the member function of the derived class?? Have I read that correctly? Can't I just access the derived class' implementation directly like so; EnemyObject.Update()?

Also, if I made Update() virtual would that mean I'd have to copy the code into PlayerObject's Update() function (assuming PlayerObject didn't require anything different) to keep it the same?
Last edited on
using pointers to the base class to access the member function of the derived class??

this is one way to leverage the power of polymorphism but you can also call functions with objects directly as in standard (i.e non-inheritance, non-polymorphic) cases
Can't I just access the derived class' implementation directly like so; EnemyObject.Update()?

sure can, see object c in examle program below
if I made Update() virtual would that mean I'd have to copy the code into PlayerObject's Update() function

you can keep it the same as the base class implementation (again see program below for class B) or keep base class + add some extra stuff (see class C below) or give it an entirely new implementation as you wish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

class A {
   public:
      virtual void print(){ std::cout << "print A \n";};
      virtual ~A(){}
};

class B: public A {
    public:
       void print(){ /*cout << "print B" << endl;*/ A::print(); };//class B has same
};

class C: public A {
     public:
        void print(){ /*cout << "print C" << endl;*/ A::print(); std::cout << "And some extra stuff \n"; };
};

int main()
{
   A *a = new C();//base class pointer to derived class object
   B *b = new B();//derived class pointer to derived class object
   C c{};//derived class object

   a->print(); // calls C::print()/
   std::cout << "End of a \n\n";
   b->print(); // calls B::print() which is the same as A::print()/
   std::cout << "End of b \n\n";
   c.print(); //  calls C::print()/
   std::cout << "End of c \n\n";

   delete a; delete b;
}

Ah I see. That makes perfect sense now.

Thank you for the help gunnerfunner, I really appreciate it :)
Also take a look at the following link for under what conditions your base class may need a virtual destructor: http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors
Ouch... that makes my head hurt!

That's important stuff to know though so I'll try to get my head around it and keep it in mind.

Thank you once again gunnerfunner.
Topic archived. No new replies allowed.