virtual funtions??

Started by
2 comments, last by SriLumpa 13 years, 4 months ago
i have a base class which has some base funtions like

virtual void AI();
virtual void Move();


when i inherit from this do i have to include in the .h and .cpp file of the inherited
void AI();
void Move();

some dont have any AI();
is this the only way to do it ?

is there a better way around this ???
Advertisement
Hi thedodgeruk.

To inherit from a class you should include its header file.

You generally never include cpp files.

It's not the only way to do it - for example you could do it using composition. Although it's hard to say if it is better or not - it depends on many things.
If some of your subclasses don't need to do anything, you can do one of two things:
  1. Provide a default implementation in the base class, which can be simply an empty function
  2. Provide an empty implementation in the subclasses.
I think the OP is asking if he has to redefine the methods in the derived class.
This depends on whether they have a definition in the base class.

"some dont have any AI" : then the derivation meaning "is a" is probably incorrect. What are the names of your classes ?

You should show some code. Does your first question come from a compilation error you get ?

This topic is closed to new replies.

Advertisement