Sams Teach Yourself C++ in 21 Days

(singke) #1
Creating and Using Namespaces 657

18



  1. Consider the following code:
    int x = 4;
    int main()
    {
    for( y = 1; y < 10; y++)
    {
    cout << y << “:” << endl;
    {
    int x = 10 * y;
    cout << “X = “ << x << endl
    }
    }
    // HERE
    }
    What is the value of Xwhen this program reaches “HERE” in the listing?

  2. Can I use names defined in a namespace without using the usingkeyword?

  3. What are the major differences between normal and unnamed namespaces?

  4. What are the two forms of statements with the usingkeyword? What are the dif-
    ferences between those two forms?

  5. What are the unnamed namespaces? Why do we need unnamed namespaces?

  6. What is the standard namespace?


Exercises ........................................................................................................


1.BUG BUSTERS:What is wrong in this program?
0: #include <iostream>
1: int main()
2: {
3: cout << “Hello world!” << endl;
4: return 0;
5: }


  1. List three ways of fixing the problem found in Exercise 1.

  2. Show the code for declaring a namespace called MyStuff. This namespace should
    contain a class called MyClass.

Free download pdf