Advertisement
chzchz

Untitled

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