-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScope_resolution_and_Block_scope.cpp
113 lines (93 loc) · 3.28 KB
/
Scope_resolution_and_Block_scope.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
// author: jaydattpatel
#include<iostream>
using namespace std;
int glob = 7;
void print()
{
cout<<"print() :"<<glob<<endl;
}
class X
{
public:
static int count;
};
int X::count = 10; // define static data member count of class X.
int main()
{
cout<<"-------\"::\" Globle scope resolution --------------\n";
int glob = 5;
cout<<"Local glob : "<<glob<<endl; // "first find "glob" variable in Local if it is found then it will take local varibale otherwise it will take globle variable
print();
cout<<"(::globe) : "<<::glob<<endl; // "::" you can use scope resolution to use globle varible due to same name of variables.
int X = 0; // hides class type X
cout << "X::count : "<<X::count << endl; // use static member of class X
cout << X << endl;
int m = 123;
cout<<"-------\"{}\" Block scope --------------\n";
cout << "main m: " << m << endl;
{
int m = 1;
cout << "{inner m}: " << m << endl;
{
int m = 2;
cout << "{{inner of inner m}}: " << m << endl;
}
cout << "{inner m}: " << m << endl;
}
cout << "main m: " << m << endl;
cout<<"-------\"{}\" Block scope (If or If else if else ) --------------\n";
cout << "main m: " << m << endl;
if(1)
{
int m = 3;
cout << "{inner m}: " << m << endl;
{
int m = 4;
cout << "{{inner of inner m}}: " << m << endl;
}
cout << "{inner m}: " << m << endl;
}
else
{
int m = 5;
cout << "{inner m}: " << m << endl;
{
int m = 6;
cout << "{{inner of inner m}}: " << m << endl;
}
cout << "{inner m}: " << m << endl;
}
cout << "main m: " << m << endl;
cout<<"-------\"{}\" Block scope(for loop) --------------\n";
cout << "main m: " << m << endl;
{
int m = 7;
cout << "{inner m}: " << m << endl;
{
int m=8;
cout << "{{inner of inner m}}: " << m << endl;
for(int m=0;m<2;m++)
{
cout<<"{{{inner of inner of inner m}}}: "<<m<<endl;
}
cout << "{{inner of inner m}}: " << m << endl;
}
cout << "{inner m}: " << m << endl;
}
cout << "main m: " << m << endl;
cout<<"-------\"{}\" Block scope(while or do while loop) --------------\n";
cout << "main m: " << m << endl;
{
int m = 9;
cout << "{inner m}: " << m << endl;
{
int m=0;
while(m<10)
m++;
cout << "{{inner of inner m}}: " << m << endl;
}
cout << "{inner m}: " << m << endl;
}
cout << "main m: " << m << endl;
return 0;
}