1 #include<iostream>
2 using namespace std;
3
4 int cc=0;
5 class test
6 {
7 public:
8 test() // constructor
9 {
10 cc++;
11 cout<<"nNo. of objects created: "<<cc;
12 }
13
14 ~test() // destructor
15 {
16 cout<<"nNo. of objects destroyed: "<<cc;
17 cc--;
18 }
19
20 };
21
22 int main()
23 {
24 cout<<"nEnter MAIN";
25 test t1, t2, t3, t4;
26
27 // block 1
28 {
29 cout<<"nnEnter Block 1: ";
30 test t5;
31 }
32
33 // block 2
34 {
35 cout<<"nnEnter Block 2: ";
36 test t7;
37 cout <<"nvariable t7===n";
38 }
39
40 cout<<"nnRE-Enter MAIN";
41
42 return 0;
43 }

DESTRUCTOR EXAMPLES

  • 1.
    1 #include<iostream> 2 usingnamespace std; 3 4 int cc=0; 5 class test 6 { 7 public: 8 test() // constructor 9 { 10 cc++; 11 cout<<"nNo. of objects created: "<<cc; 12 } 13 14 ~test() // destructor 15 { 16 cout<<"nNo. of objects destroyed: "<<cc; 17 cc--; 18 } 19 20 }; 21 22 int main() 23 { 24 cout<<"nEnter MAIN"; 25 test t1, t2, t3, t4; 26 27 // block 1 28 { 29 cout<<"nnEnter Block 1: "; 30 test t5; 31 } 32 33 // block 2 34 { 35 cout<<"nnEnter Block 2: "; 36 test t7; 37 cout <<"nvariable t7===n"; 38 } 39 40 cout<<"nnRE-Enter MAIN"; 41 42 return 0; 43 }