24.3 |
Fill in the blanks for each of the following:
- Keyword _____________ specifies that a namespace or namespace member is being used.
- Operator _____________ is the operator keyword for logical OR.
- Storage specifier_____________ allows a member of a const object to be modified.
- The _____________ qualifier specifies that an object can be modified by other programs.
- Precede a member with its _____________ name and the scope resolution operator _____________ if the possibility exists of a scoping conflict.
- The body of a namespace is delimited by _____________.
- For a const object with no _____________ data members, operator _____________ must be used every time a member is to be modified.
|
24.4 |
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.
|
24.5 |
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;
8
9 namespace RegionalInformation
10 {
11 short getPopulation(); // assume definition exists
12 MapData map; // assume definition exists
13 } // end RegionalInformation
14 } // end CountryInformation
15
16 namespace Data
17 {
18 using namespace CountryInformation::RegionalInformation;
19 void *function( void *, int );
20 } // end Data
|
- Variable kilometers is visible within namespace Data.
- Object string1 is visible within namespace Data.
- Constant POLAND is not visible within namespace Data.
- Constant GERMANY is visible within namespace Data.
- Function function is visible to namespace Data.
- Namespace Data is visible to namespace CountryInformation.
- Object map is visible to namespace CountryInformation.
- Object string1 is visible within namespace RegionalInformation.
|
24.6 |
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.]
|
24.7 |
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.]
|
24.8 |
What problem do virtual base classes solve?
|
24.9 |
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?
|
24.10 |
Find the error(s) in each of the following. When possible, explain how to correct each error.
-
namespace Name {
int x;
int y;
mutable int z;
};
-
int integer = const_cast< int >( double );
-
namespace PCM( 111, "hello" ); // construct namespace
|