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(string const &occupation, string const &name)
- : occupation_(occupation), name_(name) {
- }
- string getName() const {
- return name_;
- }
- string getOccupation() const {
- return occupation_;
- }
- virtual void Walk(string const &destination) const {
- outInfo() << " walks to: " << destination << endl;
- }
- protected:
- ostream &outInfo() const {
- return cout << occupation_ << ": " << name_;
- }
- private:
- string const occupation_;
- string const name_;
- };
- class Student : public Person {
- public:
- Student(string const &name, string const &favouriteSong)
- : Person("Student", name), favouriteSong_(favouriteSong) {
- }
- void Learn() const {
- outInfo() << " learns" << endl;
- }
- void SingSong() const {
- outInfo() << " sings a song: " << favouriteSong_ << endl;
- }
- void Walk(string const &destination) const override {
- Person::Walk(destination);
- SingSong();
- }
- private:
- string const favouriteSong_;
- };
- class Teacher : public Person {
- public:
- Teacher(string const &name, string const &subject)
- : Person("Teacher", name), subject_(subject) {
- }
- void Teach() const {
- outInfo() << " teaches: " << subject_ << endl;
- }
- private:
- string const subject_;
- };
- class Policeman : public Person {
- public:
- Policeman(string const &name) : Person("Policeman", name) {}
- void Check(Person const &person) const {
- outInfo() << " checks " << person.getOccupation() << ". "
- << person.getOccupation() << "'s name is: " << person.getName() << endl;
- }
- };
- void VisitPlaces(Person const &person, vector<string> const &places) {
- for (auto const &place: places) {
- person.Walk(place);
- }
- }
- 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"});
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement