-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.txt
More file actions
65 lines (56 loc) · 2.15 KB
/
log.txt
File metadata and controls
65 lines (56 loc) · 2.15 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
1219-2013 use of inheritance, polymorphism, OCP
circle and square
=> commit 975f20a
1. How to store a list of different shape objects in a vector?
A vector can only store object (or poiter to obejct)
of the same type. We need to have a type that fits all.
-> convert to void *
2. How to convert void * to Circle * and Square *? How do we know
which type to convert to? Luckily each shape has a name!
-> static_cast<Circle *>(p)
3. write up the printShape function.
=> commit 2971044
Problem: now add a new class called Line.
1. add the class Line.
2. modify printShape function: add if check for line
=> commit 8587bb1
Look back:
1. Much duplicated code
2. The need to modify printShape every time you add a new type
Remove duiplicated code: Inheritance. Should not do anything to the tests.
explain how inheritance works. Draw class diagram before/after
1. add class Shape with stuff to be shared.
2. Derive Circle from Shape:
modifying ctor,
remove duplicated code: _name, name(),
modify print() (due to _name)
3. ditto Square
4. ditto Line
=> commit 635181d
How many line did we save?
1. review printShape, can we improve it?
now that all shapes derive from Shape, we shopuld be
able to chnage void * to Shape *.
modify test, printShape
=> commit 0dad724
How much did we simplify?
Next add yet another shape called point.
But argue to solve printShape modification problem.
1. identify what makes printShape change.
->print() [name() is shared now, so no problem]
add Shape::print as non-virtual
2. Change printSHape: comment out the if's;
-> tests failing, do cout, only shape name printed as
in Shape::print
3. "Let C++ runtime decides what print() to call for you"
-> make Shape::print virtual
all tests passing with one line change
remove couts and commented code
=> commit 2dd6d78
4. add the point class w/0 modifying printShape.
add test, add Point, test passing, done.
also drop static casts from tests, done.
=> commit 0c40c91
OCP: "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"
What is open for extension? New substypes of Shape.
What is closed for modification? printShape and Existing shapes.