fork download
  1. #include <iostream>
  2.  
  3. class weapon {
  4.  
  5. public:
  6. int fireRate;
  7. int bulletDamage;
  8. int range;
  9. int activeBullet;
  10.  
  11. public:
  12. virtual void fire(void) {std::cout << "machine " << '\n';}
  13. virtual ~weapon() {std::cout << "destructor is virtual" << '\n';}
  14. };
  15.  
  16. class machineGun: public weapon {
  17. public:
  18. void fire(void) {std::cout << "machine gun firing" << '\n';}
  19. ~machineGun(void) { std::cout << "machine gun destroyed" << '\n';}
  20. };
  21.  
  22. class flamer: public weapon {
  23. public:
  24. void fire(void) {std::cout << "flamer firing" << '\n';}
  25. ~flamer(void) {std::cout << "flamer destroyed" << '\n';}
  26. };
  27.  
  28. int main(void)
  29. {
  30. const int count = 2;
  31. weapon *weapons[count];
  32.  
  33. machineGun *a = new machineGun();
  34. flamer *b = new flamer();
  35.  
  36. weapons[0] = a;
  37. weapons[1] = b;
  38.  
  39. weapons[0]->fire();
  40. weapons[1]->fire();
  41.  
  42. delete a;
  43. delete b;
  44.  
  45. }
Success #stdin #stdout 0.01s 5496KB
stdin
Standard input is empty
stdout
machine gun firing
flamer firing
machine gun destroyed
destructor is virtual
flamer destroyed
destructor is virtual