fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Parent{
  4. public:
  5. Parent(){};
  6. virtual~Parent(){
  7. cout << "destructor parent\n";
  8. }
  9. virtual void fn(){
  10. cout << "call parent fn\n";
  11. }
  12. };
  13. class Child:public Parent{
  14. public:
  15. ~Child(){
  16. cout << "destructor child\n";
  17. }
  18. virtual void fn(){
  19. cout << "call child fn\n";
  20. }
  21. };
  22. void test(Parent &obj){
  23. obj.fn();
  24. }
  25. int main() {
  26. Parent obj1;
  27. Child obj2;
  28. Parent *obj3 = new Child;
  29. test(obj1);
  30. test(obj2);
  31. test(*obj3);
  32. delete obj3;
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
call parent fn
call child fn
call child fn
destructor child
destructor parent
destructor child
destructor parent
destructor parent