forked from anishsingh20/Programming-in-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualDestructor.cpp
More file actions
40 lines (33 loc) · 809 Bytes
/
VirtualDestructor.cpp
File metadata and controls
40 lines (33 loc) · 809 Bytes
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
#include<iostream>
using namespace std;
//Virtual destructors
class Person {
public:
Person(){
cout<<"Person called"<<endl;
}
virtual ~Person() {
cout<<"Person deleted"<<endl;
}
};
class Anish : public Person {
public:
Anish() {
cout<<"Anish called"<<endl;
}
~Anish(){
cout<<"Anish deleted"<<endl;
}
};
int main ()
{
Anish *a= new Anish();
//first constructor of Parent goes into stack, followed by Child class. And when memeory is deallocated in LIFO fashion
// first child class object is destructed, followed by the parent class
cout<<"\n"<<endl;
cout<<"Deleting from Stack"<<endl;
cout<<"\n";
Person *p = a ;//creating a pointer of type Person which refers to pointer of type Anish class
delete(p); // will automatically delete derived class
return 0;
}