Skip to content

Commit 2e4bd9f

Browse files
authored
Merge pull request #889 from pushpakumari5117/issue-888
Added the code for Destructor in C++
2 parents fe450db + dae3bad commit 2e4bd9f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

OOPs/Destructor/Destructor_cpp.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)