fork download
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <memory>
  4. #include <vector>
  5. #include <iostream>
  6.  
  7. struct NPC
  8. {
  9.  
  10. virtual void AI() =0 ;
  11. };
  12.  
  13. struct Pig : public NPC
  14. {
  15. void AI() { std::cout << "Not by the hair of my chinny chin chin!\n" ; }
  16. };
  17.  
  18. struct Wolf : public NPC
  19. {
  20. void AI() { std::cout << "Little pig, little pig! Let me in!\n" ; }
  21. };
  22.  
  23.  
  24. const unsigned numInitialEnemies = 10 ;
  25.  
  26. int main()
  27. {
  28. std::srand(std::time(0)) ;
  29.  
  30. std::vector<std::unique_ptr<NPC>> enemies ;
  31.  
  32. for ( unsigned i=0; i<numInitialEnemies; ++i )
  33. {
  34. if ( rand() % 2 )
  35. enemies.emplace_back(new Pig) ;
  36. else
  37. enemies.emplace_back(new Wolf) ;
  38. }
  39.  
  40. for ( unsigned i=0; i<enemies.size(); ++i )
  41. enemies[i]->AI() ;
  42. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
Not by the hair of my chinny chin chin!
Not by the hair of my chinny chin chin!
Not by the hair of my chinny chin chin!
Little pig, little pig!  Let me in!
Not by the hair of my chinny chin chin!
Little pig, little pig!  Let me in!
Little pig, little pig!  Let me in!
Little pig, little pig!  Let me in!
Little pig, little pig!  Let me in!
Not by the hair of my chinny chin chin!