fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Vehicle {
  5. public:
  6. virtual void show() { cout<<"I'm a vehicle"<<endl; } // virtual
  7. void print() { cout <<"I'm a vehicle"<<endl; } // not virtual
  8. void invokeshow() { show(); } // not virtual but invoking virtual
  9. void invokespecificshow() { Vehicle::show(); } // not virtual invoking specific
  10. ~Vehicle() {} //at least one virtual ? then virtual destructor
  11. };
  12.  
  13. class Motorcycle: public Vehicle {
  14. public:
  15. void show() override { cout<<"I'm a motorcycle"<<endl; }
  16. void print() { cout <<"I'm a motorcycle"<<endl; }
  17. };
  18.  
  19. void test(Vehicle &v) {
  20. v.show();
  21. v.print();
  22. v.invokeshow();
  23. v.invokespecificshow();
  24. }
  25.  
  26. int main() {
  27. Motorcycle mt;
  28. test(mt);
  29. return 0;
  30. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
I'm a motorcycle
I'm a vehicle
I'm a motorcycle
I'm a vehicle