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 the previous exercise so the
struct
is declared in a properly “guarded” header file, the definition is
in one
cpp
file and your
main( )
is 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
the previous exercise but move the functions so they are member functions of the
struct,
and test again.
Write
and compile a piece of code that performs data member selection and a 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
the previous exercise with
Stack.
Create
a file containing a function
f(
)
which takes an
int
argument and prints it to the console using the
printf(
)
function in
<stdio.h>
by saying:
printf(“%d\n”,
i)
where
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++, and 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 true (nonzero) 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 video tape 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 the previous exercise. 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
which
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.