|
|
|
|
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 simple class with
an overloaded operator++. Try calling this operator in both pre- and
postfix form and see what kind of compiler warning you
get.
- Create a simple
class containing an int and overload the operator+ as a member
function. Also provide a print( ) member function that takes an
ostream& as an argument and prints to that ostream&.
Test your class to show that it works
correctly.
- Add a
binary operator- to Exercise 2 as a member function. Demonstrate that you
can use your objects in complex expressions like
a + b –
c. - Add an
operator++ and operator-- to Exercise 2, both the prefix and the
postfix versions, such that they return the incremented or decremented object.
Make sure that the postfix versions return the correct value.
- Modify the
increment and decrement operators in Exercise 4 so that the prefix versions
are return a non-const reference and the
postfix versions are return a const
object. Show that they work correctly and explain why this would be done in
practice. - Change the
print( ) function in Exercise 2 so that it is the overloaded
operator<< as in
IostreamOperatorOverloading.cpp.
- Modify
Exercise 3 so that the operator+ and operator- are non-member
functions. Demonstrate that they still work
correctly.
- Add the
unary operator- to Exercise 2 and demonstrate that it works
correctly.
- Create a
class that contains a single private char. Overload the iostream
operators << and >> (as in
IostreamOperatorOverloading.cpp) and test them. You can test them with
fstreams, stringstreams, and cin and
cout.
- Determine
the dummy constant value that your compiler passes for postfix operator++
and operator--.
- Write a
Number class that holds a double, and add overloaded operators for
+, –, *, /, and assignment. Choose the return values for these
functions so that expressions can be chained together, and for efficiency. Write
an automatic type conversion operator
intdouble( ). - Modify
Exercise 11 so that the return value optimization is used, if you have
not already done
so.
- Create a class
that contains a pointer, and demonstrate that if you allow the compiler to
synthesize the operator= the result of using that operator will be
pointers that are aliased to the same storage. Now fix the problem by defining
your own operator= and demonstrate that it corrects the aliasing. Make
sure you check for self-assignment and handle that case
properly.
- Write a
class called Bird that contains a string member and a static
int. In the default constructor, use the int to automatically
generate an identifier that you build in the string, along with the name
of the class (Bird #1, Bird #2, etc.). Add an operator<<
for ostreams to print out the Bird objects. Write an
assignment operator= and a copy-constructor. In main( ),
verify that everything works
correctly.
- Write a
class called BirdHouse that contains an object, a pointer and a reference
for class Bird from Exercise 14. The constructor should take the three
Birds as arguments. Add an operator<< for ostreams
for BirdHouse.
Write anDisallow the assignment
operator= and a copy-constructor. In
main( ), verify that everything works correctly. Make sure
that you can chain assignments for BirdHouse objects and build
expressions involving multiple
operators. - Add
an int data member to both Bird and BirdHouse in Exercise
15. Add member operators +, -, *, and / that use the
int members to perform the operations on the respective members. Verify
that these
work.
- Repeat
Exercise 16 using non-member
operators.
- Add an
operator-- to SmartPointer.cpp and
NestedSmartPointer.cpp.
- Modify
CopyingVsInitialization.cpp so that all of the constructors print a
message that tells you what’s going on. Now verify that the two forms of
calls to the copy-constructor (the assignment form and the parenthesized form)
are
equivalent.
- Attempt
to create a non-member operator= for a class and see what kind of
compiler message you
get.
- Create a class
with an
copy-constructor assignment operator
that has a second argument, a string that has a default value that
says “CC op= call.” Create a function that
takes assigns an object of your class by value
to another one and show that your
copy-constructorassignment operator is called
correctly. - In
CopyingWithPointers.cpp, remove the operator= in DogHouse
and show that the compiler-synthesized operator= correctly copies the
string but simply aliases the Dog
pointer.
- In
ReferenceCounting.cpp, add a static int and an ordinary int
as data members to both Dog and DogHouse. In all constructors
for both classes, increment the static int and assign the result to the
ordinary int to keep track of the number of objects that have been
created. Make the necessary modifications so that all the printing statements
will say the int identifiers of the objects
involved.
- Create a
class containing a string as a data member. Initialize the string
in the constructor, but do not create a copy-constructor or operator=.
Make a second class that has a member object of your first class; do not create
a copy-constructor or operator= for this class either. Demonstrate that
the copy-constructor and operator= are properly synthesized by the
compiler.
- Combine
the classes in OverloadingUnaryOperators.cpp and Integer.cpp.
- Modify
PointerToMemberOperator.cpp by adding two new member functions to
Dog that take no arguments and return void. Create and test an
overloaded operator->* that works with your two new
functions.
- Add an
operator->* to
NestedSmartPointer.cpp.
- Create
two classes, Apple and Orange. In Apple, create a
constructor that takes an Orange as an argument. Create a function that
takes an Apple and call that function with an Orange to show that
it works. Now make the Apple constructor explicit to demonstrate
that the automatic type conversion is thus prevented. Modify the call to your
function so that the conversion is made explicitly and thus
succeeds.
- Add a
global operator* to ReflexivityInOverloading.cpp and demonstrate
that it is
reflexive.
- Create
two classes and create an operator+ and the conversion functions such
that addition is reflexive for the two
classes.
- Fix
TypeConversionFanout.cpp by creating an explicit function to call to
perform the type conversion, instead of one of the automatic conversion
operators.
- Write
simple code that uses the +, -, *, and / operators
for doubles. Figure out how your compiler generates assembly code and
look at the assembly language that’s generated to discover and explain
what’s going on under the
hood.
[49]
Rob Murray, C++ Strategies & Tactics, Addison-Wesley, 1993, page
47.
|
|
|