#include <iostream>
using namespace std;
class Base {
public :
virtual void tell( ) {
cout << "Base" << endl;
}
void tell_usual( ) {
cout << "Base" << endl;
}
} ;
class Derived : public Base {
public :
void tell( ) override {
cout << "Derived" << endl;
}
void tell_usual( ) {
cout << "Derived" << endl;
}
} ;
int main( ) {
Base * base = new Base( ) ;
Derived * derived = new Derived( ) ;
Base * derived_as_base = derived; //pay attention! ptr to Base!
cout << "Base::tell " ;
base- > tell( ) ;
cout << "Base::tell_usual " ;
base- > tell_usual( ) ;
cout << "Derived::tell " ;
derived- > tell( ) ;
cout << "Derived::tell_usual " ;
derived- > tell_usual( ) ;
cout << "(Base*)Derived::tell (POLYMORPH!) " ;
derived_as_base- > tell( ) ;
cout << "(Base*)Derived::tell_usual " ;
derived_as_base- > tell_usual( ) ;
delete base;
delete derived;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjbGFzcyBCYXNlIHsKCXB1YmxpYzoKCXZpcnR1YWwgdm9pZCB0ZWxsKCkJewoJCWNvdXQ8PCJCYXNlIjw8ZW5kbDsKCX0KCXZvaWQgdGVsbF91c3VhbCgpIHsKCQljb3V0PDwiQmFzZSI8PGVuZGw7Cgl9Cn07CgpjbGFzcyBEZXJpdmVkIDogcHVibGljIEJhc2UgewoJcHVibGljOgoJdm9pZCB0ZWxsKCkgb3ZlcnJpZGUgewoJCWNvdXQ8PCJEZXJpdmVkIjw8ZW5kbDsKCX0KCXZvaWQgdGVsbF91c3VhbCgpIHsKCQljb3V0PDwiRGVyaXZlZCI8PGVuZGw7Cgl9Cn07CgoKaW50IG1haW4oKSB7CglCYXNlICogYmFzZSA9IG5ldyBCYXNlKCk7CglEZXJpdmVkICogZGVyaXZlZCA9IG5ldyBEZXJpdmVkKCk7CglCYXNlICpkZXJpdmVkX2FzX2Jhc2UgPSBkZXJpdmVkOyAvL3BheSBhdHRlbnRpb24hIHB0ciB0byBCYXNlIQoJY291dDw8IkJhc2U6On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