Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in C++
Prev Contents / Index Next


Solutions to selected exercises can be found in the electronic document The Thinking in C++ Annotated Solution Guide, available for a small fee from

  1. Write a program that uses the F( ) macro shown at the beginning of the chapter and demonstrates that it does not expand properly, as described in the text. Repair the macro and show that it works correctly.
  2. Write a program that uses the FLOOR( ) macro shown at the beginning of the chapter. Show the conditions under which it does not work properly.
  3. Modify MacroSideEffects.cpp so that BAND( ) works properly.
  4. Create two identical functions, f1( ) and f2( ). Inline f1( ) and leave f2( ) as an non-inline function. Use the Standard C Library function clock( ) that is found in <ctime> to mark the starting point and ending points and compare the two functions to see which one is faster. You may need to make repeated calls to the functions inside your timing loop in order to get useful numbers.
  5. Experiment with the size and complexity of the code inside the functions in Exercise 4 to see if you can find a break-even point where the inline function and the non-inline function take the same amount of time. If you have them available, try this with different compilers and note the differences.
  6. Prove that inline functions default to internal linkage.
  7. Create a class that contains an array of char. Add an inline constructor that uses the Standard C library function memset( ) to initialize the array to the constructor argument (default this to ‘ ’), and an inline member function called print( ) to print out all the characters in the array.
  8. Take the NestFriend.cpp example from Chapter 5 and replace all the member functions with inlines. Make them non-in situ inline functions. Also change the initialize( ) functions to constructors.
  9. Modify StringStack.cpp from Chapter 8 to use inline functions.
  10. Create an enum called Hue containing red, blue, and yellow. Now create a class called Color containing a data member of type Hue and a constructor that sets the Hue from its argument. Add access functions to “get” and “set” the Hue. Make all of the functions inlines.
  11. Modify Exercise 10 to use the “accessor” and “mutator” approach.
  12. Modify Cpptime.cpp so that it measures the time from the time that the program begins running to the time when the user presses the “Enter” or “Return” key.
  13. Create a class with two inline member functions, such that the first function that’s defined in the class calls the second function, without the need for a forward declaration. Write a main that creates an object of the class and calls the first function.
  14. Create a class A with an inline default constructor that announces itself. Now make a new class B and put an object of A as a member of B, and give B an inline constructor. Create an array of B objects and see what happens.
  15. Create a large quantity of the objects from the previous Exercise, and use the Time class to time the difference between non-inline constructors and inline constructors. (If you have a profiler, also try using that.)
  16. Write a program that takes a string as the command-line argument. Write a for loop that removes one character from the string with each pass, and use the DEBUG( ) macro from this chapter to print the string each time.
  17. Correct the TRACE( ) macro as specified in this chapter, and prove that it works correctly.
  18. Modify the FIELD( ) macro so that it also contains an index number. Create a class whose members are composed of calls to the FIELD( ) macro. Add a member function that allows you to look up a field using its index number. Write a main( ) to test the class.
  19. Modify the FIELD( ) macro so that it automatically generates access functions for each field (the data should still be private, however). Create a class whose members are composed of calls to the FIELD( ) macro. Write a main( ) to test the class.
  20. Write a program that takes two command-line arguments: the first is an int and the second is a file name. Use require.h to ensure that you have the right number of arguments, that the int is between 5 and 10, and that the file can successfully be opened.
  21. Write a program that uses the IFOPEN( ) macro to open a file as an input stream. Note the creation of the ifstream object and its scope.
  22. (Challenging) Determine how to get your compiler to generate assembly code. Create a file containing a very small function and a main( ) that calls the function. Generate assembly code when the function is inlined and not inlined, and demonstrate that the inlined version does not have the function call overhead.

[45]Andrew Koenig goes into more detail in his book C Traps & Pitfalls (Addison-Wesley, 1989).

[46] Co-author with Tom Plum of C++ Programming Guidelines, Plum Hall, 1991.

Thinking in C++
Prev Contents / Index Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire