|
|
|
|
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
https://www.BruceEckel.com.
- In the Standard C library,
the function puts( ) prints a char array to the console (so you can
say puts("hello")). Write a C program that uses puts( ) but
does not include <stdio.h> or otherwise declare the function.
Compile this program with your C compiler. (Some C++ compilers are not distinct
from their C compilers; in this case you may need to discover a command-line
flag that forces a C compilation.) Now compile it with the C++ compiler and note
the
difference.
- Create a
struct declaration with a single member function, then create a
definition for that member function. Create an object of your new data type, and
call the member
function.
- Change
your solution to Exercise 2 so the struct is declared in a properly
“guarded” header file, with the definition in one cpp file
and your main( ) in
another.
- Create a
struct with a single int data member, and two global functions,
each of which takes a pointer to that struct. The first function has a
second int argument and sets the struct’s int to the
argument value, the second displays the int from the struct. Test
the functions.
- Repeat Exercise 4
but move the functions so they are member functions of the struct, and
test again.
- Create a
class that (redundantly) performs data member selection and a member function
call using the this keyword (which refers to the address of the current
object).
- Make a
Stash that holds doubles. Fill it with 25 double values,
then print them out to the
console.
- Repeat
Exercise 7 with
Stack.
- Create
a file containing a function f( ) that takes an int argument
and prints it to the console using the printf( ) function in
<stdio.h> by saying: printf(“%d\n”, i) in
which i is the int you wish to print. Create a separate file
containing main( ), and in this file declare f( ) to
take a float argument. Call f( ) from inside
main( ). Try to compile and link your program with the C++ compiler
and see what happens. Now compile and link the program using the C compiler, and
see what happens when it runs. Explain the
behavior.
- Find out
how to produce assembly language from your C and C++ compilers. Write a function
in C and a struct with a single member function in C++. Produce assembly
language from each and find the function names that are produced by your C
function and your C++ member function, so you can see what sort of name
decoration occurs inside the
compiler.
- Write a
program with conditionally-compiled code in main( ), so that when a
preprocessor value is defined one message is printed, but when it is not defined
another message is printed. Compile this code experimenting with a
#define within the program, then discover the way your compiler takes
preprocessor definitions on the command line and experiment with
that.
- Write a
program that uses assert( ) with an argument that is always false
(zero) to see what happens when you run it. Now compile it with #define
NDEBUG and run it again to see the
difference.
- Create
an abstract data type that represents a videotape in a video rental store. Try
to consider all the data and operations that may be necessary for the Video
type to work well within the video rental management system. Include a
print( ) member function that displays information about the
Video.
- Create
a Stack object to hold the Video objects from Exercise 13. Create
several Video objects, store them in the Stack, then display them
using
Video::print( ).
- Write
a program that prints out all the sizes for the fundamental data types on your
computer using
sizeof.
- Modify
Stash to use a vector<char> as its underlying data
structure.
- Dynamically
create pieces of storage of the following types, using new: int,
long, an array of 100 chars, an array of 100 floats. Print
the addresses of these and then free the storage using
delete.
- Write
a function that takes a char* argument. Using new, dynamically
allocate an array of char that is the size of the char array
that’s passed to the function. Using array indexing, copy the characters
from the argument to the dynamically allocated array (don’t forget the
null terminator) and return the pointer to the copy. In your
main( ), test the function by passing a static quoted character
array, then take the result of that and pass it back into the function. Print
both strings and both pointers so you can see they are different storage. Using
delete, clean up all the dynamic
storage.
- Show an
example of a structure declared within another structure (a nested
structure). Declare data members in both structs, and declare and
define member functions in both structs. Write a main( ) that
tests your new
types.
- How big is a
structure? Write a piece of code that prints the size of various structures.
Create structures that have data members only and ones that have data members
and function members. Then create a structure that has no members at all. Print
out the sizes of all these. Explain the reason for the result of the structure
with no data members at
all.
- C++
automatically creates the equivalent of a typedef for structs, as
you’ve seen in this chapter. It also does this for enumerations and
unions. Write a small program that demonstrates
this.
- Create a
Stack that holds Stashes. Each Stash will hold five lines
from an input file. Create the Stashes using new. Read a file into
your Stack, then reprint it in its original form by extracting it from
the Stack.
- Modify Exercise 22
so that you create a struct that encapsulates the Stack of
Stashes. The user should only add and get lines via member functions, but
under the covers the struct happens to use a Stack of
Stashes.
- Create
a struct that holds an int and a pointer to another instance of
the same struct. Write a function that takes the address of one of these
structs and an int indicating the length of the list you want
created. This function will make a whole chain of these structs (a
linked list), starting from the argument (the head of the list),
with each one pointing to the next. Make the new structs using
new, and put the count (which object number this is) in the int.
In the last struct in the list, put a zero value in the pointer to
indicate that it’s the end. Write a second function that takes the head of
your list and moves through to the end, printing out both the pointer value and
the int value for each
one.
- Repeat Exercise
24, but put the functions inside a struct instead of using
“raw” structs and
functions.
[33]
This term can cause debate. Some people use it as defined here; others use it to
describe access control, discussed in the following
chapter.
[34]
To write a function definition for a function that takes a true variable
argument list, you must use varargs, although these should be avoided in
C++. You can find details about the use of varargs in your C
manual.
[35]
However, in Standard C++ file static is a deprecated feature.
|
|
|