diff --git a/cpp/virtual_destructor/virtual_destructor.cpp b/cpp/virtual_destructor/virtual_destructor.cpp new file mode 100644 index 0000000..680fc5f --- /dev/null +++ b/cpp/virtual_destructor/virtual_destructor.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +class A1 { + public: + A1 () { + cout << "A1() called" << endl; + } + + virtual ~A1 () { // won't be called if non-virtual + cout << "~A1() called" << endl; + } +}; + +class A2 : public A1 { + public: + A2 () { + cout << "A2() called" << endl; + } + + ~A2 () { + cout << "~A2() called" << endl; + } +}; + +int main (int argc, char *argv[]) +{ + A1 *ap = new A2(); + delete ap; +}