-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path12_04.cpp
51 lines (41 loc) · 870 Bytes
/
12_04.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
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
class Shape {
private:
string name;
public:
Shape(string name) :
name(name) {
}
virtual int Area() {
throw logic_error("Not implemented. Do override");
return -1;
}
string GetShapeName() {
return name;
}
virtual ~Shape() {}
};
class Rectangle: public Shape {
int wid;
int height;
public:
Rectangle(string name, int wid, int height) :
Shape(name), wid(wid), height(height) {
}
int Area() {
return wid * height;
}
};
void process(Shape* shape) {
// This function knows nothing about children!
// Compile time determined
cout << "This shape's name is: " << shape->GetShapeName() << ". ";
// Run Time determined
cout << "Its area: " << shape->Area() << "\n";
}
int main() {
Rectangle r("Nice Rect", 4, 5);
process(&r);
return 0;
}