-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz_18.17.cpp
197 lines (162 loc) · 5.55 KB
/
quiz_18.17.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* 实际编写代码检验你对上一题的回答是否正确。
*/
#include <iostream>
using std::cout;
using std::endl;
// using declarations for all the members of namespace Exercise
// are located at the location labeled position 1.
namespace Test0
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
} // namespace Exercise
int ivar = 0;
// using Exercise::ivar;
// error C2874: using-declaration causes a multiple declaration of 'Test0::Exercise::ivar'
// So we delete it to make the program compile.
using Exercise::dvar;
using Exercise::limit;
void manip()
{
double dvar = 3.1416;
cout << "********** Before call Test0::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test0::ivar " << Test0::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
++ivar; // ++ Test0::ivar
++Test0::ivar; // ++ Test0::ivar
cout << "********** After call Test0::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test0::ivar " << Test0::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
} // namespace Test0
// using declarations for all the members of namespace Exercise
// are located at the location labeled position 2.
namespace Test1
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
} // namespace Exercise
int ivar = 0;
void manip()
{
using Exercise::ivar;
// using Exercise::dvar;
// This using declaration causes a redefinition of Test1::Exercise::dvar.
// So we delete it to make the program compile.
using Exercise::limit;
double dvar = 3.1416;
cout << "********** Before call Test1::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test1::ivar " << Test1::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
++ivar; // ++ Exercise::ivar
++Test1::ivar; // ++ Test1::ivar
cout << "********** After call Test1::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test1::ivar " << Test1::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
} // namespace Test1
// using directive for namespace Exercise is located at the
// location labeled position 1.
namespace Test2
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
} // namespace Exercise
int ivar = 0;
using namespace Exercise;
void manip()
{
double dvar = 3.1416;
cout << "********** Before call Test2::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test2::ivar " << Test2::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
// ++ivar;
// error C2872: 'ivar' : ambiguous symbol
// could be int Test2::ivar or int Test2::Exercise::ivar
++Test2::ivar; // ++ Test2::ivar
cout << "********** After call Test2::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test2::ivar " << Test2::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
} // namespace Test2
// using directive for namespace Exercise is located at the
// location labeled position 2.
namespace Test3
{
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
} // namespace Exercise
int ivar = 0;
void manip()
{
using namespace Exercise;
double dvar = 3.1416;
cout << "********** Before call Test3::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test3::ivar " << Test3::ivar << endl
<< "dvar in function manip" << dvar << endl;
int iobj = limit + 1; // Exercise::limit + 1
// ++ivar;
// error C2872: 'ivar' : ambiguous symbol
// could be int Test3::ivar or int Test3::Exercise::ivar
++Test3::ivar; // ++ Test3::ivar
cout << "********** After call Test3::manip **********" << endl;
cout << "Exercise::ivar " << Exercise::ivar << endl
<< "Exercise::dvar " << Exercise::dvar << endl
<< "Exercise::limit " << Exercise::limit << endl
<< "Test3::ivar " << Test3::ivar << endl
<< "dvar in function manip " << dvar << endl
<< "iboj in function manip " << iobj << endl;
}
} // namespace Test3
int main()
{
Test0::manip();
cout << endl;
Test1::manip();
cout << endl;
Test2::manip();
cout << endl;
Test3::manip();
cout << endl;
return 0;
}