|
|
|
|
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.
- Turn the “bird &
rock” code fragment at the beginning of this chapter into a C program
(using structs for the data types), and show that it compiles. Now try to
compile it with the C++ compiler and see what
happens.
- Take the
code fragments in the beginning of the section titled “References in
C++” and put them into a main( ). Add statements to print
output so that you can prove to yourself that references are like pointers that
are automatically
dereferenced.
- Write
a program in which you try to (1) Create a reference that is not initialized
when it is created. (2) Change a reference to refer to another object after it
is initialized. (3) Create a NULL
reference.
- Write a
function that takes a pointer argument, modifies what the pointer points to, and
then returns the destination of the pointer as a
reference.
- Create a
class with some member functions, and make that the object that is pointed to by
the argument of Exercise 4. Make the pointer a const and make some of the
member functions const and prove that you can only call the const
member functions inside your function. Make the argument to your function a
reference instead of a
pointer.
- Take the
code fragments at the beginning of the section titled “Pointer
references” and turn them into a
program.
- Create a
function that takes an argument of a reference to a pointer to a pointer and
modifies that argument. In main( ), call the
function.
- Create a
function that takes a char& argument and modifies that argument. In
main( ), print out a char variable, call your function for
that variable, and print it out again to prove to yourself that it has been
changed. How does this affect program
readability?
- Write a
class that has a const member function and a non-const member
function. Write three functions that take an object of that class as an
argument; the first takes it by value, the second by reference, and the third by
const reference. Inside the functions, try to call both member functions
of your class and explain the
results.
- (Somewhat
challenging) Write a simple function that takes an int as an argument,
increments the value, and returns it. In main( ), call your
function. Now discover how your compiler generates assembly code and trace
through the assembly statements so that you understand how arguments are passed
and returned, and how local variables are indexed off the
stack.
- Write a
function that takes as its arguments a char, int, float,
and double. Generate assembly code with your compiler and find the
statements that push the arguments on the stack before a function
call.
- Write a
function that returns a double. Generate assembly code and determine how
the value is
returned.
- Produce
assembly code for PassingBigStructures.cpp. Trace through and demystify
the way your compiler generates code to pass and return large
structures.
- Write a
simple recursive function that decrements its argument and returns zero if the
argument becomes zero, otherwise it calls itself. Generate assembly code for
this function and explain how the way that the assembly code is created by the
compiler supports
recursion.
- Write
code to prove that the compiler automatically synthesizes a copy-constructor if
you don’t create one yourself. Prove that the synthesized copy-constructor
performs a bitcopy of primitive types and calls the copy-constructor of
user-defined
types.
- Write a class
with a copy-constructor that announces itself to cout. Now create a
function that passes an object of your new class in by value and another one
that creates a local object of your new class and returns it by value. Call
these functions to prove to yourself that the copy-constructor is indeed quietly
called when passing and returning objects by value.
- Create a class that
contains a double*. The constructor initializes the double* by
calling new double and assigning a value to the resulting storage from
the constructor argument. The destructor prints the value that’s pointed
to, assigns that value to -1, calls delete for the storage, and then sets
the pointer to zero. Now create a function that takes an object of your class by
value, and call this function in main( ). What happens? Fix the
problem by writing a
copy-constructor.
- Create
a class with a constructor that looks like a copy-constructor, but that has an
extra argument with a default value. Show that this is still used as the
copy-constructor.
- Create
a class with a copy-constructor that announces itself. Make a second class
containing a member object of the first class, but do not create a
copy-constructor. Show that the synthesized copy-constructor in the second class
automatically calls the copy-constructor of the first
class.
- Create a very
simple class, and a function that returns an object of that class by value.
Create a second function that takes a reference to an object of your class. Call
the first function as the argument of the second function, and demonstrate that
the second function must use a const reference as its
argument.
- Create a
simple class without a copy-constructor, and a simple function that takes an
object of that class by value. Now change your class by adding a private
declaration (only) for the copy-constructor. Explain what happens when your
function is
compiled.
- This
exercise creates an alternative to using the copy-constructor. Create a class
X and declare (but don’t define) a private copy-constructor.
Make a public clone( ) function as a const member function
that returns a copy of the object that is created using new. Now write a
function that takes as an argument a const X& and clones a local copy
that can be modified. The drawback to this approach is that you are responsible
for explicitly destroying the cloned object (using delete) when
you’re done with
it.
- Explain
what’s wrong with both Mem.cpp and MemTest.cpp from Chapter
7. Fix the
problem.
- Create a
class containing a double and a print( ) function that prints
the double. In main( ), create pointers to members for both
the data member and the function in your class. Create an object of your class
and a pointer to that object, and manipulate both class elements via your
pointers to members, using both the object and the pointer to the
object.
- Create a
class containing an array of int. Can you index through this array using
a pointer to
member?
- Modify
PmemFunDefinition.cpp by adding an overloaded member function
f( ) (you can determine the argument list that causes the overload).
Now make a second pointer to member, assign it to the overloaded version of
f( ), and call the function through that pointer. How does the
overload resolution happen in this
case?
- Start with
FunctionTable.cpp from Chapter 3. Create a class that contains a
vector of pointers to functions, with add( ) and
remove( ) member functions to add and remove pointers to functions.
Add a run( ) function that moves through the vector and calls
all of the
functions.
- Modify
the above Exercise 27 so that it works with pointers to member functions
instead.
[48]
Thanks to Owen Mortensen for this example
|
|
|