-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_persons.cpp
103 lines (96 loc) · 2.34 KB
/
class_persons.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
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Person {
public:
Person() {}
explicit Person(const string& name, const string& profession) :
name(name),
profession(profession)
{}
virtual string Name() const {
return name;
}
virtual string Profession() const {
return profession;
}
virtual void Walk(const string& destination) const {
PrintPerson();
cout << " walks to: " << destination << endl;
}
void VisitPlaces(const vector<string>& places) {
for (const auto& place : places) {
Walk(place);
}
}
void PrintPerson() const {
cout << Profession() << ": " << Name();
}
private:
const string name;
const string profession;
};
class Student : public Person {
public:
Student() {}
explicit Student(const string& name, const string& favourite_song) :
Person(name, "Student"),
favourite_song(favourite_song)
{}
string FavouriteSong() const {
return favourite_song;
}
void Learn() const {
PrintPerson();
cout << " learns" << endl;
}
void Walk(const string& destination) const override {
Person::Walk(destination);
SingSong();
}
void SingSong() const {
PrintPerson();
cout << " sings a song: " << FavouriteSong() << endl;
}
private:
const string favourite_song;
};
class Teacher : public Person {
public:
Teacher() {}
explicit Teacher(const string& name, const string& subject) :
Person(name, "Teacher"),
subject(subject)
{}
string Subject() const {
return subject;
}
void Teach() const {
PrintPerson();
cout << " teaches: " << Subject() << endl;
}
private:
const string subject;
};
class Policeman : public Person {
public:
Policeman() {}
explicit Policeman(const string& name) :
Person(name, "Policeman")
{}
void Check(const Person& person) {
PrintPerson();
cout << " checks " << person.Profession() << ". "
<< person.Profession() << "'s name is: " << person.Name() << endl;
}
};
int main() {
Teacher t("Jim", "Math");
Student s("Ann", "We will rock you");
Policeman p("Bob");
t.VisitPlaces({"Moscow", "London"});
p.Check(s);
s.VisitPlaces({"Moscow", "London"});
return 0;
}