fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3. class Base {
  4. public:
  5. virtual void tell() {
  6. cout<<"Base"<<endl;
  7. }
  8. void tell_usual() {
  9. cout<<"Base"<<endl;
  10. }
  11. };
  12.  
  13. class Derived : public Base {
  14. public:
  15. void tell() override {
  16. cout<<"Derived"<<endl;
  17. }
  18. void tell_usual() {
  19. cout<<"Derived"<<endl;
  20. }
  21. };
  22.  
  23.  
  24. int main() {
  25. Base * base = new Base();
  26. Derived * derived = new Derived();
  27. Base *derived_as_base = derived; //pay attention! ptr to Base!
  28. cout<<"Base::tell ";
  29. base->tell();
  30. cout<<"Base::tell_usual ";
  31. base->tell_usual();
  32. cout<<"Derived::tell ";
  33. derived->tell();
  34. cout<<"Derived::tell_usual ";
  35. derived->tell_usual();
  36. cout<<"(Base*)Derived::tell (POLYMORPH!) ";
  37. derived_as_base->tell();
  38. cout<<"(Base*)Derived::tell_usual ";
  39. derived_as_base->tell_usual();
  40.  
  41. delete base;
  42. delete derived;
  43. return 0;
  44. }
Success #stdin #stdout 0s 4524KB
stdin
Standard input is empty
stdout
Base::tell Base
Base::tell_usual Base
Derived::tell Derived
Derived::tell_usual Derived
(Base*)Derived::tell (POLYMORPH!) Derived
(Base*)Derived::tell_usual Base