Advertisement
NelloRizzo

Shapes

Mar 20th, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. // Shape.h - NON C'E' cpp
  2. #include "Canvas.h"
  3.  
  4. class Shape
  5. {
  6. public:
  7.     Shape() {}
  8.     virtual ~Shape() {}
  9.     virtual void draw(Canvas& c) = 0;
  10. };
  11. // Rectangle.h
  12. #include "Shape.h"
  13. #include "Canvas.h"
  14.  
  15. class Rectangle :
  16.     public Shape
  17. {
  18. private:
  19.     int x;
  20.     int y;
  21.     int width;
  22.     int height;
  23. public:
  24.     Rectangle(int x, int y, int width, int height);
  25.     virtual ~Rectangle();
  26.  
  27.     void draw(Canvas& c) override;
  28. };
  29.  
  30. // Rectangle.cpp
  31. #include "Rectangle.h"
  32.  
  33.  
  34. Rectangle::Rectangle(int x, int y, int width, int height):
  35.     Shape(),
  36.     x(x), y(y), width(width), height(height)
  37. {
  38. }
  39.  
  40.  
  41. Rectangle::~Rectangle()
  42. {
  43. }
  44.  
  45. void Rectangle::draw(Canvas & c)
  46. {
  47.     // disegno i lati orizzontali del rettangolo
  48.     for (int x1 = 0; x1 <= width; ++x1) {
  49.         c.set(x + x1, y);
  50.         c.set(x + x1, y + height);
  51.     }
  52.     // disegno i lati verticali del rettangolo
  53.     for (int y1 = 0; y1 <= height; ++y1) {
  54.         c.set(x, y + y1);
  55.         c.set(x + width, y + y1);
  56.     }
  57. }
  58. // Square.h - NON C'E' cpp
  59. #include "Rectangle.h"
  60. class Square :
  61.     public Rectangle
  62. {
  63. public:
  64.     Square(int x, int y, int side) :Rectangle(x, y, side, side) {}
  65.     virtual ~Square() {}
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement