-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstatic_members_I.cpp
63 lines (51 loc) · 1.43 KB
/
static_members_I.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 <bits/stdc++.h>
using namespace std;
/*
static members
-static data member :
--Associated with class not with object
--shared by all objects of a class
--Needs to be defined outside the class scope (in addition to the declaration within the class scope) to avoid linker error.
--Must be initialized in a source file
--is constructed before main() starts and destructed after main() ends
--can be private/public type
--can be accessed (a) with the class-name followed by the scope resolution operator(::)
(b) as a member of any object of the class
-- virtually eliminates any need for global variables in OOPs environment
*/
class MyClass {
int x; //non static
static int y; //static : Belongs to class
public:
static int z;
void get() {
x = 15;
y = 15;
}
void print() {
x += 10;
y += 10;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
};
int MyClass::y = 0; //Defined static data member (must be defined in global scope)
int MyClass::z = 0;
//static data members are initialized during program setup
int main() {
MyClass obj1;
MyClass obj2;
cout << MyClass::z << endl;
obj1.get();
obj2.get();
obj1.print();
obj2.print();
/*
Output:
x = 25
y = 25
x = 25
y = 35
*/
return 0;
}