-
Notifications
You must be signed in to change notification settings - Fork 7
/
destructor.cpp
42 lines (36 loc) · 1.03 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
#include<bits/stdc++.h>
using namespace std;
class Point {
private:
int x, y;
public:
Point() {
x = 0, y = 0;
cout << "Default constructor invoked\n";
}
Point(int x, int y) {
this->x = x;
this->y = y;
cout << "Parametrized constructor invoked\n";
}
~Point() {
cout << "Destructor invoked\n";
}
};
int main() {
/*
What is a destructor?
--> It is an instance member function of a class hence can never be static
--> Destructor name is same as class name preceded by ~(pronounced tilde) symbol
--> No return type
--> No arguments, hence no overloading is possible
--> It is invoked implicitly when object is going to get destroyed
Use of destructor?
--> It should be defined to release resources allocated to an object
Some important points:
--> If a programmer does not explicitly create a destructor, compiler creates one.
--> But if the programmer creates one explicitly, the compiler does not create one.
*/
Point p1;
Point p2(2, 3);
}