fork download
  1. #include <list>
  2. #include <iostream>
  3. #include <memory>
  4. using namespace std;
  5.  
  6. class Person {
  7. public:
  8. virtual ~Person() = default;
  9. virtual void talk()=0;
  10. };
  11.  
  12. class Student : public Person {
  13. public:
  14. void talk() {
  15. cout << "Teach me!\n";
  16. }
  17. };
  18.  
  19. class Teacher : public Person {
  20. public:
  21. void talk() {
  22. cout << "Listen!\n";
  23. }
  24. };
  25.  
  26. int main() {
  27. list<std::unique_ptr<Person>> people;
  28. people.push_back(unique_ptr<Person>(new Student()));
  29. people.push_back(unique_ptr<Person>(new Teacher()));
  30.  
  31. for (auto& p : people) {
  32. p->talk();
  33. }
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 4548KB
stdin
Standard input is empty
stdout
Teach me!
Listen!