fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. using namespace std;
  5.  
  6. class BaseClass
  7. {
  8. public:
  9. void foo() {
  10. cout << "Before calling " << typeid(*this).name() << "::doFoo()" << endl;
  11. doFoo();
  12. cout << "After calling " << typeid(*this).name() << "::doFoo()" << endl;
  13. }
  14. protected:
  15. virtual void doFoo() = 0;
  16. };
  17.  
  18. class DerivedClass: public BaseClass
  19. {
  20. protected: // or private, depends on use case
  21. virtual void doFoo() {/*come impl*/}
  22. };
  23.  
  24. int main() {
  25. BaseClass * bp = new DerivedClass();
  26. bp->foo();
  27. delete bp;
  28. return 0;
  29. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Before calling 12DerivedClass::doFoo()
After calling 12DerivedClass::doFoo()