Advertisement
Guest User

Untitled

a guest
Dec 14th, 2011
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1.     class Shape;
  2.  
  3.     typedef std::unique_ptr<Shape> shape_ptr;
  4.  
  5.     class Shape{
  6.  
  7.         public:
  8.             Shape(){};
  9.             virtual ~Shape(){};
  10.  
  11.             virtual void draw() const = 0;
  12.             virtual float area() const = 0;
  13.  
  14.             virtual shape_ptr clone() const = 0;
  15.             virtual shape_ptr create() const = 0;
  16.     };
  17.  
  18.     class Rectangle:public Shape{
  19.         public:
  20.  
  21.             Rectangle(int height=0, int width=0):m_Height(height),m_Width(width){};
  22.             ~Rectangle(){};
  23.  
  24.             virtual void draw() const;
  25.             virtual float area() const;
  26.  
  27.             virtual shape_ptr clone() const{ return shape_ptr(new Rectangle(*this)); };
  28.             virtual shape_ptr create() const{ return shape_ptr(new Rectangle()); };
  29.  
  30.         private:
  31.             int m_Height;
  32.             int m_Width;
  33.     };
  34.  
  35.  
  36.     class Circle:public Shape{
  37.         public:
  38.  
  39.             Circle(float radius=0):m_Radius(radius){};
  40.             ~Circle(){};
  41.  
  42.             virtual void draw() const;
  43.             virtual float area() const;
  44.  
  45.             virtual shape_ptr clone() const{ return shape_ptr(new Circle(*this)); };
  46.             virtual shape_ptr create() const{ return shape_ptr(new Circle()); };
  47.  
  48.         private:
  49.             float m_Radius;
  50.     };
  51.  
  52.     class ShapeContainer{
  53.  
  54.         public:
  55.  
  56.             typedef std::unique_ptr<Shape> ShapePtr;
  57.             typedef std::vector<ShapePtr> ShapePtrContainer;
  58.  
  59.             ShapeContainer(){};
  60.             ShapeContainer( ShapeContainer && );
  61.             ShapeContainer( const ShapeContainer & );
  62.             ~ShapeContainer(){};
  63.  
  64.             const ShapePtr & at(int ) const;
  65.  
  66.             ShapeContainer & operator = (ShapeContainer&&);
  67.             int size() const{ return static_cast<int>(m_vect.size()); };
  68.  
  69.             friend ShapeContainer createBasicShapes();
  70.             friend ShapeContainer createComplexShapes();
  71.  
  72.             friend void swap(ShapeContainer& x, ShapeContainer& y) {x.m_vect.swap(y.m_vect);}
  73.  
  74.         private:
  75.  
  76.             ShapePtrContainer m_vect;
  77.     };
  78.  
  79.     class Foo{
  80.  
  81.         public:
  82.  
  83.             Foo(){};
  84.             Foo( const ShapeContainer & );
  85.             ~Foo(){};
  86.  
  87.             void drawShapes() const;
  88.  
  89.  
  90.         private:
  91.             ShapeContainer m_container;
  92.  
  93.    
  94.     };
  95.  
  96.     void Rectangle::draw() const{
  97.         std::cout << "draw rectangle!\n";
  98.     }
  99.  
  100.     float Rectangle::area() const{
  101.         float area = static_cast<float>(m_Height*m_Width);
  102.         std::cout << "this is the Rectangle's area: " << area << "\n";
  103.         return area;
  104.     }
  105.  
  106.     void Circle::draw() const{
  107.         std::cout << "draw circle!\n";
  108.     }
  109.  
  110.     float Circle::area() const{
  111.         float area = static_cast<float>(m_Radius*2*3.1415);
  112.         std::cout << "this is the Circle's area: " << area << "\n";
  113.         return area;
  114.     }
  115.  
  116.     ShapeContainer::ShapeContainer(ShapeContainer && other)
  117.         :m_vect(std::move(other.m_vect))
  118.     {
  119.    
  120.     }
  121.  
  122.     ShapeContainer & ShapeContainer::operator = (ShapeContainer&& other){
  123.         m_vect = std::move(other.m_vect);
  124.         return (*this);
  125.     }
  126.  
  127.     ShapeContainer::ShapeContainer( const ShapeContainer & other ){
  128.         for(int i=0; i<(int)other.m_vect.size(); i++){
  129.             m_vect.push_back(other.m_vect[i]->clone());
  130.         }
  131.     }
  132.  
  133.     ShapeContainer createBasicShapes(){
  134.         ShapeContainer cont;
  135.         cont.m_vect.push_back(ShapeContainer::ShapePtr(new Circle()));
  136.         cont.m_vect.push_back(ShapeContainer::ShapePtr(new Rectangle()));
  137.         return cont;
  138.     }
  139.  
  140.     ShapeContainer createComplexShapes(){
  141.         ShapeContainer cont;
  142.         cont.m_vect.push_back(ShapeContainer::ShapePtr(new Rectangle()));
  143.         return cont;
  144.     }
  145.  
  146.     const ShapeContainer::ShapePtr & ShapeContainer::at( int index ) const{
  147.         return m_vect[index];
  148.     }
  149.  
  150.     Foo::Foo( const ShapeContainer & cont )
  151.         :m_container(cont)
  152.     {
  153.    
  154.     }
  155.  
  156.     void Foo::drawShapes() const{
  157.    
  158.         for(int i=0; i < m_container.size(); i++){
  159.             m_container.at(i)->draw();
  160.         }
  161.     }
  162.  
  163.     int main(int argc, char ** argv){
  164.         Foo myfoo(createBasicShapes());
  165.         myfoo.drawShapes();
  166.     }
  167.  
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement