Skip to content

Instantly share code, notes, and snippets.

@Inkln
Last active October 7, 2019 16:21
Show Gist options
  • Save Inkln/9b30d305917aae8e6390f45cbe2ad1c8 to your computer and use it in GitHub Desktop.
Save Inkln/9b30d305917aae8e6390f45cbe2ad1c8 to your computer and use it in GitHub Desktop.
Homework LSP
#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;
}
#include <iostream>
#include <type_traits>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cassert>
// All methods are constant and correct
// No problem in real world)
// If you need to change size of rectangle or square
// you should create new
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_; }
uint32_t GetArea() const { return height_ * width_; }
private:
// instead of protected, now it's impossible to create
// other setters in derived classes
uint32_t height_, width_;
};
class Square : public Rectangle {
public:
Square(uint32_t size) : Rectangle(size, size) {}
Square(const Square&) = default;
};
int main(void) {
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment