From 046d0da8736e22084ce0dc5b27077d1df3259910 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Sun, 9 Mar 2014 14:47:56 +0400 Subject: [PATCH] Virtual destructor example added. --- cpp/virtual_destructor/virtual_destructor.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cpp/virtual_destructor/virtual_destructor.cpp 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; +}