Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <cassert>
  7.  
  8. class Rectangle {
  9. public:
  10. Rectangle(uint32_t height, uint32_t width) :
  11. height_(height), width_(width) {}
  12.  
  13. Rectangle(const Rectangle&) = default;
  14.  
  15. uint32_t GetHeight() const { return height_; }
  16. uint32_t GetWidth() const { return width_; }
  17.  
  18. virtual void SetWidth(uint32_t width) { width_ = width; }
  19. virtual void SetHeight(uint32_t height) { height_ = height; }
  20.  
  21. uint32_t GetArea() const { return height_ * width_; }
  22. uint32_t GetPerimeter() const { return (height_ + width_) * 2; }
  23.  
  24. protected:
  25. uint32_t height_, width_;
  26. };
  27.  
  28.  
  29. class Square : public Rectangle {
  30. public:
  31. explicit Square(uint32_t size) : Rectangle(size, size) {}
  32.  
  33. Square(const Square&) = default;
  34.  
  35. void SetWidth(uint32_t width) override {
  36. Rectangle::width_ = width;
  37. Rectangle::height_ = width;
  38. }
  39. void SetHeight(uint32_t height) override {
  40. Rectangle::height_ = height;
  41. Rectangle::width_ = height;
  42. }
  43. };
  44.  
  45. bool CheckInvariant(Rectangle&& rect) {
  46. uint32_t height = 6;
  47. uint32_t width = 7;
  48. rect.SetHeight(height);
  49. rect.SetWidth(width);
  50.  
  51. return rect.GetHeight() == height && rect.GetWidth() == width;
  52. }
  53.  
  54.  
  55. int main(int argc, char** argv, char** env) {
  56. Rectangle rectangle(5, 5);
  57. Square square(5);
  58.  
  59. std::cerr << "Area test:" <<
  60. (rectangle.GetArea() == square.GetArea() ? "OK" : "FAIL") << std::endl;
  61.  
  62. rectangle.SetHeight(6);
  63. rectangle.SetWidth(7);
  64.  
  65. square.SetHeight(6);
  66. square.SetWidth(7);
  67.  
  68. std::cerr << "Area test after resize:" <<
  69. (rectangle.GetArea() == square.GetArea() ? "OK" : "FAIL") << std::endl;
  70.  
  71. std::cerr << "Invariant for rectangle: " <<
  72. (CheckInvariant(Rectangle(1, 1)) ? "OK" : "FAIL") << std::endl;
  73.  
  74. std::cerr << "Invariant for square: " <<
  75. (CheckInvariant(Square(1)) ? "OK" : "FAIL") << std::endl;
  76.  
  77. return EXIT_SUCCESS;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement