-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDestructor.cpp
49 lines (43 loc) · 934 Bytes
/
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
/*
A destructor is a special member function that is automatically called in C++
programming when an object is going to be destroyed or exits its scope. The
name for a destructor in C++ is the same as the class name followed by the
tilde (~) symbol.
*/
//Syntax:
class MyClass {
public:
MyClass() { // Constructor
// Code
}
~MyClass() { // Destructor
// Code
}
};
//Example
#include <iostream>
using namespace std;
class MyClass {
public:
// Constructor
MyClass() {
cout << "Constructor called\n";
}
// Destructor
~MyClass() {
cout << "Destructor called\n";
}
};
int main() {
// Creating an object of MyClass
MyClass obj;
// The object goes out of scope when main() finishes
// The destructor is automatically called at this point
return 0;
}
/*
-------------------------
| Himel Sarder |
| Dept. Of CSE, BSFMSTU |
|------------------------
*/