1. Waite Group's Object-Oriented Programming in C++, Third
Edition (Publisher: Macmillan Computer Publishing) Author(s):
Robert Lafore ISBN: 157169160x Publication Date: 11/25/98 Bottom of
Form Previous Table of Contents Next Preface The major changes to
this Third Edition are concerned with Standard C++ and
object-oriented design. In addition, the book is no longer geared
exclusively to Borland C++ compilers. Standard C++, finalized in
the fall of 1997, introduced many new features to C++. Some of
these features, such as templates and exceptions, had already been
adopted by compiler manufacturers. However, the Standard Template
Library (STL) has only recently been included in compilers. This
book adds a chapter on the STL. Weve also introduced other features
from Standard C++, including new header files, the string class,
new-style casts, namespaces, and so on. The design of
object-oriented programs has received increasing emphasis in recent
years, so weve added a chapter on object-oriented design. The
advent of Standard C++ means that, at least to a greater extent
than before, all compilers should treat source code in the same
way. Accordingly, weve modified our emphasis on Borland compilers,
and now focus on code that should work with any Standard C++
compiler. Of course, the reality seldom matches the ideal, so so
the programs in this book have been tested with both Microsoft and
Borland compilers, and modified when necessary to work with both of
them. Previous Table of Contents Next Page 1 ABC Amber CHM
Converter Trial version,
http://www.processtext.com/abcchm.html
2. Waite Group's Object-Oriented Programming in C++, Third
Edition (Publisher: Macmillan Computer Publishing) Author(s):
Robert Lafore ISBN: 157169160x Publication Date: 11/25/98 Bottom of
Form Previous Table of Contents Next About the Author Robert Lafore
has been writing books about computer programming since 1982. His
best-selling titles include Assembly Language Programming for the
IBM PC, C Programming Using Turbo C++, C++ Interactive Course, and
Data Structures and Algorithms in Java. Mr. Lafore holds degrees in
mathematics and electrical engineering, and has been active in
programming since the days of the PDP-5, when 4K of main memory was
considered luxurious. His interests include hiking, windsurfing,
and recreational mathematics. Acknowledgments to the Third Edition
Id like to thank the entire team at Macmillan Computer Publishing.
In particular, Tracy Dunkelberger ably spearheaded the entire
project and exhibited great patience with what turned out to be a
lengthy schedule. Jeff Durham handled the myriad details involved
in interfacing between me and the editors with skill and good
humor. Andrei Kossorouko lent his expertise in C++ to ensure that I
didnt make this edition worse instead of better. Acknowledgments to
the Second Edition My thanks to the following professorsusers of
this book as a text at their respective colleges and
universitiesfor their help in planning the second edition: Dave
Bridges, Frank Cioch, Jack Davidson, Terrence Fries, Jimmie
Hattemer, Jack Van Luik, Kieran Mathieson, Bill McCarty, Anita
Millspaugh, Ian Moraes, Jorge Prendes, Steve Silva, and Edward
Wright. I would like to thank the many readers of the first edition
who wrote in with corrections and suggestions, many of which were
invaluable. At Waite Group Press, Joanne Miller has ably ridden
herd on my errant scheduling and filled in as academic liaison, and
Scott Calamar, as always, has made sure that everyone knew what
they were doing. Deirdre Greene provided an uncannily sharp eye as
copy editor. Thanks, too, to Mike Radtke and Harry Henderson for
their expert technical reviews. Special thanks to Edward Wright, of
Western Oregon State College, for reviewing and experimenting with
the new exercises. Acknowledgments to the First Edition My primary
thanks go to Mitch Waite, who poured over every inch of the
manuscript with painstaking attention to detail and made a
semi-infinite number of helpful suggestions. Bill McCarty of Azusa
Pacific University reviewed the content of the manuscript and its
suitability for classroom use, suggested many excellent
improvements, and attempted to correct my dyslexic spelling. Page 2
ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
3. George Leach ran all the programs, and, to our horror, found
several that didnt perform correctly in certain circumstances. I
trust these problems have all been fixed; if not, the fault is
entirely mine. Scott Calamar of The Waite Group dealt with the
myriad organizational aspects of writing and producing this book.
His competence and unfailing good humor were an important
ingredient in its completion. I would also like to thank Nan
Borreson of Borland for supplying the latest releases of the
software (among other useful tidbits), Harry Henderson for
reviewing the exercises, Louise Orlando of The Waite Group for ably
shepherding the book through production, Merrill Peterson of Matrix
Productions for coordinating the most trouble-free production run
Ive ever been involved with, Juan Vargas for the innovative design,
and Frances Hasegawa for her uncanny ability to decipher my
sketches and produce beautiful and effective art. Dedication This
book is dedicated to GGL another inodomitable spirit.222 Tell Us
What You Think! As the reader of this book, you are our most
important critic and commentator. We value your opinion and want to
know what were doing right, what we could do better, what areas
youd like to see us publish in, and any other words of wisdom youre
willing to pass our way. As the Executive Editor for the Advanced
Programming and Distributed Architectures team at Macmillan
Computer Publishing, I welcome your comments. You can fax, email,
or write me directly to let me know what you did or didnt like
about this bookas well as what we can do to make our books
stronger. Please note that I cannot help you with technical
problems related to the topic of this book, and that due to the
high volume of mail I receive, I might not be able to reply to
every message. When you write, please be sure to include this books
title and author as well as your name and phone or fax number. I
will carefully review your comments and share them with the author
and editors who worked on the book. Fax: 317-817-7070 Email:
[email protected] Mail: Tracy Dunkelberger Executive Editor
Advanced Programming and Distributed Architectures Macmillan
Computer Publishing 201 West 103rd Street Indianapolis, IN 46290
USA Previous Table of Contents Next Page 3 ABC Amber CHM Converter
Trial version, http://www.processtext.com/abcchm.html
4. Waite Group's Object-Oriented Programming in C++, Third
Edition (Publisher: Macmillan Computer Publishing) Author(s):
Robert Lafore ISBN: 157169160x Publication Date: 11/25/98 Bottom of
Form Previous Table of Contents Next APPENDIX A ASCII Chart Table
A.1 IBM Character Codes DEC HEX Symbol Key Use in C 0 00 (NULL)
Ctrl 2 1 01 A Ctr A 2 02 B Ctrl B 3 03 C Ctrl C 4 04 D Ctrl B 5 05
E Ctrl E 6 06 F Ctrl F 7 07 G Ctrl G Beep 8 08 H Backspace
Backspace 9 09 I Tab Tab 10 0A J Ctrl J Linefeed (new line) 11 0B K
Ctrl K Vertical Tab 12 0C L Ctrl L Form Feed 13 0D M Enter Carriage
Return 14 0E N Ctrl N 15 0F O Ctrl O 16 10 P Ctrl P 17 11 Q Ctrl Q
18 12 R Ctrl R 19 13 S Ctrl S 20 14 T Ctrl T 21 15 U Ctrl U 22 16 _
Ctrl V 23 17 W Ctrl W 24 18 X Ctrl X 25 19 Y Ctrl Y 26 1A Z Ctrl Z
27 1B a Escape Page 4 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
6. 68 44 D D 69 45 E E 70 46 F F 71 47 G G 72 48 H H 73 49 I I
74 4A J J 75 4B K K 76 4C L L 77 4D M M 78 4E N N 79 4F O O 80 50 P
P 81 51 Q Q 82 52 R R 83 53 S S 84 54 T T 85 55 U U 86 56 V V 87 57
W W 88 58 X X 89 59 Y Y 90 5A Z Z 91 5B [ [ 92 5C 93 5D ] ] 94 5E ^
^ 95 5F _ _ 96 60 ' ' 97 61 a a 98 62 b b 99 63 c c 100 64 d d 101
65 e e 102 66 f f 103 67 g g 104 68 h h 105 69 i i 106 6A j j 107
6B k k Page 6 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
7. 108 6C l l 109 6D m m 110 6E n n 111 6F o o 112 70 p p 113
71 q q 114 72 r r 115 73 s s 116 74 t t 117 75 u u 118 76 v v 119
77 w w 120 78 x x 121 79 y y 122 7A z z 123 7B { { 124 7C | | 125
7D } } 126 7E ~ ~ 127 7F f Ctrl 128 80 Alt 128 129 81 Alt 129 130
82 Alt 130 131 83 Alt 131 132 84 Alt 132 133 85 Alt 133 134 86 Alt
134 135 87 Alt 135 136 88 Alt 136 137 89 Alt 137 138 8A Alt 138 139
8B Alt 139 140 8C Alt 140 141 8D Alt 141 142 8E Alt 142 143 8F Alt
143 144 90 Alt 144 145 91 Alt 145 146 92 Alt 146 147 93 Alt 147
Page 7 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
8. 148 94 Alt 148 149 95 Alt 149 150 96 Alt 150 151 97 Alt 151
152 98 Alt 152 153 99 Alt 153 154 9A Alt 154 155 9B Alt 155 156 9C
Alt156 157 9D Alt157 158 9E Alt158 159 9F Alt159 160 A0 Alt160 161
A1 Alt161 162 A2 Alt162 163 A3 Alt163 164 A4 Alt164 165 A5 Alt165
166 A6 a Alt166 167 A7 o Alt167 168 A8 Alt168 169 A9 Alt169 170 AA
Alt170 171 AB ' Alt 171 172 AC .. Alt 172 173 AD Alt 173 174 AE Alt
174 175 AF Alt 175 176 B0 Alt 176 177 B1 Alt 177 178 B2 Alt 178 179
B3 Alt 179 180 B4 Alt 180 181 B5 Alt 181 182 B6 Alt 182 183 B7 Alt
183 184 B8 Alt 184 185 B9 Alt 185 186 BA Alt 186 187 BB a Alt 187
Page 8 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
9. 188 BC o Alt 188 189 BD Alt 189 190 BE Alt 190 191 BF Alt
191 192 C0 Alt 192 193 C1 Alt 193 194 C2 Alt 194 195 C3 Alt 195 196
C4 Alt 196 197 C5 Alt 197 198 C6 Alt 198 199 C7 Alt 199 200 C8 Alt
200 201 C9 ... Alt 201 202 CA g Alt 202 203 CB Alt 203 204 CC Alt
204 205 CD Alt 205 206 CE Alt 206 207 CF Alt 207 208 D0 Alt 208 209
D1 Alt 209 210 D2 " Alt 210 211 D3 " Alt 211 212 D4 Alt 212 213 D5
" Alt 213 214 D6 Alt 214 215 D7 Alt 215 216 D8 Alt 216 217 D9 Alt
217 218 DA / Alt 218 219 DB Alt 219 220 DC < Alt 220 221 DD >
Alt 221 222 DE fi Alt 222 223 DF fl Alt 223 224 E0 Alt 224 225 E1
Alt 225 226 E2 Alt 226 227 E3 Alt 227 Page 9 ABC Amber CHM
Converter Trial version,
http://www.processtext.com/abcchm.html
10. 228 E4 Alt 228 229 E5 Alt 229 230 E6 Alt 230 231 E7 Alt 231
232 E8 Alt 232 233 E9 Alt 233 234 EA Alt 234 235 EB Alt 235 236 EC
Alt 236 237 ED Alt 237 238 EE Alt 238 239 EF Alt 239 240 F0 Alt 240
241 F1 + Alt 241 242 F2 Alt 242 243 F3 Alt 243 244 F4 Alt 244 245
F5 1 Alt 245 246 F6 Alt 246 247 F7 Alt 247 248 F8 Alt 248 249 F9
Alt 249 250 FA . Alt 250 251 FB Alt 251 252 FC Alt 252 253 FD Alt
253 254 FE < Alt 254 255 FF (blank) Alt 255 Those key sequences
consisting of Ctrl are typed by pressing the CTRL key, and while it
is being held down, pressing the key indicated. These sequences are
based on those defined for PC Personal Computer series keyboards.
The key sequences may be defined differently on other keyboards.
IBM Extended ASCII characters can be displayed by pressing the Alt
key and then typing the decimal code of the character on the
keypad. Previous Table of Contents Next Page 10 ABC Amber CHM
Converter Trial version,
http://www.processtext.com/abcchm.html
11. Waite Group's Object-Oriented Programming in C++, Third
Edition (Publisher: Macmillan Computer Publishing) Author(s):
Robert Lafore ISBN: 157169160x Publication Date: 11/25/98 Bottom of
Form Previous Table of Contents Next APPENDIX B STANDARD C++
KEYWORDS Keywords implement specific C++ language features. They
cannot be used as names for variables or other user-defined program
elements. Many of the keywords are common to both C and C++, while
others are specific to C++. Some compilers may support additional
keywords, which usually begin with one or two underscores, as in
_cdecl or __int16. A asm auto B bool break C case catch char class
const const_cast continue D default delete do double dynamic_cast E
else enum explicit export extern F false Page 11 ABC Amber CHM
Converter Trial version,
http://www.processtext.com/abcchm.html
12. float for friend G goto I if inline int L long M main
mutable N namespace new O operator P private protected public R
register reinterpret_cast return S short signed sizeof static
static_cast struct switch T template this throw true try typedef
Page 12 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
13. typeid typename U union unsigned using V virtual void
volatile W wchar_t while Previous Table of Contents Next Page 13
ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
14. Waite Group's Object-Oriented Programming in C++, Third
Edition (Publisher: Macmillan Computer Publishing) Author(s):
Robert Lafore ISBN: 157169160x Publication Date: 11/25/98 Bottom of
Form Previous Table of Contents Next APPENDIX C MICROSOFT VISUAL
C++ This appendix tells you how to use Microsoft Visual C++ to
create console-mode applications, which are the kind of
applications used in this book. This discussion is based on Visual
C++ version 5.0. The present version of Visual C++ has good
(although not perfect) adherence to Standard C++. It comes in
various versions, including a student version for under $100. Well
assume that Visual C++ is installed in your system, and that you
know how to start it by using the Windows Start button and
navigating to the appropriate menu item: Microsoft Visual C++.
Youll want to make sure you can see file extensions (like .CPP)
when operating MVC++. In Windows Explorer, make sure that the
option Hide MS-DOS File Extensions for File Types That are
Registered is not checked. Screen Elements When you start Microsoft
Visual C++ youll see that the resulting application is actually
called Microsoft Developer Studio. The studio can work with other
languages besides C++, but we wont worry about that here. The
Developer Studio window is initially divided into three parts. On
the left is the View Pane. This has three tabs, for ClassView,
FileView, and InfoView. Once you have a project going, the
ClassView tab will show you the class hierarchy of your program,
and FileView will show you the files used in the project. InfoView
allows you to navigate through the documentation and help file
structure. Click the plus signs to expand the hierarchies, then
double-click the document you want to read. The largest part of the
screen usually holds a document window. It can be used for various
purposes, including displaying your source files. It can also
display the contents of help files. At the bottom of the screen is
a long window with more tabs: Build, Debug, and so on. This will
display messages when you perform operations such as compiling your
program. Single-File Programs Its easy to build and execute a
single-file console program using Microsoft Visual C++. There are
two possibilities: the file already exists or the file needs to be
written. In either case you should begin by making sure that no
project is currently open. (Well discuss projects in a moment.)
Click the File menu. If the Close Workspace item is active (not
grayed) click it to close the current workspace. Building an
Existing File Page 14 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
15. If the .CPP source file already exists, as it does for the
example programs in this book, select Open from the File menu.
(Note that this is not the same as Open Workspace.) Use the Open
dialog box to navigate to the appropriate file, select it, and
click the Open button. The file will appear in the document window.
(If youre compiling an example program that uses Console Graphics
Lite, such as the CIRCSTRC program in Chapter 5, Functions, or the
CIRCLES program in Chapter 6, Objects and Classes, turn to the
section Building Console Graphics Lite Programs.) To compile and
link this file, select Build from the Build menu. A dialog box will
appear asking if you want to create a Default Project Workspace.
Click Yes. The file will be compiled and linked with any necessary
library files. To run the program, select Execute from the Project
menu. If all goes well, a console window will appear with the
programs output displayed in it. When the program terminates, youll
see the phrase Press any key to continue. The compiler arranges for
this to be inserted following the termination of any program. It
keeps the console display on the screen long enough to see the
programs output. You can also run programs directly from MS-DOS. In
Windows 95 and 98, you can obtain a box for MS-DOS by clicking the
Start button, selecting Programs and then the MS-DOS Prompt item.
In the resulting window youll see whats called the C-prompt: the
letter C, usually followed by the name of the current directory.
You can navigate from one directory to another by typing cd (for
Change Directory) and the name of the new directory. To execute a
program, including any of the examples from this book, make sure
youre in the same directory as the appropriate .EXE file, and type
the name of the program (with no extension). You can find out more
about MS-DOS using the Windows help system. Writing a New File To
start writing your own .CPP file, select New from the File menu and
click the Files tab. Select C++ Source File, and click OK. A blank
document window will appear. Type in your program. Save the new
file by selecting Save As from the File menu, navigating to the
correct directory, and typing the file name with the .CPP extension
(such as myProg.cpp). As before, select Build from the Build menu
and click Yes to the default workspace question. Your program will
be compiled and linked. If there are errors, they will appear in
the Build window at the bottom of the screen. (You may need to
click the Build tab to make this window appear.) If you
double-click the error line, an arrow will appear next to the line
containing the error in the source file. Also, if you position the
cursor on the error number in the Build window (such as C2143) and
press the F1 key, an explanation of the error will appear in the
document window. You can correct the errors and repeat the build
process until the message reads 0 error(s), 0 warning(s). To
execute the program, select Execute from the Build menu. Before
working on a new program, dont forget to select Close Workspace
from the File menu. This ensures that you begin with a clean
workspace. To open a program youve already built, select Open
Workspace from File menu, navigate to the right directory, and
double-click the file with the appropriate name and the .DSW
extension. Run-Time Type Information (RTTI) A few programs, such as
EMPL_IO.CPP in Chapter 12, Streams and Files, use RTTI. With
Microsoft Visual C++ you need to enable a compiler option to make
this feature work. Select Settings from the Project menu and click
the C/C++ tab. From the Category list box, select C++ Language.
Click the checkbox named Enable Run-Time Type Information. This
will avoid various compiler and linker errors, some of which are
misleading. Page 15 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
16. Multifile Programs Weve shown the quick and dirty approach
to building programs. This approach works with one-file programs.
When projects have more than one file things become slightly more
complicated. Well start by reviewing whats meant by the terms
workspace and project. Projects and Workspaces Visual C++ uses a
concept called a workspace, which is one level of abstraction
higher than a project. A workspace can contain many projects. It
consists of a directory and several configuration files. Within it,
each project can have its own directory, or the files for all the
projects can simply reside in the workspace directory. Conceptually
its probably easiest, at least for the small programs in this book,
to assume that every project has its own separate workspace. Thats
what well assume in this discussion. A project corresponds to an
application (program) that youre developing. It consists of all the
files needed to create that application as well as information
about how these files are to be combined. The result of building a
project is usually a single .EXE file that a user can execute.
(Other results are possible, such as .DLL files.) Source Files
Already Exist Lets assume that the files you want to include in a
new project already exist, and that they are in a particular
directory. Select New from the File menu, and click the Projects
tab in the New dialog box. Select Win32 Console Application from
the list. First, in the Location box, type the path to the
directory, but do not include the directory name itself. Next, type
the name of the directory containing the files in the Project Name
box. (By clicking the button to the right of the Location field you
can navigate to the appropriate directory, but make sure to delete
the directory name itself from the location field.) Make sure the
Create New Workspace box is checked, and click OK. For example, if
the files are in C:BookCh13Elev, then you would first type
C:BookCh13 in the Location field and then Elev in the Project Name
field. When you type the project name, its automatically added to
the location. (If it was there already it would be added again,
resulting in a location of C:BookCh13ElevElev , which is not what
you want.) At this point various project-oriented files, with
extension .DSP, .DSW, and so forth, have been added to the
directory. Now you need to add your source files to the project.
This includes both .CPP and .H files. Select Add To Project from
the Project menu, click Files, select the files you want to add,
and click OK. You can review the files youve selected by clicking
the FileView tab and then the plus sign for the project. You can
also see the class structure, complete with member functions, by
clicking the ClassView tab. To open a file so you can see it and
modify it, select Open from the File menu and select the file.
Sometimes a file (such as the msoftCon.h file necessary for console
graphics programs) is not in the same directory as the other source
files for your program. You can nevertheless add it to your project
in the same way that you add other files. Select Add To Project
from the Project menu, select Files, and then navigate to the file
you want (or type in the complete pathname). Saving, Closing, and
Opening Projects Page 16 ABC Amber CHM Converter Trial version,
http://www.processtext.com/abcchm.html
17. To save the project, select Save Workspace. To close the
project, select Close Workspace. (Answer Yes to the query Close All
Document Windows.) To open an existing project, select Open
Workspace from the file menu, navigate to the proper directory,
select the .DSW file, and click Open. Compiling and Linking As with
one-file programs, the easiest way to compile, link, and run a
multifile program is to select Execute from the Build menu. You can
compile and link your project without running it by selecting Build
from the Build menu. Building Console Graphics Lite Programs
Building programs that use the Console Graphics Lite functions
(described in Appendix E, Console Graphics Lite) requires some
steps in addition to those needed for ordinary example programs.
Programs that use these functions should include the line #include
msoftcon.h. Open the source file for the program as described
earlier. Select Build from the Build menu. Answer Yes when asked if
you want to create a default project workspace. A project will be
created, but the compiler will complain it cant find MSOFTCON.H. To
tell it where to find this file, select Options from the Tools
menu. Click on the Directories tab. Select Include Files from the
Show Directories For list. On the bottom line of the Directories
list, type the complete pathname of the directory where MSOFTCON.H
is stored. (This directory should be called MSOFTCON.) Click on OK.
Now try building your file again. Now the compiler can find the
header file, but there will be numerous linker errors because the
linker doesnt know where to find the code for the graphics
functions. This code is in MSOFTCON.CPP. Select Add To Project from
the Project menu; then select Files. In the resulting dialog box
(called Insert Files into Project), navigate to the MSOFTCON
directory. Select the MSOFTCON.CPP file. Click OK. Now your program
should compile and link correctly. Select Execute from the Build
menu to see it run. Debugging In Chapter 3, Loops and Decisions, we
suggest using the debugger to provide an insight into how loops
work. Heres how to do that with Microsoft Visual C++. These same
steps can help you debug your program if it behaves incorrectly.
Well be discussing onefile programs here, but the same approach
applies, with appropriate variations, to larger multifile programs.
Start by building your program as you normally would. Fix any
compiler and linker errors. Make sure your program listing is
displayed in the Edit window. Single Stepping To start the
debugger, simply press the F10 key. Youll see a yellow arrow appear
in the margin of the listing, pointing to the opening brace
following main. If you want to start somewhere other than the
beginning of the program, position the cursor on the line where you
want to start debugging. Then, from the Build menu, select Start
Debug, and then Run to Cursor. The yellow arrow will appear next to
the statement selected. Now press the F10 key. This causes the
debugger to step to the next executable statement. The yellow arrow
will show where you are. Each press of F10 moves it to the next
statement. If youre in a loop, you Page 17 ABC Amber CHM Converter
Trial version, http://www.processtext.com/abcchm.html
18. ll see the yellow arrow move down through the statements in
the loop and then jump back to the top of the loop. Watching
Variables Youll see a Watch window in the bottom right corner of
your screen. To observe the values of variables change as you step
through the program, youll need to place these variables names in
this Watch window. To do this, rightclick a variable name in the
source code. A popup menu will appear. Select QuickWatch from this
menu. In the resulting QuickWatch dialog box, click Add Watch. The
variable and its current value will appear in the Watch window. If
a variable is out of scope, such as before its been defined, the
Watch window will show an error message instead of a value next to
the variable name. Stepping Into Functions If your program uses
functions, you can step into them (singlestep through the
statements within the function) by using the F11 key. By contrast,
the F10 key steps over function calls (treats them as a single
statement). If you use F11 to trace into library routines like
cout