Sams Teach Yourself C++ in 21 Days

(singke) #1
In Review 217

Each of the other choices, with the exception of ChangeDimensions, calls a function.
This makes the switchstatement on lines 74–102 cleaner. ChangeDimensionscannot
call out to a function because it must change the dimensions of the rectangle. If the rec-
tangle were passed (by value) to a function such asDoChangeDimensions(), the dimen-
sions would be changed on the local copy of the rectangle in DoChangeDimensions()
and not on the rectangle in main(). On Day 8, “Understanding Pointers,” and Day 10,
“Working with Advanced Functions,” you’ll learn how to overcome this restriction, but
for now the change is made in the main()function.
Note how the use of an enumeration makes the switchstatement much cleaner and eas-
ier to understand. Had the switch depended on the numeric choices (1–5) of the user, you
would have to constantly refer to the description of the menu to see which pick was
which.
On line 68, the user’s choice is checked to be certain it is in range. If not, an error mes-
sage is printed and the menu is reprinted. Note that the switchstatement includes an
“impossible” default condition. This is an aid in debugging. If the program is working,
that statement can never be reached.
Congratulations! You’ve completed the first week! Now, you can create and understand
sophisticated C++ programs. Of course, there’s much more to do, and next week starts
with one of the most difficult concepts in C++: pointers. Don’t give up now, you’re about
to delve deeply into the meaning and use of object-oriented programming, virtual func-
tions, and many of the advanced features of this powerful language.
Take a break, bask in the glory of your accomplishment, and then turn the page to start
Week 2.

10 0672327112_w1_wir.qxd 11/19/04 12:26 PM Page 217

Free download pdf