forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Destructor.cpp
62 lines (56 loc) · 1.07 KB
/
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
#include<iostream>
#include<string.h>
using namespace std;
//Destructor
class Sample
{
private:
int x;
public:
Sample()
{
x=2;
cout<<"\nDefault Constructor Called";
}
Sample(int a)
{
x=a;
cout<<"\n2.Parametrized Constructor Called"<<x;
}
Sample(int b,int y)
{
x=b;
cout<<"\n@Parametrized Constructor Called"<<x;
}
/*
What is destructor?
1. Destructor also a special member function that is
used to deallocate memeory.
2. It is also envoked automatically when the objects are going
out of the scope.
3. ~classname()
4. No parameter are passed.
5. Destructor can't be overloaded.
6. ~ Tilde operator.
7. Class name & Destructor name also be same.
*/
~Sample()
{
cout<<"\nDestructor Called";
cout<<"X="<<x;
}
};
int main()
{
Sample s1;
Sample s2(12);
Sample s3(15);
//j order e constructor calling hy tar opposite order e destructor calling hy
/*
Ex-->
Destructor CalledX=15
Destructor CalledX=12
Destructor CalledX=2
*/
return 0;
}