-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_key_word.cpp
126 lines (99 loc) · 2.02 KB
/
static_key_word.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
using namespace std;
void normal_var_demo(){
int i = 0;
cout << i << endl;
i++;
}
void static_var_demo(){
static int i = 0;
cout << i << endl;
i++;
}
class static_exp{
public:
static int i;
static_exp();
~static_exp();
static void print_msg();
static_exp& test();
private:
int var;
static float stat_var;
};
static_exp::static_exp(){
var = 10;
//stat_var = 20.03; //gives error
cout << "inside constructor" << endl;
}
static_exp::~static_exp(){
cout << "inside destructor" << endl;
}
int static_exp::i = 1;
float static_exp::stat_var = 20.0;
void static_exp::print_msg(){
cout << "inside static member functions" << endl;
//cout << var << endl; // trying to access normal member variable from static member function (gives error)
cout << stat_var << endl; // trying to access static member variable from static member function
}
static_exp& static_exp::test(){
static_exp::i++;
cout << static_exp::i << " ";
return *this;
}
void normal_class_obj_test(){
int count = 0;
if (count == 0){
static_exp se1;
static_exp se2;
/*
se1.i = 1;
se2.i = 2; gives error "undefined referece to static_exp::i" */
cout << se1.i << endl;}
}
void static_class_obj_test(){
int count = 0;
if (count == 0){
static static_exp se1;
cout << se1.i << endl;
}
}
class base{
private:
int x;
public:
base(int x_){
x = x_;
}
int get(){
return x;
}
};
class derive{
private:
static base b_obj;
public:
static int get(){
return b_obj.get();
}
};
base derive::b_obj(0);
int main(){
cout << "normal variable"<<endl;
for (int i = 0; i < 3; i++)
normal_var_demo();
cout << "static variable" << endl;
for (int i = 0; i < 3; i++)
static_var_demo();
cout << "class experimentations"<<endl;
normal_class_obj_test();
static_class_obj_test();
static_exp::print_msg();
static_exp se_obj;
se_obj.test().test().test().test();
cout << endl;
derive d_obj;
cout << d_obj.get() << endl;
cout << "end of main" << endl;
return 0;
}