title: Drawing objects - Better class design?
body: <p>I have a problem designing a class that will allow me to draw objects of various shapes.</p>
  
  <ol>
  <li>Shape is the base class</li>
  <li>Triangle, Square, Rectangle are derived classes from <code>Shape</code> class</li>
  <li>I have a <code>vector&lt;Shape*&gt; ShapeCollection</code> that stores the derived objects i.e. <code>Triangle,Square, Rectangle</code></li>
  <li>Once I pick the an object from the vector I need to draw the object onto the screen.</li>
  </ol>
  
  <p>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 <code>Shape</code>. 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. </p>
  
  <p>So more or less below is what I say</p>
  
  <pre><code>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
          }
     }
  }
  </code></pre>
  
  <p>Where as I will have a DrawSquare, DrawTriangle function which will do the drawing.</p>
  
  <p>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!</p>
  
  <p>Any guidance is much appreciated.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>@Adrian and @Jerry suggested to use virtual function, I thought of it, but I need to have my Drawing away from the base class <code>Shape</code></p>