Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <memory>
  5.  
  6. using namespace std;
  7.  
  8. class Printable
  9. {
  10. public:
  11.     Printable()
  12.     {
  13.         cout << "PRINTABLE CONSTRUCTOR" << endl;
  14.     }
  15.  
  16.     ~Printable()
  17.     {
  18.         cout << "PRINTABLE DESTRUCTOR" << endl;
  19.     }
  20.  
  21.     virtual void print() const = 0;
  22.  
  23.     friend ostream& operator<<(ostream& stream, const Printable& obj);
  24.  
  25. };
  26.  
  27. class Shape : public Printable
  28. {
  29. public:
  30.     Shape(int width, int height, char pattern = '*') :
  31.         m_width(width),
  32.         m_height(height),
  33.         m_pattern(pattern)
  34.     {
  35.         cout << "SHAPE CONSTRUCTOR" << endl;
  36.     }
  37.  
  38.     ~Shape()
  39.     {
  40.         cout << "SHAPE DESTRUCTOR" << endl;
  41.     }
  42.  
  43.     void changePattern(char pattern)
  44.     {
  45.         m_pattern = pattern;
  46.     }
  47.  
  48.     char getPattern() const
  49.     {
  50.         return m_pattern;
  51.     }
  52.  
  53. protected:
  54.     int m_width;
  55.     int m_height;
  56.     char m_pattern;
  57. };
  58.  
  59. ostream& operator<<(ostream& stream, const Printable& obj)
  60. {
  61.     obj.print();
  62.     return stream;
  63. }
  64.  
  65. class Triangle : public Shape
  66. {
  67. public:
  68.     Triangle(int height) :
  69.         Shape(height, height)
  70.     {
  71.         cout << "TRIANGLE CONSTRUCTOR" << endl;
  72.     }
  73.  
  74.     ~Triangle()
  75.     {
  76.         cout << "TRIANGLE DESTRUCTOR" << endl;
  77.     }
  78.  
  79.     void print() const override
  80.     {
  81.         cout << "TRIANGLE:" << endl;
  82.         for (int i = 1; i <= m_height; ++i)
  83.         {
  84.             for (int j = 0; j < i; ++j)
  85.             {
  86.                 cout << m_pattern;
  87.             }
  88.  
  89.             cout << endl;
  90.         }
  91.     }
  92. };
  93.  
  94.  
  95. class Rectangle : public Shape
  96. {
  97. public:
  98.     Rectangle(int width, int height) :
  99.         Shape(width, height)
  100.     {
  101.         cout << "RECTNAGLE CONSTRUCTOR" << endl;
  102.     }
  103.  
  104.     ~Rectangle()
  105.     {
  106.         cout << "RECTNAGLE DESTRUCTOR" << endl;
  107.     }
  108.  
  109.     void print() const override
  110.     {
  111.         cout << "RECTANGLE:" << endl;
  112.         for (int i = 0; i < m_width; ++i)
  113.         {
  114.             for (int j = 0; j < m_height; ++j)
  115.             {
  116.                 cout << m_pattern;
  117.             }
  118.  
  119.             cout << endl;
  120.         }
  121.     }
  122. };
  123.  
  124. class Square : public Rectangle
  125. {
  126. public:
  127.     Square(int size) :
  128.         Rectangle(size, size)
  129. //        m_width(size), m_height(size)
  130.     {
  131.         cout << "SQUARE CONSTRUCTOR" << endl;
  132.     }
  133.  
  134.     ~Square()
  135.     {
  136.         cout << "SQUARE DESTRUCTOR" << endl;
  137.     }
  138.  
  139.     void print() const override
  140.     {
  141.         cout << "SQUARE FROM ";
  142.         Rectangle::print();
  143.     }
  144.  
  145.     void changeSize(int width, int height)
  146.     {
  147.         m_width = width;
  148.         m_height = height;
  149.     }
  150. };
  151.  
  152. int main()
  153. {
  154.     Square s(10);
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement