-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactoring.cpp
More file actions
107 lines (86 loc) · 2.22 KB
/
Copy pathrefactoring.cpp
File metadata and controls
107 lines (86 loc) · 2.22 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Human
{
public:
Human(const string &name, const string &type) : name_(name), type_(type) {}
virtual void Walk(const string &destination) const
{
Description() << " walks to " << destination << endl;
}
ostream &Description() const
{
return cout << type_ + ": " + name_;
}
const string name_;
const string type_;
};
class Student : public Human
{
public:
Student(const string &name, const string &favouriteSong) :
Human(name, "Student"),
favouriteSong_(favouriteSong)
{}
void Learn() const
{
Description() << " learns" << endl;
}
void Walk(const string &destination) const override
{
Human::Walk(destination);
SingSong();
}
void SingSong() const
{
Description() << " sings a song: " << favouriteSong_ << endl;
}
private:
const string favouriteSong_;
};
class Teacher : public Human
{
public:
Teacher(const string &name, const string &subject) :
Human(name, "Teacher"),
subject_(subject)
{}
void Teach() const
{
Description() << " teaches: " << subject_ << endl;
}
private:
const string subject_;
};
class Policeman : public Human
{
public:
Policeman(const string &name) : Human(name, "Policeman") {}
void Check(const Human &h)
{
Description() << " checks " << h.type_ << ". " << h.type_ << "'s name is: " << h.name_ << endl;
}
};
void VisitPlaces(const Human &h, const vector<string> &places)
{
for (const auto &p : places)
{
h.Walk(p);
}
}
int main()
{
Teacher t("Jim", "Math");
Student s("Ann", "We will rock you");
Policeman p("Bob");
VisitPlaces(t, {"Moscow", "London"});
p.Check(s);
VisitPlaces(s, {"Moscow", "London"});
cout << endl << "Correct output:" << endl;
cout << "Teacher: Jim walks to: Moscow\nTeacher: Jim walks to: London\nPoliceman: Bob checks Student. Student's name is: Ann\n";
cout << "Student: Ann walks to: Moscow\nStudent: Ann sings a song: We will rock you\nStudent: Ann walks to: London\n";
cout << "Student: Ann sings a song: We will rock you\n";
return 0;
}