-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtemplate_method.cpp
More file actions
59 lines (51 loc) · 1.01 KB
/
Copy pathtemplate_method.cpp
File metadata and controls
59 lines (51 loc) · 1.01 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
/*!
* \brief. Template Method. Define the skeleton of an algorithm
* in an operation,deferring some steps to subclasses.Template
* Method lets subclasses redefine certain steps of an algorithm
* without changing the algorithm's.
*/
#include <iostream>
#include <memory>
class Animal {
public:
void GetInfo() {
this->GetSpecies();
this->GetName();
}
protected:
// protected virtual functions.
virtual void GetSpecies() {
printf("Animal.");
}
virtual void GetName() {
printf("A.\n");
}
};
// Use protected functions to override.
class Lion : public Animal {
protected:
void GetSpecies() {
printf("Lion.");
}
void GetName() {
printf("B.\n");
}
};
class Tiger : public Animal {
protected:
void GetSpecies() {
printf("Tiger.");
}
void GetName() {
printf("C.\n");
}
};
int main() {
Animal *animal_a = new Lion();
Animal *animal_b = new Tiger();
animal_a->GetInfo();
animal_b->GetInfo();
delete animal_a;
delete animal_b;
return 0;
}