summaryrefslogtreecommitdiffstats
path: root/virtual_destructor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'virtual_destructor.cpp')
-rw-r--r--virtual_destructor.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/virtual_destructor.cpp b/virtual_destructor.cpp
new file mode 100644
index 0000000..75324c6
--- /dev/null
+++ b/virtual_destructor.cpp
@@ -0,0 +1,40 @@
+/* outputs:
+ *
+ * derived destructor called
+ * base destructor called
+ */
+
+#include <iostream>
+
+/* will generate a class's default ctor, copy ctor, copy assignment operator,
+ * by default data member and dtor are private, default ctor, copy ctor and
+ * assignment operator are public
+ */
+class Base
+{
+ public:
+ virtual ~Base();
+};
+Base::~Base() {
+ std::cout << "base destructor called\n";
+}
+
+class Derived : public Base
+{
+ public:
+ ~Derived();
+};
+Derived::~Derived() {
+ std::cout << "derived destructor called\n";
+}
+
+int main(int argc, char **argv)
+{
+ /* when d goes out of scope, it will call its destructor will call base
+ * class destructor
+ */
+ Derived d;
+ Base b = d; /* conversion of d into Base takes place on rhs, d is sliced down */
+
+ return 0;
+}