|
|
|
|
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.
- Create a function with a
static variable that is a pointer (with a default argument of zero). When the
caller provides a value for this argument it is used to point at the beginning
of an array of int. If you call the function with a zero argument (using
the default argument), the function returns the next value in the array, until
it sees a “-1” value in the array (to act as an end-of-array
indicator). Exercise this function in
main( ).
- Create
a function that returns the next value in a Fibonacci sequence every time you
call it. Add an argument that is a bool with a default value of
false such that when you give the argument with true it
“resets” the function to the beginning of the Fibonacci sequence.
Exercise this function in
main( ).
- Create
a class that holds an array of ints. Set the size of the array using
static const int inside the class. Add a const int
variable, and initialize it in the constructor initializer list; make the
constructor inline. Add a static int member variable and
initialize it to a specific value. Add a static member function that
prints the static data member. Add an inline member function
called print( ) to print out all the values in the array and to call
the static member function. Exercise this class in
main( ).
- Create
a class called Monitor that keeps track of the number of times that its
incident( ) member function has been called. Add a
print( ) member function that displays the number of incidents. Now
create a global function (not a member function) containing a static
Monitor object. Each time you call the function it should call
incident( ), then print( )
member function to display the incident count. Exercise the
function in
main( ). - Modify
the Monitor class from Exercise 4 so that you can
decrement( ) the incident count. Make a class Monitor2 that
takes as a constructor argument a pointer to a Monitor1, and which stores
that pointer and calls incident( ) and print( ). In the
destructor for Monitor2, call decrement( ) and
print( ). Now make a static object of Monitor2 inside
a function. Inside main( ), experiment with calling the function and
not calling the function to see what happens with the destructor of
Monitor2.
- Make
a global object of Monitor2 and see what
happens.
- Create a
class with a destructor that prints a message and then calls
exit( ). Create a global object of this class and see what
happens.
- In
StaticDestructors.cpp, experiment with the order of constructor and
destructor calls by calling f( ) and g( ) inside
main( ) in different orders. Does your compiler get it
right?
- In
StaticDestructors.cpp, test the default error handling of your
implementation by turning the original definition of out into an
extern declaration and putting the actual definition after the definition
of a (whose Obj constructor sends information to out). Make
sure there’s nothing else important running on your machine when you run
the program or that your machine will handle faults
robustly.
- Prove that
file static variables in header files don’t clash with each other when
included in more than one cpp
file.
- Create a
simple class containing an int, a constructor that initializes the
int from its argument, a member function to set the int from its
argument, and a print( ) function that prints the int. Put
your class in a header file, and include the header file in two cpp
files. In one cpp file make an instance of your class, and in the other
declare that identifier extern and test it inside main( ).
Remember, you’ll have to link the two object files or else the linker
won’t find the
object.
- Make the
instance of the object in Exercise 11 static and verify that it cannot be
found by the linker because of
this.
- Declare a
function in a header file. Define the function in one cpp file and call
it inside main( ) in a second cpp file. Compile and verify
that it works. Now change the function definition so that it is static
and verify that the linker cannot find
it.
- Modify
Volatile.cpp from Chapter 8 to make comm::isr( ) something
that could actually work as an interrupt service routine. Hint: an interrupt
service routine doesn’t take any
arguments.
- Write and
compile a simple program that uses the auto and register
keywords.
- Create a
header file containing a namespace. Inside the namespace create
several function declarations. Now create a second header file that includes the
first one and continues the namespace, adding several more function
declarations. Now create a cpp file that includes the second header file.
Alias your namespace to another (shorter) name. Inside a function definition,
call one of your functions using scope resolution. Inside a separate function
definition, write a using directive to introduce your namespace into that
function scope, and show that you don’t need scope resolution to call the
functions from your
namespace.
- Create a
header file with an unnamed namespace. Include the header in two separate
cpp files and show that an unnamed space is unique for each translation
unit.
- Using the
header file from Exercise 17, show that the names in an unnamed namespace are
automatically available in a translation unit without
qualification.
- Modify
FriendInjection.cpp to add a definition for the friend function and to
call the function inside
main( ).
- In
Arithmetic.cpp, demonstrate that the using directive does not
extend outside the function in which the directive was
made.
- Repair the
problem in OverridingAmbiguity.cpp, first with scope resolution, then
instead with a using declaration that forces the compiler to choose one
of the identical function
names.
- In two header
files, create two namespaces, each containing a class (with all inline
definitions) with a name identical to that in the other namespace. Create a
cpp file that includes both header files. Create a function, and inside
the function use the using directive to introduce both namespaces. Try
creating an object of the class and see what happens. Make the using
directives global (outside of the function) to see if it makes any difference.
Repair the problem using scope resolution, and create objects of both
classes.
- Repair the
problem in Exercise 22 with a using declaration that forces the compiler
to choose one of the identical class
names.
- Extract the
namespace declarations in BobsSuperDuperLibrary.cpp and
UnnamedNamespaces.cpp and put them in separate header files, giving the
unnamed namespace a name in the process. In a third header file create a new
namespace that combines the elements of the other two namespaces with
using declarations. In main( ), introduce your new namespace
with a using directive and access all the elements of your
namespace.
- Create a
header file that includes <string> and <iostream> but
does not use any using directives or using declarations. Add
“include guards” as you’ve seen in the header files in this
book. Create a class with all inline functions that contains a string
member, with a constructor that initializes that string from its argument
and a print( ) function that displays the string. Create a
cpp file and exercise your class in
main( ).
- Create
a class containing a static double and long. Write a
static member function that prints out the
values.
- Create a
class containing an int, a constructor that initializes the int
from its argument, and a print( ) function to display the
int. Now create a second class that contains a static object of
the first one. Add a static member function that calls the static
object’s print( ) function. Exercise your class in
main( ).
- Create
a class containing both a const and a non-const static
array of int. Write static methods to print out the arrays.
Exercise your class in
main( ).
- Create
a class containing a string, with a constructor that initializes the
string from its argument, and a print( ) function to display
the string. Create another class that contains both const and
non-const static arrays of objects of the first class, and
static methods to print out these arrays. Exercise this second class in
main( ).
- Create a
struct that contains an int and a default constructor that
initializes the int to zero. Make this struct local to a function.
Inside that function, create an array of objects of your struct and
demonstrate that each int in the array has automatically been initialized
to zero.
- Create a
class that represents a printer connection, and that only allows you to have one
printer.
- In a header
file, create a class Mirror that contains two data members: a pointer to
a Mirror object and a bool. Give it two constructors: the default
constructor initializes the bool to true and the Mirror
pointer to zero. The second constructor takes as an argument a pointer to a
Mirror object, which it assigns to the object’s internal pointer;
it sets the bool to false. Add a member function
test( ): if the object’s pointer is nonzero, it returns the
value of test( ) called through the pointer. If the pointer is zero,
it returns the bool. Now create five cpp files, each of which
includes the Mirror header. The first cpp file defines a global
Mirror object using the default constructor. The second file declares the
object in the first file as extern, and defines a global Mirror
object using the second constructor, with a pointer to the first object.
Keep doing this until you reach the last file, which will also contain a global
object definition. In that file, main( ) should call the
test( ) function and report the result. If the result is
true, find out how to change the linking order for your linker and change
it until the result is
false.
- Repair
the problem in Exercise 32 using technique one shown in this
book.
- Repair the
problem in Exercise 32 using technique two shown in this
book.
- Without
including a header file, declare the function puts( ) from the
Standard C Library. Call this function from
main( ).
[47]Bjarne
Stroustrup and Margaret Ellis, The Annotated C++ Reference Manual,
Addison-Wesley, 1990, pp. 20-21.
|
|
|