Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Person {
  9. public:
  10. Person(string const &occupation, string const &name)
  11. : occupation_(occupation), name_(name) {
  12. }
  13.  
  14. string getName() const {
  15. return name_;
  16. }
  17.  
  18. string getOccupation() const {
  19. return occupation_;
  20. }
  21.  
  22. virtual void Walk(string const &destination) const {
  23. outInfo() << " walks to: " << destination << endl;
  24. }
  25.  
  26. protected:
  27. ostream &outInfo() const {
  28. return cout << occupation_ << ": " << name_;
  29. }
  30.  
  31. private:
  32. string const occupation_;
  33. string const name_;
  34. };
  35.  
  36.  
  37. class Student : public Person {
  38. public:
  39. Student(string const &name, string const &favouriteSong)
  40. : Person("Student", name), favouriteSong_(favouriteSong) {
  41. }
  42.  
  43. void Learn() const {
  44. outInfo() << " learns" << endl;
  45. }
  46.  
  47. void SingSong() const {
  48. outInfo() << " sings a song: " << favouriteSong_ << endl;
  49. }
  50.  
  51. void Walk(string const &destination) const override {
  52. Person::Walk(destination);
  53. SingSong();
  54. }
  55.  
  56. private:
  57. string const favouriteSong_;
  58. };
  59.  
  60.  
  61. class Teacher : public Person {
  62. public:
  63. Teacher(string const &name, string const &subject)
  64. : Person("Teacher", name), subject_(subject) {
  65. }
  66.  
  67. void Teach() const {
  68. outInfo() << " teaches: " << subject_ << endl;
  69. }
  70.  
  71. private:
  72. string const subject_;
  73. };
  74.  
  75.  
  76. class Policeman : public Person {
  77. public:
  78. Policeman(string const &name) : Person("Policeman", name) {}
  79.  
  80. void Check(Person const &person) const {
  81. outInfo() << " checks " << person.getOccupation() << ". "
  82. << person.getOccupation() << "'s name is: " << person.getName() << endl;
  83. }
  84. };
  85.  
  86.  
  87. void VisitPlaces(Person const &person, vector<string> const &places) {
  88. for (auto const &place: places) {
  89. person.Walk(place);
  90. }
  91. }
  92.  
  93.  
  94. int main() {
  95. Teacher t("Jim", "Math");
  96. Student s("Ann", "We will rock you");
  97. Policeman p("Bob");
  98.  
  99. VisitPlaces(t, {"Moscow", "London"});
  100. p.Check(s);
  101. VisitPlaces(s, {"Moscow", "London"});
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement