summaryrefslogtreecommitdiffstats
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
parente8244aab814168304da11283b52608c81f75d346 (diff)
downloadc++-master.tar.gz
c++-master.tar.bz2
c++-master.zip
demo explicit need of calling copy ctorHEADmaster
-rw-r--r--inheritance_ctor_calls.cpp77
-rw-r--r--virtual_destructor.cpp40
2 files changed, 117 insertions, 0 deletions
diff --git a/inheritance_ctor_calls.cpp b/inheritance_ctor_calls.cpp
new file mode 100644
index 0000000..6d692a8
--- /dev/null
+++ b/inheritance_ctor_calls.cpp
@@ -0,0 +1,77 @@
+/* g++ -Wall -std=gnu++98 -pedantic -MD -O0 -g inheritance_ctor_calls.cpp -o inheritance_ctor_calls
+ *
+ * Note: line 64 is very IMPORTANT!
+ * output:
+ *
+ * node1 default ctor called
+ * node2 default ctor called with y
+ * node1 default ctor called
+ * node2 copy ctor called
+ *
+ */
+
+#include <iostream>
+
+class Node1
+{
+ public:
+ Node1() : x_(0) {
+ std::cout << "node1 default ctor called\n";
+ };
+ Node1(int x) : x_(x) {
+ std::cout << "node1 default ctor called with x\n";
+ };
+
+ virtual void foo() {
+ std::cout << "node1 foo called\n";
+ }
+
+ int x_;
+};
+
+class Node2 : public Node1
+{
+ public:
+ Node2() : y_(0) {
+ std::cout << "node2 default ctor called\n";
+ };
+ Node2(int y) : y_(y) {
+ std::cout << "node2 default ctor called with y\n";
+ };
+
+ Node2(const Node2 &n) : y_(0) {
+ std::cout << "node2 copy ctor called\n";
+ }
+
+ virtual void foo() {
+ std::cout << "node2 foo called\n";
+ /* invoke parent method */
+ Node1::foo();
+ /* do things unique to this class */
+ /* ... */
+ }
+
+ int y_;
+};
+
+class Collection : public Node2
+{
+ public:
+ /* only immediate base class may be initialized, Node1 is 2 level deep here hence error */
+ Collection() : /* Node1(2), error */ Node2(1), z(0) {
+ }
+ /* parent copy ctor needs to be invoked since derived class' copy ctor is overridden */
+ Collection(const Collection& c) : Node2(c), z(0) {
+ /* ... */
+ }
+
+ int z;
+};
+
+int main(int argc, char **argv)
+{
+ Collection c;
+ Collection d(c);
+
+ return 0;
+}
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;
+}