Inheritance, Polymorphism and virtual functions

Hi,

I'm trying to get to grips with Inheritance, Polymorphism and virtual functions, as the title states, now what I was trying to do was a base class Shape with some member functions like draw() (which should draw the shape), info() (which should print info like area and what kind of shape it is) and menu() (which should print a menu and let the user choose to draw the shape or show the info for the shape).

From this base class Shape I try to make two subclasses like twoDimensionalShape and threeDimensional shape. I would like the function menu() to be the same for both subclasses (since it'll pretty much be a loop with a switch statement letting the player choose which other memeber funtion to invoke). How ever the rest of the functions will be individual to each subclass.

all this works fine as long as I create two different objects and access the objects individual member functions like:

Shape myShape
myShape.info()
myShape.draw()

this will work. But if I try to put the objects in a vector<Shape> (adding one threeDimensionalShape and one twoDimensionalShape I will only be able to access the base class's members (they are not purely virtual since if I tried that I couldn't put the different subclasses in a vector<Shape>)

I was under the impression that something like this was supposed to be possible. Could someone maybe explain if it works and how?

What I would like would be to use a vector with different objects (of the subclasses) which I could walk through and access for example the function draw and the function would draw something different everytime depending on which object is at that index.

like:
vector<Shape> myShapes; /** vector with one twoDimentionalShape and one threeDimensionalShape **/

// invoking function draw for different objects returns different result
for (int i = 0; i < myShapes.size(); i++)
myShapes.at(i).draw();

Thanks in advance!
Last edited on
You need to make your vector a vector<Shape*>.

What is happening is called object slicing. Suppose you have Circle derived from Shape.
When you attempt to store a Circle in your vector, because the vector has room enough only
to store a Shape, and sizeof( Circle ) > sizeof( Shape ) because Circle has some data members,
you end up "slicing" just the Shape portion of the Circle object and storing that.

Ok I see. But how come the object won't be sliced if I use a vector with pointers to Shape objects? Is that because a pointer to an object is always the same size?

Thank you very much for your reply, it helped alot!
Yes, roughly speaking. I have to be able to store pointers to a base class type, otherwise
polymorphism is broken.
Ok so it's a little more complicated then, no need to go further for a beginner, I'm happy with roughly :)
Tried it out with pointers to the base class and it works like a charm (after I remembered to use the -> operator (don't know what it's called) to access the member functions). Thanks again for your help!
Topic archived. No new replies allowed.