fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct Gun
  5. {
  6. virtual void reload() { std::cout << "Gun::reload\n" ; }
  7. };
  8.  
  9. struct Pistol : public Gun
  10. {
  11. static const unsigned MagCap = 17 ;
  12.  
  13. std::string slidePos ;
  14. unsigned roundsLeft ;
  15.  
  16. Pistol( ) : slidePos("closed"), roundsLeft(MagCap) {}
  17.  
  18.  
  19. void reload()
  20. {
  21. std::cout << "Pistol::reload\n" ;
  22.  
  23. if ( slidePos == "open" )
  24. roundsLeft = MagCap ;
  25. else
  26. {
  27. slidePos = "open" ;
  28. roundsLeft = MagCap+1 ;
  29. }
  30. slidePos = "closed" ;
  31. }
  32. };
  33.  
  34. int main()
  35. {
  36. Pistol p ;
  37. Gun g ;
  38.  
  39. Gun* ptr = &p ;
  40. ptr->reload() ;
  41.  
  42. ptr = &g ;
  43. ptr->reload() ;
  44. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
Pistol::reload
Gun::reload