-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp.cpp
46 lines (32 loc) · 953 Bytes
/
lsp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
class Rectangle {
protected:
int width;
int height;
public:
Rectangle(int width, int height) : width(width), height(height) {}
virtual void set_width(int width) { this->width = width; };
virtual void set_height(int height) { this->height = height; };
int get_height() { return height; };
int get_width() { return width; };
int area() const { return width * height; }
};
class Square : public Rectangle {
public:
Square(int size) : Rectangle(size, size) {}
void set_width(int width) override { this->width = height = width; }
void set_height(int height) override { this->height = width = height; }
};
void process(Rectangle &r) {
int w = r.get_width();
r.set_height(10);
cout << "epected area = " << w * 10 << ", got " << r.area() << endl;
}
int main() {
Rectangle r{5, 5};
process(r);
Square s{5};
process(s);
return 0;
}