Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <memory>
- #include <iostream>
- class Shape;
- typedef std::unique_ptr<Shape> shape_ptr;
- class Shape{
- public:
- Shape(){};
- virtual ~Shape(){std::cout << "Shape destructor: " << this << std::endl;};
- virtual void draw() const = 0;
- virtual float area() const = 0;
- virtual shape_ptr clone() const = 0;
- virtual shape_ptr create() const = 0;
- static shape_ptr defineRectangle(int, int );
- static shape_ptr defineCircle(float);
- };
- class Rectangle:public Shape{
- public:
- typedef std::unique_ptr<Rectangle> rectangle_SmartPtr;
- Rectangle(int height=0, int width=0):m_Height(height),m_Width(width){};
- Rectangle(const Rectangle & rect):m_Height(rect.m_Height),m_Width(rect.m_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:
- typedef std::unique_ptr<Circle> circle_SmartPtr;
- Circle(float radius=0):m_Radius(radius){};
- Circle(const Circle & other):m_Radius(other.m_Radius){};
- ~Circle(){std::cout << "Circle destructor: " << this << std::endl; };
- 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;
- };
- /////////////////////////////////////////////////////////////////////
- shape_ptr Shape::defineRectangle(int height, int width){
- shape_ptr ptrRectangle = shape_ptr(new Rectangle(height, width));
- return (ptrRectangle);
- }
- shape_ptr Shape::defineCircle(float radius){
- shape_ptr ptrCircle = shape_ptr(new Circle(radius));
- return (ptrCircle);
- }
- /////////////////////////////////////////////////////////////////////
- void Rectangle::draw() const{
- std::cout << "draw rectangle at address: " << this << std::endl;
- }
- float Rectangle::area() const{
- float area = static_cast<float>(m_Height*m_Width);
- std::cout << "this is the Rectangle's area: " << area << std::endl;
- return area;
- }
- /////////////////////////////////////////////////////////////////
- void Circle::draw() const{
- std::cout << "draw circle at address: " << this << std::endl;
- }
- float Circle::area() const{
- float area = static_cast<float>(m_Radius*2*3.1415);
- std::cout << "this is the Circle's area: " << area << std::endl;
- return area;
- }
- int main( int argc, char ** argv ){
- std::vector<std::unique_ptr<Shape> > vect;
- vect.push_back(Shape::defineCircle(10));
- vect.push_back(Shape::defineRectangle(5, 4));
- vect.push_back(std::unique_ptr<Shape>(new Circle(10)));
- vect.push_back(std::unique_ptr<Shape>(new Rectangle(5,4)));
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement