Main Page

Previous Page
Next Page

[Page 1226 (continued)]



Fill in the blanks for each of the following:

  1. Keyword _____________ specifies that a namespace or namespace member is being used.

  2. Operator _____________ is the operator keyword for logical OR.

  3. Storage specifier_____________ allows a member of a const object to be modified.

  4. The _____________ qualifier specifies that an object can be modified by other programs.

  5. Precede a member with its _____________ name and the scope resolution operator _____________ if the possibility exists of a scoping conflict.

  6. The body of a namespace is delimited by _____________.

  7. For a const object with no _____________ data members, operator _____________ must be used every time a member is to be modified.


Write a namespace, Currency, that defines constant members ONE, TWO, FIVE, TEN, TWENTY, FIFTY and HUNDRED. Write two short programs that use Currency. One program should make all constants available and the other should make only FIVE available.


Given the namespaces in Fig. 24.15, determine whether each statement is true or false. Explain any false answers.

Figure 24.15. namespaces for Exercise 24.5.

 1  namespace CountryInformation
 2  {
 3     using namespace std;
 4     enum Countries { POLAND, SWITZERLAND, GERMANY,
 5                      AUSTRIA, CZECH_REPUBLIC };
 6     int kilometers;
 7     string string1;
 9     namespace RegionalInformation
10     {
11        short getPopulation(); // assume definition exists
12        MapData map; // assume definition exists
13     } // end RegionalInformation
14  } // end CountryInformation
16  namespace Data
17  {
18     using namespace CountryInformation::RegionalInformation;
19     void *function( void *, int );
20  } // end Data

    [Page 1227]
  1. Variable kilometers is visible within namespace Data.

  2. Object string1 is visible within namespace Data.

  3. Constant POLAND is not visible within namespace Data.

  4. Constant GERMANY is visible within namespace Data.

  5. Function function is visible to namespace Data.

  6. Namespace Data is visible to namespace CountryInformation.

  7. Object map is visible to namespace CountryInformation.

  8. Object string1 is visible within namespace RegionalInformation.


Compare and contrast mutable and const_cast. Give at least one example of when one might be preferred over the other. [Note: This exercise does not require any code to be written.]


Write a program that uses const_cast to modify a const variable. [Hint: Use a pointer in your solution to point to the const identifier.]


What problem do virtual base classes solve?


Write a program that uses virtual base classes. The class at the top of the hierarchy should provide a constructor that takes at least one argument (i.e., do not provide a default constructor). What challenges does this present for the inheritance hierarchy?


Find the error(s) in each of the following. When possible, explain how to correct each error.

  1. namespace Name {
       int x;
       int y;
       mutable int z;

  2. int integer = const_cast< int >( double );

  3. namespace PCM( 111, "hello" );    // construct namespace

Previous Page
Next Page