fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Vehicle
  7. {
  8. public:
  9. void print()
  10. {
  11. cout << "print()" << endl;
  12. }
  13.  
  14. virtual ~Vehicle() {}
  15. };
  16.  
  17. class FastVehicle : public Vehicle
  18. {
  19. public:
  20. void print(int a)
  21. {
  22. cout << "print(" << a << ")" << endl;
  23. }
  24.  
  25. void somethingElse()
  26. {
  27. cout << "Something else..." << endl;
  28. }
  29. };
  30.  
  31. int main()
  32. {
  33. vector<Vehicle*> vehicleVector;
  34.  
  35. vehicleVector.push_back(new Vehicle());
  36. vehicleVector.push_back(new FastVehicle());
  37. vehicleVector.push_back(new Vehicle());
  38.  
  39. for(Vehicle* vehicle : vehicleVector)
  40. {
  41. FastVehicle* fastVehicle = dynamic_cast<FastVehicle*>(vehicle);
  42.  
  43. if(fastVehicle)
  44. {
  45. fastVehicle->print(1337);
  46. fastVehicle->somethingElse();
  47. }
  48. else
  49. {
  50. vehicle->print();
  51. }
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
print()
print(1337)
Something else...
print()