0

Hi there,

Looking for some advice regarding the following hierarchy structure:

http://i40.tinypic.com/9a1us0.jpg

I'm storing base class pointers in a std::list in my program:

list<Vehicle*>mylist

Is it possible for me to use Pure Virtual functions in the base class?

For example:

virtual int getMPG() = 0;

Seems fine, as all derived types have an _mpg data member.

But, if I use:

virtual int getNoOfDoors() = 0;

I am having problems as Boat objects don't have a _noOfDoors data member. Only those derived from Car do.

So I am wondering, is it not possible for me to use Pure Virtual functions in the base class?

I am accessing all the objects in my program through base class pointers you see.

How does one normally handle this hierarchy?

Thanks very much for any advice!

:)

Edited by Carrots: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

The base class is where you would put pure virtual functions so that all derived classes have to implement them. Although the boat class may not have doors, then getNoOfDoors() would just return 0 so that the virtual function would be implemented in the derived class.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.