-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34destructors.cpp
More file actions
37 lines (34 loc) · 999 Bytes
/
Copy path34destructors.cpp
File metadata and controls
37 lines (34 loc) · 999 Bytes
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
#include <iostream>
using namespace std;
// Destructors never takes any argument or returns any value
// It is used to clear storage from dynamically allocated objects
static int count = 0;
class num
{
public:
num()
{
count++;
cout << "The number of object that called the constructor is = " << count << endl;
}
~num() // this is a destructor that uses tilda ~ before a constructor like setup except arguments
{
cout << "The number of object that called the destructor is = " << count << endl;
count--;
}
};
int main()
{
// this is a representation of constructor call and destructor call
cout << "Entering the main function" << endl;
cout << "Creating an object " << endl;
num n1;
{
cout << "Entering a block" << endl;
cout << "Creating 2 more block objects" << endl;
num n2, n3;
cout << "Exiting the block" << endl;
}
cout << "Exiting the main function" << endl;
return 0;
}