-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathInheritance_Part_IV(Constr & Deconstr).cpp
65 lines (58 loc) · 1.99 KB
/
Inheritance_Part_IV(Constr & Deconstr).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;
/*
Inheritance - IIV
Constructor-Destructor
-Derived class inherits the Constructors and Destructor of Base class (but in different semantics)
-Derived class cannot override or overload a Constructor or the Destructor of Base class.
Construction-Deconstruction
-A constructor of the Derived class must "first" call the constructor of Base class to construct the Base class instance of the Derived class
-A Deconstructor of the Derived class must call the destructor of Base class to destruct the Base class instance of the Derived class
*/
class B { //Base class
protected:
int data_;
public:
B(int d = 0) : data_(d) {
cout << "B::B(int): " << data_ << endl;
}
~B() {
cout << "B::~B(): " << data_ << endl;
}
};
class D : public B { //Derived Class
int info_;
public:
D(int d, int i) : B(d), info_(i) { //Explicit construction of Base (B(d))
cout << "D::D(int, int): " << data_ << ", " << info_ << endl;
}
explicit D(int i) : info_(i) { //Implicit construction of Base by compiler (Possible only because Base has default constructor which is similar to no parameter constructor
//else compiler error
cout << "D::D(int): " << data_ << ", " << info_ << endl;
}
~D() {
cout << "D::~D(): " << data_ << ", " << info_ << endl;
}
};
int main() {
B b(0);
D d1(1, 2);
D d2(3);
/*
Output:
B::B(int): 0 //Obj. b
B::B(int): 1 //Obj. d1
D::D(int, int): 1, 2 //Obj. d1
B::B(int): 0 //Obj. d2
D::D(int): 0, 3 //Obj. d2
D::~D(): 0, 3 //Obj. d2
B::~B(): 0 //Obj. d2
D::~D(): 1, 2 //Obj. d1
B::~B(): 1 //Obj. d1
B::~B(): 0 //Obj. b
*/
//Note :
//1) First construct base class object and then derived class object
//2) First destruct derived class object and then base class object
return 0;
}