-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount-number-of-object.cpp
72 lines (62 loc) · 2.09 KB
/
Count-number-of-object.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
63
64
65
66
67
68
69
70
71
72
/**
* @brief This program demonstrates the usage of constructors and destructors in a C++ class.
*
* The calc class is defined with a constructor and a destructor.
* The constructor increments a static counter each time an object is created,
* and the destructor decrements the counter each time an object is destroyed.
* The main function creates four instances of the calc class,
* which triggers the constructor and destructor for each object.
*
* The program outputs the number of objects created and destroyed at each step.
* This helps illustrate the concept of object lifetime management in C++.
*
* @return 0 - Indicates successful program execution.
*/
/*WAP in C++ which defines a class with constructor and destructor
which will count number of objects created and destroyed.*/
// Including Header File
#include <iostream>
// Using Namespace
using namespace std;
// Declaring Static Variable
int count = 0;
/// @brief Class for Calculation
class calc
{
public:
/**
* @brief Constructor for the calc class.
*
* This constructor increments the static count variable each time an object of the calc class is created.
* It then outputs the current number of objects created to the console.
*/
calc()
{
count++;
cout << "Number of objects created:" << count << endl;
}
/**
* @brief Destructor for the calc class.
*
* This destructor decrements the static count variable each time an object of the calc class is destroyed.
* It then outputs the current number of objects remaining to the console.
*/
~calc()
{
cout << "Number of objects destroyed:" << count << endl;
count--;
}
};
/**
* @brief The main function of the program.
*
* This function creates four instances of the calc class and then exits.
* The calc class has a constructor and destructor that keep track of the number of objects created and destroyed.
*
* @return 0 - Indicates successful program execution.
*/
int main()
{
calc c1, c2, c3, c4; // Creating four instances of the calc class
return 0;
}