Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Liskov Substitution Principle
- // Objects in a program should be replaceable with instances of their subtypes
- // w/o altering the correctness of the program
- #include "stdafx.h"
- #include <iostream>
- class Rectangle
- {
- protected:
- int width, height;
- public:
- Rectangle(const int width, const int height)
- : width{width},
- height{height}
- {
- }
- virtual int GetWidth() const
- {
- return width;
- }
- virtual void SetWidth(const int width)
- {
- this->width = width;
- }
- virtual int GetHeight() const
- {
- return height;
- }
- virtual void SetHeight(const int height)
- {
- this->height = height;
- }
- int Area() const { return width*height; }
- };
- class Square : public Rectangle
- {
- public:
- Square(int size) : Rectangle{size,size} {}
- void SetWidth(const int width) override {
- this->width = height = width;
- }
- void SetHeight(const int height) override {
- this->height = width = height;
- }
- };
- void process(Rectangle& r)
- {
- int w = r.GetWidth();
- r.SetHeight(10);
- std::cout << "expect area = " << (w * 10)
- << ", got " << r.Area() << std::endl;
- }
- // > 2. The solution of this project is actually build a factory with
- struct RectangleFactory
- {
- static Rectangle CreateRectangle(int w, int h);
- static Rectangle CreateSquare(int size);
- };
- int main()
- {
- Rectangle r{ 5,5 };
- process(r);
- // > 1. If you override the rectangle with the square values it will give you the wrong result;
- //Square s{ 5 };
- //process(s);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement