|
|
|
|
Exercises
Solutions to selected exercises
can be found in the electronic document The Thinking in C++ Annotated
Solution Guide, available for a small fee from
www.BruceEckel.com.
- 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.
- 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.
- Modify
MacroSideEffects.cpp so that BAND( ) works
properly.
- 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.
- 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.
- Prove
that inline functions default to internal
linkage.
- 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.
- 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.
- Modify
StringStack.cpp from Chapter 8 to use inline
functions.
- 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.
- Modify
Exercise 10 to use the “accessor” and “mutator”
approach.
- 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.
- 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.
- 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.
- 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.)
- 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.
- Correct the
TRACE( ) macro as specified in this chapter, and prove that it works
correctly.
- 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.
- 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.
- 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.
- 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.
- (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.
|
|
|