1: //namespace1.cpp
2: //Illustrates the definition of different variables with the same name
3: //in different namespaces, and attempts to access those variables.
5: #include <iostream>
6: #include <iomanip>
7: using namespace std;
9: #include "utilities.h"
10: using Scobey::Pause;
12: namespace MyStuff
13: {
14: int n = 5; //This is MyStuff::n.
16: } //<-- Note the absence of a semi-colon following the terminating brace.
19: namespace YourStuff
20: {
21: int n = 10; //This is YourStuff::n.
22: }
25: int n = 15; //This is a global n.
28: int main()
29: {
30: //using namespace MyStuff; //1
31: //using namespace YourStuff; //2
32: //using MyStuff::n; //3
33: //using YourStuff::n; //4
35: //int n = 20; //5
37: cout << "\nThe value of n is " << n << ".\n";
38: Pause(0, "", 1);
40: cout << "\nThe value of ::n is " << ::n << ".\n";
41: Pause(0, "", 2);
43: cout << "\nThe value of YourStuff::n is "
44: << YourStuff::n << ".\n";
45: Pause(0, "", 3);
47: cout << "\nThe value of MyStuff::n is "
48: << MyStuff::n << ".\n";
49: Pause(0, "", 4);
50: }
52: /*
53: Questions:
54: 1. What values or error are output if the program is run as is?
55: 2. What values or error are output if comment //1 is activated?
56: 3. What values or error are output if comment //2 is activated?
57: 4. What values or error are output if comment //3 is activated?
58: 5. What values or error are output if comment //4 is activated?
59: 6. What values or error are output if comment //5 is activated?
61: Answers:
62: 1. 15 15 10 5
63: 2. error C2872: 'n' : ambiguous symbol
64: 3. error C2872: 'n' : ambiguous symbol
65: 4. 5 15 10 5
66: 5. 10 15 10 5
67: 6. 20 15 10 5
68: */