Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Shape;
- typedef std::unique_ptr<Shape> shape_ptr;
- class Shape{
- public:
- Shape(){};
- virtual ~Shape(){};
- virtual void draw() const = 0;
- virtual float area() const = 0;
- virtual shape_ptr clone() const = 0;
- virtual shape_ptr create() const = 0;
- };
- class Rectangle:public Shape{
- public:
- Rectangle(int height=0, int width=0):m_Height(height),m_Width(width){};
- ~Rectangle(){};
- virtual void draw() const;
- virtual float area() const;
- virtual shape_ptr clone() const{ return shape_ptr(new Rectangle(*this)); };
- virtual shape_ptr create() const{ return shape_ptr(new Rectangle()); };
- private:
- int m_Height;
- int m_Width;
- };
- class Circle:public Shape{
- public:
- Circle(float radius=0):m_Radius(radius){};
- ~Circle(){};
- virtual void draw() const;
- virtual float area() const;
- virtual shape_ptr clone() const{ return shape_ptr(new Circle(*this)); };
- virtual shape_ptr create() const{ return shape_ptr(new Circle()); };
- private:
- float m_Radius;
- };
- class ShapeContainer{
- public:
- typedef std::unique_ptr<Shape> ShapePtr;
- typedef std::vector<ShapePtr> ShapePtrContainer;
- ShapeContainer(){};
- ShapeContainer( ShapeContainer && );
- ShapeContainer( const ShapeContainer & );
- ~ShapeContainer(){};
- const ShapePtr & at(int ) const;
- ShapeContainer & operator = (ShapeContainer&&);
- int size() const{ return static_cast<int>(m_vect.size()); };
- friend ShapeContainer createBasicShapes();
- friend ShapeContainer createComplexShapes();
- friend void swap(ShapeContainer& x, ShapeContainer& y) {x.m_vect.swap(y.m_vect);}
- private:
- ShapePtrContainer m_vect;
- };
- class Foo{
- public:
- Foo(){};
- Foo( const ShapeContainer & );
- ~Foo(){};
- void drawShapes() const;
- private:
- ShapeContainer m_container;
- };
- void Rectangle::draw() const{
- std::cout << "draw rectangle!\n";
- }
- float Rectangle::area() const{
- float area = static_cast<float>(m_Height*m_Width);
- std::cout << "this is the Rectangle's area: " << area << "\n";
- return area;
- }
- void Circle::draw() const{
- std::cout << "draw circle!\n";
- }
- float Circle::area() const{
- float area = static_cast<float>(m_Radius*2*3.1415);
- std::cout << "this is the Circle's area: " << area << "\n";
- return area;
- }
- ShapeContainer::ShapeContainer(ShapeContainer && other)
- :m_vect(std::move(other.m_vect))
- {
- }
- ShapeContainer & ShapeContainer::operator = (ShapeContainer&& other){
- m_vect = std::move(other.m_vect);
- return (*this);
- }
- ShapeContainer::ShapeContainer( const ShapeContainer & other ){
- for(int i=0; i<(int)other.m_vect.size(); i++){
- m_vect.push_back(other.m_vect[i]->clone());
- }
- }
- ShapeContainer createBasicShapes(){
- ShapeContainer cont;
- cont.m_vect.push_back(ShapeContainer::ShapePtr(new Circle()));
- cont.m_vect.push_back(ShapeContainer::ShapePtr(new Rectangle()));
- return cont;
- }
- ShapeContainer createComplexShapes(){
- ShapeContainer cont;
- cont.m_vect.push_back(ShapeContainer::ShapePtr(new Rectangle()));
- return cont;
- }
- const ShapeContainer::ShapePtr & ShapeContainer::at( int index ) const{
- return m_vect[index];
- }
- Foo::Foo( const ShapeContainer & cont )
- :m_container(cont)
- {
- }
- void Foo::drawShapes() const{
- for(int i=0; i < m_container.size(); i++){
- m_container.at(i)->draw();
- }
- }
- int main(int argc, char ** argv){
- Foo myfoo(createBasicShapes());
- myfoo.drawShapes();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement