-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_variable.cpp
More file actions
46 lines (38 loc) · 858 Bytes
/
Copy pathstatic_variable.cpp
File metadata and controls
46 lines (38 loc) · 858 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
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
class Human
{
public:
// cannot initialize the static variable inside class
// static int human_count = 0;
static int human_count;
Human()
{
human_count++;
}
void humanTotal()
{
cout << "There are "<<human_count<<" peoples are in this program" <<endl;
}
static void humanCount();
};
// static data member can be declared globally also
int Human::human_count = 0;
void Human::humanCount()
{
cout << "There are "<<human_count<<" peoples are in this program " <<endl;
}
int main()
{
cout << Human::human_count<<endl;
Human anil;
Human anjali;
Human rashith;
Human sandeep;
// non static data member
anil.humanTotal();
// static data member
Human ::humanCount();
cout << Human::human_count<<endl;
return 0;
}