-
Notifications
You must be signed in to change notification settings - Fork 14
/
StaticClass.cpp
55 lines (39 loc) · 1.08 KB
/
StaticClass.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
//static variable and static member methods
#include<iostream>
using namespace std;
class Human {
public:
static int scount;//cannot be defined inside a class
int count=0;
//constructor
Human() {
scount++;
count++;
cout<<"Number of static humans"<<" "<<scount<<endl;
}
//destructor
~Human()
{
cout<<endl;
cout<<"There are now "<<scount<<" humans reamining"<<endl;
scount--;
}
static void totalHuman() {
cout<<"There are "<<scount<< " "<< "people"<<endl;
// cout<<"There are "<<count<< " "<< "people"<<endl;-Produces Error as static member function can only access, static class variable
}
};
//static variable needs to be defined outside a class defination
int Human :: scount=0;
int main () {
//only 1 copy of static variable created and shared amongst all the objects.
Human anish;
Human mrinal;
Human lucky;
Human vaibhav;
//4 objects- the constructor is called 4 times and scount(static variable) becomes 4
Human :: totalHuman();
//can access static variables and methods without using an object of class.
cout<< Human :: scount;
return 0;
}