summaryrefslogtreecommitdiffstats
path: root/virtual_destructor.cpp
diff options
context:
space:
mode:
authorKamil Kaminski <kylek389@gmail.com>2014-02-28 08:31:36 -0600
committerKamil Kaminski <kylek389@gmail.com>2014-02-28 08:31:36 -0600
commit88118d727819b4d42c0e1f8b742e80b517b21939 (patch)
tree2d1ee3144a729174231e3f813ef8ae748e4f79cb /virtual_destructor.cpp
parente8244aab814168304da11283b52608c81f75d346 (diff)
downloadc++-88118d727819b4d42c0e1f8b742e80b517b21939.tar.gz
c++-88118d727819b4d42c0e1f8b742e80b517b21939.tar.bz2
c++-88118d727819b4d42c0e1f8b742e80b517b21939.zip
demo explicit need of calling copy ctorHEADmaster
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;
+}