Sign in to follow this  
thedodgeruk

virtual funtions??

Recommended Posts

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 ???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this