-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticvariableex.cpp
More file actions
74 lines (51 loc) · 1.05 KB
/
staticvariableex.cpp
File metadata and controls
74 lines (51 loc) · 1.05 KB
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
73
74
#include <iostream>
using namespace std;
void AddToCount()
{
static int count = 0; //cannot access count from outside the function
count++;
cout << count << endl;
}
class Item
{
public:
Item()
{
cout << "item has been created" << endl;
}
~Item() {
cout << " an item has been destroyed" << endl;
}
};
class Critter {
public:
static int CritterCount;
const int flag = 0;
Critter() {
cout << " A critter is born" << endl;
CritterCount++;
}
static void AnnounceCount() {
cout << CritterCount << endl;
}
};
int Critter::CritterCount = 0; // this is how to initialize a static variable and it must be done outside the class
int main() {
//AddToCount();
//AddToCount();
Critter::CritterCount = 13;
cout << Critter::CritterCount << endl;
{
static Item item;
}
Critter crit;
cout << Critter::CritterCount << endl;
Critter::AnnounceCount();
Critter* crit2 = new Critter;
delete crit2;
/*for (int i = 0; i < 100; i++)
{
AddToCount();
}*/
// count--; // cannot access it from outside the AddToCount function
}