Objectives

Setup

STL

Shape Classes

#include "util.h"

void vectest();
void listtest();
void dequetest();
void settest();
void maptest();
void stacktest();
void queuetest();
void priorityqueuetest();
void iteratortest();

void polytest1();

int main()
{
/*  vectest();
  listtest();
  dequetest();
  settest();
  maptest();
  stacktest();
  queuetest();
  priorityqueuetest();
  iteratortest();*/

  polytest1();
}
void polytest2()
{
  Shape* shapes[2];
  shapes[0] = new Ellipse(Point (1,1), 10);
  shapes[1] = new Rectangle(Point(2,2), 20, 10);

  for (int i=0; i<2; i++)
  {
    shapes[i]->draw();
  }
}
  foreach (Shape *s, shapes)
  {
    s->draw();
  }

list of shapes

void polytest3()
{
  Ellipse   e(Point (1,1), 10);
  Rectangle r(Point(2,2), 20, 10);

  list <Shape> shapeList;

  shapeList.push_back(e);
  shapeList.push_back(r);

  foreach (Shape &amp;s, shapeList)
  {
    s.draw();
  }
}
  virtual void draw()=0;
  list <Shape*> shapeList;

  shapeList.push_back(new Ellipse(Point (1,1), 10));
  shapeList.push_back(new Rectangle(Point(2,2), 20, 10));

  foreach (Shape *s, shapeList)
  {
    s->draw();
  }
}
template <typename T>
struct mylist : public list<T>
{
  mylist()
  {}

  virtual mylist()
  {
    foreach (T &amp;s, *this)
    {
     delete s;
    }
  }
};
  mylist <Shape*> shapeList;

  shapeList.push_back(new Ellipse(Point (1,1), 10));
  shapeList.push_back(new Rectangle(Point(2,2), 20, 10));

  foreach (Shape *s, shapeList)
  {
    s->draw();
  }

Ref2

#pragma once

template < class T >
class Ref2
  {
  public:
    Ref2(const T &amp;s)                           {KillData = true;  t = s.clone();}
    Ref2(T *s)                                 {KillData = false; t = s;}
    Ref2(const Ref2 < T > &amp;r)                  {KillData = true;
                                                t = r.t?r.t->clone():NULL;}
    Ref2()                                    {if (t &amp;&amp; KillData) delete t;}
    Ref2&amp; operator= (const Ref2 < T > &amp; r)     {if (t &amp;&amp; KillData) delete t;
                                                KillData = true;
                                                t = r.t?r.t->clone():NULL;
                                                return *this; }
    T* operator->() const                      {return t;}
    int operator< (const Ref2 < T > &amp; r) const {return t?r.t?(*t) < (*r.t):false:true;}
    operator T&amp;()   const                      {return *t;}
    operator T*()   const                      {return t;}
    T&amp; operator*()  const                      {return *t;}
  protected:
    T *t;
  private:
    bool KillData;
  };
  list <Ref2 <Shape> > shapeList;
  shapeList.push_back(Ellipse(Point (1,1), 10));
  shapeList.push_back(Rectangle(Point(2,2), 20, 10));

  foreach (Shape *s, shapeList)
  {
    s->draw();
  }
  // Shape...
  virtual Shape* clone() const
  {return 0;};

  //... Ellipse...
  Shape* clone() const
  {
   return new Ellipse(origin, radius);
  }

  //... Rectangle..
  Shape* clone() const
  {
    return new Rectangle(origin, width, height);
  }
  static Ellipse persistentEllipse(Point(20,20), 22);
  shapeList.push_back(&amp;persistentEllipse);
  list <Ref2 <Shape> > shapeList;
  shapeList.push_back(new Ellipse(Point(20,20), 22));

ptr_container

#include <boost/ptr_container/ptr_list.hpp>
using namespace boost;
  ptr_list <Shape>  shapeList;

  shapeList.push_back(new Ellipse(Point (1,1), 10));
  shapeList.push_back(new Rectangle(Point(2,2), 20, 10));

  ptr_list <Shape>::iterator i;
  for (i=shapeList.begin(); i##=shapeList.end(); i++)
    i->draw();

  foreach (Shape &amp;s, shapeList)
  {
    s.draw();
  }

Exercises

STL Lab Archive

Exercise 1: ptr_container

Exercise 2: Camera