|
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 class with
public, private, and protected data members and function
members. Create an object of this class and see what kind of compiler messages
you get when you try to access all the class
members.
- Write a
struct called Lib that contains three string objects
a, b, and c. In main( ) create a Lib
object called x and assign to x.a, x.b, and x.c.
Print out the values. Now replace a, b, and c with an array
of string s[3]. Show that your code in main( ) breaks as a
result of the change. Now create a class called Libc, with
private string objects a, b, and c, and
member functions seta( ), geta( ), setb( ),
getb( ), setc( ), and getc( ) to set and get
the values. Write main( ) as before. Now change the private
string objects a, b, and c to a private array
of string s[3]. Show that the code in main( ) does not
break as a result of the
change.
- Create a
class and a global friend function that manipulates the private
data in the
class.
- Write two
classes, each of which has a member function that takes a pointer to an object
of the other class. Create instances of both objects in main( ) and
call the aforementioned member function in each
class.
- Create three
classes. The first class contains private data, and grants friendship to
the entire second class and to a member function of the third class. In
main( ), demonstrate that all of these work
correctly.
- Create a
Hen class. Inside this, nest a Nest class. Inside Nest,
place an Egg class. Each class should have a display( )
member function. In main( ), create an instance of each class and
call the display( ) function for each
one.
- Modify Exercise
6 so that Nest and Egg each contain private data. Grant
friendship to allow the enclosing classes access to this private
data.
- Create a class
with data members distributed among numerous public, private, and
protected sections. Add a member function showMap( ) that
prints the names of each of these data members and their addresses. If possible,
compile and run this program on more than one compiler and/or computer and/or
operating system to see if there are layout differences in the
object.
- Copy the
implementation and test files for Stash in Chapter 4 so that you can
compile and test Stash.h in this
chapter.
- Place
objects of the Hen class from Exercise 6 in a Stash. Fetch them
out and print them (if you have not already done so, you will need to add
Hen::print( )).
- Copy
the implementation and test files for Stack in Chapter 4 so that you can
compile and test Stack2.h in this
chapter.
- Place
objects of the Hen class from Exercise 6 in a Stack. Fetch them
out and print them (if you have not already done so, you will need to add
Hen::print( )).
- Modify
Cheshire in Handle.cpp, and verify that your project manager
recompiles and relinks only this file, but doesn’t recompile
UseHandle.cpp.
- Create
a StackOfInt class (a stack that holds ints) using the
“Cheshire cat” technique that hides the low-level data structure you
use to store the elements in a class called StackImp. Implement two
versions of StackImp: one that uses a fixed-length array of int,
and one that uses a vector<int>. Have a preset maximum size for the
stack so you don’t have to worry about expanding the array in the first
version. Note that the StackOfInt.h class doesn’t have to change
with
StackImp.
[36]
As noted before, sometimes access control is referred to as
encapsulation.
[37]
This name is attributed to John Carolan, one of the early pioneers in C++, and
of course, Lewis Carroll. This technique can also be seen as a form of the
“bridge” design pattern, described in Volume 2.
|
|