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!

:)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.