Advertisement
Guest User

member functions in pure abstract classes

a guest
Jan 22nd, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1.     #include <vector>
  2.     #include <memory>
  3.     #include <iostream>
  4.    
  5.     class Shape;
  6.  
  7.     typedef std::unique_ptr<Shape> shape_ptr;
  8.  
  9.     class Shape{
  10.  
  11.         public:
  12.  
  13.             Shape(){};
  14.             virtual ~Shape(){std::cout << "Shape destructor: " << this << std::endl;};
  15.  
  16.             virtual void draw() const = 0;
  17.             virtual float area() const = 0;
  18.  
  19.             virtual shape_ptr clone() const = 0;
  20.             virtual shape_ptr create() const = 0;
  21.  
  22.             static shape_ptr defineRectangle(int, int );
  23.             static shape_ptr defineCircle(float);
  24.     };
  25.  
  26.     class Rectangle:public Shape{
  27.         public:
  28.  
  29.             typedef std::unique_ptr<Rectangle> rectangle_SmartPtr;
  30.  
  31.             Rectangle(int height=0, int width=0):m_Height(height),m_Width(width){};
  32.             Rectangle(const Rectangle & rect):m_Height(rect.m_Height),m_Width(rect.m_Width){};
  33.             ~Rectangle(){};
  34.  
  35.             virtual void draw() const;
  36.             virtual float area() const;
  37.  
  38.  
  39.             virtual shape_ptr clone() const{ return shape_ptr(new Rectangle(*this)); };
  40.             virtual shape_ptr create() const{ return shape_ptr(new Rectangle()); };
  41.  
  42.         private:
  43.             int m_Height;
  44.             int m_Width;
  45.     };
  46.  
  47.  
  48.     class Circle:public Shape{
  49.         public:
  50.  
  51.             typedef std::unique_ptr<Circle> circle_SmartPtr;
  52.  
  53.             Circle(float radius=0):m_Radius(radius){};
  54.             Circle(const Circle & other):m_Radius(other.m_Radius){};
  55.             ~Circle(){std::cout << "Circle destructor: " << this << std::endl; };
  56.  
  57.             virtual void draw() const;
  58.             virtual float area() const;
  59.        
  60.             virtual shape_ptr clone() const{ return shape_ptr(new Circle(*this)); };
  61.             virtual shape_ptr create() const{ return shape_ptr(new Circle()); };
  62.  
  63.         private:
  64.  
  65.             float m_Radius;
  66.     };
  67.    
  68.     /////////////////////////////////////////////////////////////////////
  69.     shape_ptr Shape::defineRectangle(int height, int width){
  70.         shape_ptr ptrRectangle = shape_ptr(new Rectangle(height, width));
  71.         return (ptrRectangle);
  72.     }
  73.  
  74.     shape_ptr Shape::defineCircle(float radius){
  75.         shape_ptr ptrCircle = shape_ptr(new Circle(radius));
  76.         return (ptrCircle);
  77.     }
  78.    
  79.     /////////////////////////////////////////////////////////////////////
  80.  
  81.     void Rectangle::draw() const{
  82.         std::cout << "draw rectangle at address: " << this << std::endl;
  83.     }
  84.  
  85.     float Rectangle::area() const{
  86.         float area = static_cast<float>(m_Height*m_Width);
  87.         std::cout << "this is the Rectangle's area: " << area << std::endl;
  88.         return area;
  89.     }
  90.    
  91.     /////////////////////////////////////////////////////////////////
  92.  
  93.     void Circle::draw() const{
  94.         std::cout << "draw circle at address: " << this << std::endl;
  95.     }
  96.  
  97.     float Circle::area() const{
  98.         float area = static_cast<float>(m_Radius*2*3.1415);
  99.         std::cout << "this is the Circle's area: " << area << std::endl;
  100.         return area;
  101.     }
  102.  
  103.     int main( int argc, char ** argv ){
  104.  
  105.  
  106.         std::vector<std::unique_ptr<Shape> > vect;
  107.         vect.push_back(Shape::defineCircle(10));
  108.         vect.push_back(Shape::defineRectangle(5, 4));
  109.         vect.push_back(std::unique_ptr<Shape>(new Circle(10)));
  110.         vect.push_back(std::unique_ptr<Shape>(new Rectangle(5,4)));
  111.  
  112.         exit(EXIT_SUCCESS);
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement