fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4.  
  5. class Animal {
  6. public:
  7. Animal() { std::cout << "Animal()" << std::endl; };
  8. ~Animal() { std::cout << "~Animal()" << std::endl; };
  9. virtual void eat() { std::cout << "Animal is eating" << std::endl; };
  10. };
  11.  
  12. class Bear : public Animal {
  13. public:
  14. Bear() { std::cout << "Bear()" << std::endl; };
  15. ~Bear() { std::cout << "~Bear()" << std::endl; };
  16. void eat() { std::cout << "Bear is eating" << std::endl; };
  17. };
  18.  
  19. class Zebra : public Animal {
  20. public:
  21. Zebra() { std::cout << "Zebra()" << std::endl; };
  22. ~Zebra() { std::cout << "~Zebra()" << std::endl; };
  23. void eat() { std::cout << "Zebra is eating" << std::endl; };
  24. };
  25.  
  26. int main()
  27. {
  28. Bear bear;
  29. Zebra zebra;
  30.  
  31. std::vector<Animal*> animalsp;
  32. animalsp.push_back(&bear);
  33. animalsp.push_back(&zebra);
  34. for (auto i : animalsp) {
  35. i->eat();
  36. }
  37.  
  38. std::vector<Animal> animals;
  39. animals.push_back(std::move(bear));
  40. animals.push_back(std::move(zebra));
  41. for (auto i : animals) {
  42. i.eat();
  43. }
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Animal()
Bear()
Animal()
Zebra()
Bear is eating
Zebra is eating
~Animal()
Animal is eating
~Animal()
Animal is eating
~Animal()
~Animal()
~Animal()
~Zebra()
~Animal()
~Bear()
~Animal()