Arrays of dissimilar objects?

I need to create an array of objects of different Classes. How do I declare this?
It's not easy.
The classic, C way is an array of generic pointers (void *). You need someway to remember the type of each element or everything breaks down.
The OOP way requires that all classes derive from from a common class. This class is usually called "Object". The array then needs to hold Object *s, and everything that is added to it must be first casted to Object *.

Why do you need to do this, anyway? There's usually a better option than this.
This is a lab. I have a base class and three derived classes. I have created 3 objects each of the derived classes. I am also going to practice creating a virtual function in the base class, override this in each derived class to print out details. Part of the requirement of the lab is that I call the print method for each from an array.(hope this makes sense) I am rather weak in my understanding of pointers.
Okay, then all the prep work is already done. You just need to make the array of pointers to the base class. The base class should have a virtual (I think) function called print() which the three classes should implement.
Can you give me a visual example (code)?

Lets say the base class is Crayon. Derived classes are Red, Blue, Green.
Topic archived. No new replies allowed.