-
Notifications
You must be signed in to change notification settings - Fork 7
/
static_variable.cpp
49 lines (40 loc) · 949 Bytes
/
static_variable.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
#include <iostream>
using namespace std;
class Employee
{
int id;
static int count;
public:
void setData(void)
{
cout << "Enter the id" << endl;
cin >> id;
count++;
}
void getData(void)
{
cout << "The id of this employee is " << id << " and this is employee number " << count << endl;
}
static void getCount(void){
// cout<<id; // throws an error
cout<<"The value of count is "<<count<<endl;
}
};
// Count is the static data member of class Employee
int Employee::count; // Default value is 0
int main()
{
Employee harry, rohan, lovish;
// harry.id = 1;
// harry.count=1; // cannot do this as id and count are private
harry.setData();
harry.getData();
Employee::getCount();
rohan.setData();
rohan.getData();
Employee::getCount();
lovish.setData();
lovish.getData();
Employee::getCount();
return 0;
}