I have a problem designing a class that will allow me to draw objects of various shapes.
- Shape is the base class
- Triangle, Square, Rectangle are derived classes from
Shape
class - I have a
vector<Shape*> ShapeCollection
that stores the derived objects i.e.Triangle,Square, Rectangle
- Once I pick the an object from the vector I need to draw the object onto the screen.
At this point I am stuck at what the design of a class should be where as a single 'Drawing' class will do the drawing, consuming an object of 'Shape' class. As the vector will contain different objects of the same base class Shape
. As I have a thread that picks up an object from the vector and once I have an object I must be able to draw it properly.
So more or less below is what I say
class Drawing
{
public:
void Draw(Shape* shape, string objectName)
{
// Now draw the object.
// But I need to know which Object I am drawing or use
// switch statements to identify somehow which object I have
// And then draw. I know this is very BAD!!!
// e.g.
switch(objectName)
{
case "rectangle":
DrawRectangle((Rectangle*) shape)
break;
//Rest of cases follow
}
}
}
Where as I will have a DrawSquare, DrawTriangle function which will do the drawing.
This must be something that has been solved. There must be a better way of doing this as all this switch statement has to go away somehow!
Any guidance is much appreciated.
Thanks
@Adrian and @Jerry suggested to use virtual function, I thought of it, but I need to have my Drawing away from the base class Shape