-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut24.cpp
62 lines (52 loc) · 1.87 KB
/
tut24.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
#include <iostream>
using namespace std;
// Static Data Members in C++ :-
// When a static data member is created, there is only a single copy of the data member which is shared
// between all the objects of the class. As we have discussed in our previous lecture that if the data
// members are not static then every object has an individual copy of the data member and it is not shared.
// Static Methods in C++ :-
// When a static method is created, they become independent of any object and class.
// Static methods can only access static data members and static methods.
// Static methods can only be accessed using the scope resolution operator.
class Employee
{
int id;
static int count; // Bydefault the value of static variable is 0 and we can't
// change this here (i.e. at the time of declaration)
public:
void setdata(void)
{
cout << "Enter the id of employee number " << (count + 1) << " is "<< endl;
cin >> id;
count++;
}
void getdata(void)
{
cout << "The id of employee number " << count << " is " << id << endl;
}
static void getcount(void)
{
// cout<<id; //This throws an error because static function can only access
// static data member.
cout << "The value of count is " << count << endl;
}
};
// Count is the static data member of class Employee.
int Employee::count; // By default the value of static data member(or static variable
// or class variable) is 0. But we can change this value here.
int main()
{
Employee Eeena, Meena, Teeka;
// Eeena.id=1; //Cannot do this as id is private.
// Eeena.count=1; //Cannot do this as count is private.
Eeena.setdata();
Eeena.getdata();
Employee::getcount();
Meena.setdata();
Meena.getdata();
Employee::getcount();
Teeka.setdata();
Teeka.getdata();
Employee::getcount();
return 0;
}