Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class Person
- {
- public:
- Person(const string &type, const string &name)
- : _type(type),
- _name(name) {}
- virtual void
- Walk(const string &destination) const
- {
- cout << _type << ": " << _name << " walks to: " << destination << endl;
- }
- string
- GetType() const
- {
- return _type;
- }
- string
- GetName() const
- {
- return _name;
- }
- protected:
- const string _type;
- const string _name;
- };
- class Student : public Person
- {
- public:
- Student(const string &name, const string &favourite_song)
- : Person("Student", name),
- _favourite_song(favourite_song) {}
- void
- Learn() const
- {
- cout << _type << ": " << _name << " learns" << endl;
- }
- void
- SingSong() const
- {
- cout << _type << ": " << _name << " sings a song: " << _favourite_song << endl;
- }
- void
- Walk(const string &destination) const override
- {
- cout << _type << ": " << _name << " walks to: " << destination << endl;
- SingSong();
- }
- private:
- const string _favourite_song;
- };
- class Teacher : public Person
- {
- public:
- Teacher(const string &name, const string &subject)
- : Person("Teacher", name),
- _subject(subject) {}
- void
- Teach() const
- {
- cout << _type << ": " << _name << " teaches: " << _subject << endl;
- }
- private:
- const string _subject;
- };
- class Policeman : Person
- {
- public:
- Policeman(const string &name)
- : Person("Policeman", name) {}
- void
- Check(const Person &person) const
- {
- cout << _type << ": " << _name << " checks " << person.GetType() << ". ";
- cout << person.GetType() << "'s name is: " << person.GetName() << endl;
- }
- };
- void
- VisitPlaces(const Person &person, const vector<string> &places)
- {
- for (auto &place : places)
- {
- person.Walk(place);
- }
- }
- int
- main()
- {
- Teacher teacher("Jim", "Math");
- Student student("Ann", "We will rock you");
- Policeman policeman("Bob");
- VisitPlaces(teacher, {"Moscow", "London"});
- policeman.Check(student);
- VisitPlaces(student, {"Moscow", "London"});
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement