fork download
  1. #include <cstdio>
  2.  
  3. class Animal
  4. {
  5. protected: int _generation = 0;
  6.  
  7. public: Animal() : Animal(1) {
  8. }
  9.  
  10. public: Animal(int generation) {
  11. _generation = generation;
  12. }
  13.  
  14. public: virtual Animal* spawn()
  15. {
  16. puts("Spawn From Animal");
  17. return new Animal(_generation + 1);
  18. }
  19. };
  20.  
  21. class Dog : public Animal
  22. {
  23. public: Dog() : Dog(1) {
  24. }
  25.  
  26. public: Dog(int generation) {
  27. _generation = generation;
  28. }
  29.  
  30. public: virtual Dog* spawn()
  31. {
  32. puts("Spawn From Dog");
  33. return new Dog(_generation + 1);
  34. }
  35. };
  36.  
  37. int main()
  38. {
  39. Animal* a = new Dog();
  40. Animal* x = a->spawn();
  41.  
  42. Dog* b = new Dog();
  43. Dog* y = b->spawn();
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
Spawn From Dog
Spawn From Dog