Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <type_traits>
- #include <string>
- #include <cstdlib>
- #include <cstdio>
- #include <cassert>
- class Rectangle {
- public:
- Rectangle(uint32_t height, uint32_t width) :
- height_(height), width_(width) {}
- Rectangle(const Rectangle&) = default;
- uint32_t GetHeight() const { return height_; }
- uint32_t GetWidth() const { return width_; }
- virtual void SetWidth(uint32_t width) { width_ = width; }
- virtual void SetHeight(uint32_t height) { height_ = height; }
- uint32_t GetArea() const { return height_ * width_; }
- uint32_t GetPerimeter() const { return (height_ + width_) * 2; }
- protected:
- uint32_t height_, width_;
- };
- class Square : public Rectangle {
- public:
- explicit Square(uint32_t size) : Rectangle(size, size) {}
- Square(const Square&) = default;
- void SetWidth(uint32_t width) override {
- Rectangle::width_ = width;
- Rectangle::height_ = width;
- }
- void SetHeight(uint32_t height) override {
- Rectangle::height_ = height;
- Rectangle::width_ = height;
- }
- };
- bool CheckInvariant(Rectangle&& rect) {
- uint32_t height = 6;
- uint32_t width = 7;
- rect.SetHeight(height);
- rect.SetWidth(width);
- return rect.GetHeight() == height && rect.GetWidth() == width;
- }
- int main(int argc, char** argv, char** env) {
- Rectangle rectangle(5, 5);
- Square square(5);
- std::cerr << "Area test:" <<
- (rectangle.GetArea() == square.GetArea() ? "OK" : "FAIL") << std::endl;
- rectangle.SetHeight(6);
- rectangle.SetWidth(7);
- square.SetHeight(6);
- square.SetWidth(7);
- std::cerr << "Area test after resize:" <<
- (rectangle.GetArea() == square.GetArea() ? "OK" : "FAIL") << std::endl;
- std::cerr << "Invariant for rectangle: " <<
- (CheckInvariant(Rectangle(1, 1)) ? "OK" : "FAIL") << std::endl;
- std::cerr << "Invariant for square: " <<
- (CheckInvariant(Square(1)) ? "OK" : "FAIL") << std::endl;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement