-
Notifications
You must be signed in to change notification settings - Fork 28
/
Static_Dynamic_Binding_IV(Virtual Destructor).cpp
125 lines (94 loc) · 3.08 KB
/
Static_Dynamic_Binding_IV(Virtual Destructor).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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <bits/stdc++.h>
using namespace std;
/*
static and dynamic binding IV
These topics are important for Design Patterns concept
Virtual Destructor ("DESTRUCTOR MUST BE DECLARED VIRTUAL IN THE BASE CLASS")
Why destructor must be virtual in a class hierarchy ?
-clean up consistency will not work. Like shown in the example below, destructor of derived class was not called
because of which ptr_ is not freed. It only calls the Base class part of Derived object (i.e. slices)
i.e. If the destructor is not virtual in polymorphic hierarchy, it leads to slicing.
"DESTRUCTOR MUST BE DECLARED VIRTUAL IN THE BASE CLASS"
By Rule of Polymorphism hierarchy:
If destructor of Base is virtual, by inheritance the destructor of Derived is also virtual
So So So, very important : If you have a hierarchy, then it doesn't make it non-polymorphic.
*/
class B {
int data_;
public:
B(int d) : data_(d) {
cout << "B()" << endl;
}
~B() {
cout << "~B()" << endl;
}
virtual void Print() {
cout << data_;
}
};
class D : public B{
int* ptr_;
public:
D(int d1, int d2) : B(d1), ptr_(new int(d2)) {
cout << "D()" << endl;
}
~D() {
cout << "~D()" << endl;
}
void Print() {
B::Print(); //cout << data_; // ERROR! : data_ is private inside B
cout << " " << *ptr_;
}
};
int main() {
B *p = new B(2); //Output : B()
B *q = new D(3, 5); //Output : B(), D()
p->Print(); cout << endl; //Output : 2
q->Print(); cout << endl; //Output : 3 5
//Since, new was used, you need to explicitly use delete
delete p; //~B()
delete q; //~B() Whhhhhhaaaat ????? (Destructor of d(type D) not called. Dayummmm
//delete q calls the destrutor.
//And during compile time, destructor in B is non-vritual. So, compiler only sees the type of q i.e. B*
//And hence, ~B() got called.
//So, we need to make virtual ~B() and it will fix things.
return 0;
}
////////////////////////////FIXED////////////////////////////////
class B {
int data_;
public:
B(int d) : data_(d) {
cout << "B()" << endl;
}
virtual ~B() { //Destructor made virtual
cout << "~B()" << endl;
}
virtual void Print() {
cout << data_;
}
};
class D : public B{
int* ptr_;
public:
D(int d1, int d2) : B(d1), ptr_(new int(d2)) {
cout << "D()" << endl;
}
~D() { //
cout << "~D()" << endl;
}
void Print() {
B::Print(); //cout << data_; // ERROR! : data_ is private inside B
cout << " " << *ptr_;
}
};
int main() {
B *p = new B(2); //Output : B()
B *q = new D(3, 5); //Output : B(), D()
p->Print(); cout << endl; //Output : 2
q->Print(); cout << endl; //Output : 3 5
//Since, new was used, you need to explicitly use delete
delete p; //~B()
delete q; //~D() ~B() YESSSS, Fixed (NOTE : new D(3, 5) had a B object as well which is why you see ~B() as well
return 0;
}