fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Base
  5. {
  6. public:
  7. virtual void print(){cout<<"Base virtual"<<endl;}
  8. void nonvirt_print(){cout<<"Base"<<endl;}
  9. };
  10.  
  11. class Derived : public Base
  12. {
  13. public:
  14. virtual void print() override {cout<<"Derived virtual"<<endl;}
  15. void nonvirt_print(){cout<<"Derived"<<endl;}
  16. };
  17.  
  18. int main() {
  19. Derived *derived_arr[100];
  20. for(int i=0;i<100;++i)
  21. derived_arr[i] = new Derived();
  22. Base *base_arr[100];
  23. for(int i=0;i<100;++i)
  24. base_arr[i]=derived_arr[i];
  25. base_arr[10]->nonvirt_print(); //base
  26. base_arr[10]->print(); //derived virtual
  27. return 0;
  28. }
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
Base
Derived virtual