Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Exercises

25.3Fill 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.

25.4Write 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.
25.5Given the namespaces in Fig. 25.15, determine whether each statement is true or false. Explain any false answers.
  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.

Fig. 25.15. namespaces for Exercise 25.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

25.6Compare 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.]
25.7Write a program that uses const_cast to modify a const variable. [Hint: Use a pointer in your solution to point to the const identifier.]
25.8What problem do virtual base classes solve?
25.9Write 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?
25.10Find 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


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint