Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <memory>
- using namespace std;
- class Printable
- {
- public:
- Printable()
- {
- cout << "PRINTABLE CONSTRUCTOR" << endl;
- }
- ~Printable()
- {
- cout << "PRINTABLE DESTRUCTOR" << endl;
- }
- virtual void print() const = 0;
- friend ostream& operator<<(ostream& stream, const Printable& obj);
- };
- class Shape : public Printable
- {
- public:
- Shape(int width, int height, char pattern = '*') :
- m_width(width),
- m_height(height),
- m_pattern(pattern)
- {
- cout << "SHAPE CONSTRUCTOR" << endl;
- }
- ~Shape()
- {
- cout << "SHAPE DESTRUCTOR" << endl;
- }
- void changePattern(char pattern)
- {
- m_pattern = pattern;
- }
- char getPattern() const
- {
- return m_pattern;
- }
- protected:
- int m_width;
- int m_height;
- char m_pattern;
- };
- ostream& operator<<(ostream& stream, const Printable& obj)
- {
- obj.print();
- return stream;
- }
- class Triangle : public Shape
- {
- public:
- Triangle(int height) :
- Shape(height, height)
- {
- cout << "TRIANGLE CONSTRUCTOR" << endl;
- }
- ~Triangle()
- {
- cout << "TRIANGLE DESTRUCTOR" << endl;
- }
- void print() const override
- {
- cout << "TRIANGLE:" << endl;
- for (int i = 1; i <= m_height; ++i)
- {
- for (int j = 0; j < i; ++j)
- {
- cout << m_pattern;
- }
- cout << endl;
- }
- }
- };
- class Rectangle : public Shape
- {
- public:
- Rectangle(int width, int height) :
- Shape(width, height)
- {
- cout << "RECTNAGLE CONSTRUCTOR" << endl;
- }
- ~Rectangle()
- {
- cout << "RECTNAGLE DESTRUCTOR" << endl;
- }
- void print() const override
- {
- cout << "RECTANGLE:" << endl;
- for (int i = 0; i < m_width; ++i)
- {
- for (int j = 0; j < m_height; ++j)
- {
- cout << m_pattern;
- }
- cout << endl;
- }
- }
- };
- class Square : public Rectangle
- {
- public:
- Square(int size) :
- Rectangle(size, size)
- // m_width(size), m_height(size)
- {
- cout << "SQUARE CONSTRUCTOR" << endl;
- }
- ~Square()
- {
- cout << "SQUARE DESTRUCTOR" << endl;
- }
- void print() const override
- {
- cout << "SQUARE FROM ";
- Rectangle::print();
- }
- void changeSize(int width, int height)
- {
- m_width = width;
- m_height = height;
- }
- };
- int main()
- {
- Square s(10);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement