File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ // C++ program to demonstrate Destructor
2
+
3
+ // Destructor - A destructor is function which is called automatically when an object goes out of scope.
4
+ // It destructs the objects of classes.
5
+
6
+ #include < bits/stdc++.h>
7
+ using namespace std ;
8
+
9
+ // class with Destructor
10
+ class Person {
11
+ private:
12
+ string name;
13
+ int age;
14
+ public:
15
+ Person (string iname,int iage){
16
+ name=iname;
17
+ age=iage;
18
+ }
19
+ void display (){
20
+ cout<<" Name : " <<name<<endl;
21
+ cout<<" Age : " <<age<<endl;
22
+ }
23
+ // We don't need to call Destructor as it will be invoked automatically when the object goes out of scope
24
+ ~Person (){
25
+ cout<<" Destructor Called" <<endl;
26
+ }
27
+ };
28
+
29
+ int main (){
30
+ // Creating an object of class Person
31
+ Person p (" abc" ,10 );
32
+ p.display ();
33
+ return 0 ;
34
+ }
You can’t perform that action at this time.
0 commit comments