-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq51.cpp
59 lines (52 loc) · 1.19 KB
/
q51.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
/*
* WAP to demonstrate the concept of virtual destructors
*/
#include <iostream>
using namespace std;
// Class without virtual destructor
class Base1 {
public:
Base1() {
cout << "Base1 constructor called.\n";
}
~Base1() {
cout << "Base1 destructor called.\n";
}
};
class Derived1 : public Base1 {
public:
Derived1() {
cout << "Derived1 constructor called.\n";
}
~Derived1() {
cout << "Derived1 destructor called.\n";
}
};
// Base class with virtual destructor
class Base2 {
public:
Base2() {
cout << "Base2 constructor called.\n";
}
virtual ~Base2() {
cout << "Base2 destructor called.\n";
}
};
class Derived2 : public Base2 {
public:
Derived2() {
cout << "Derived2 constructor called.\n";
}
~Derived2() {
cout << "Derived2 destructor called.\n";
}
};
int main() {
cout << "WITHOUT VIRTUAL DESTRUCTOR:\n";
Base1 *ptr1 = new Derived1();
delete ptr1;
cout << "\nWITH VIRTUAL DESTRUCTOR:\n";
Base2 *ptr2 = new Derived2();
delete ptr2;
return 0;
}