Closing Remarks

Answers to Self Review Exercises

24.1

a) binary scope resolution (::). b) const_cast. c) global namespace. d) not_eq. e) multiple inheritance. f) virtual.

24.2
  1. False. It is legal to pass a non-const argument to a const function. However, when passing a const reference or pointer to a non-const function, the const_cast operator should be used to cast away the "const-ness" of the reference or pointer
  2. False. Programmers might inadvertently choose the namespace already in use.
  3. False. namespace bodies do not end in semicolons.
  4. False. namespaces can be nested.
  5. False. A mutable data member is always modifiable, even in a const member function.

Exercises

24.3

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

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

  1. namespace Name { int x; int y; mutable int z; };
  2. int integer = const_cast< int >( double );
  3. namespace PCM( 111, "hello" ); // construct namespace

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net