Table Of ContentC++ By
EXAMPLE
C
++
By
Greg Perry
i
Contents '
C++ By Example
© 1992 by Que
All rights reserved. Printed in the United States of America. No part of this
book may be used or reproduced, in any form or by any means, or stored
in a database or retrieval system, without prior written permission of the
publisher except in the case of brief quotations embodied in critical articles
and reviews. Making copies of any part of this book for any purpose other
than your own personal use is a violation of United States copyright laws.
For information, address Que, 11711 N. College Ave., Carmel, IN 46032.
Library of Congress Catalog Card Number: 92-64353
ISBN: 1-56529-038-0
This book is sold as is, without warranty of any kind, either express or
implied, respecting the contents of this book, including but not limited to
implied warranties for the book’s quality, performance, merchantability,
or fitness for any particular purpose. Neither Que Corporation nor its
dealers or distributors shall be liable to the purchaser or any other person
or entity with respect to any liability, loss, or damage caused or alleged to
be caused directly or indirectly by this book.
96 95 94 93 92 8 7 6 5 4 3 2 1
Interpretation of the printing code: the rightmost double-digit number is
the year of the book’s printing; the rightmost single-digit number, the
number of the book’s printing. For example, a printing code of 92-1 shows
that the first printing of the book occurred in 1992.
ii
C++ By
EXAMPLE
Publisher Book Design
Lloyd Short Scott Cook
Michele Laseau
Publishing Manager
Joseph Wikert Production Analyst
Mary Beth Wakefield
Development Editor
Stacy Hiquet Cover Design
Jean Bisesi
Production Editor
Kezia Endsley Indexer
Johnna VanHoose
Copy Editor
Bryan Gambrel Production
Caroline Roop (Book Shepherd)
Technical Editor Jeff Baker, Paula Carroll,
Tim Moore Michelle Cleary, Brook Farling,
Kate Godfrey, Bob LaRoche,
Editorial Assistants Laurie Lee, Jay Lesandrini,
Rosemarie Graham Cindy L. Phipps, Linda Seifert,
Melissa Keegan Phil Worthington
Composed in Palatino and MCPdigital typefaces by Prentice Hall Computer Publishing.
Screen reproductions in this book were created by means of the program Collage Plus
from Inner Media, Inc., Hollis, NH.
iii
CDontenets 'dication
Dr. Rick Burgess, you shaped my life. Good or bad, I’m what I am
thanks to your help. I appreciate the many hours we’ve shared together.
G.M.P.
iv
C++ By
EXAMPLE
About the Author
Greg Perry has been a programmer and trainer for the past 14 years.
He received his first degree in computer science, then he received a
Masters degree in corporate finance. He currently is a professor of
computer science at Tulsa Junior College, as well as a computer
consultant and a lecturer. Greg Perry is the author of 11 other
computer books, including QBASIC By Example and C By Example. In
addition, he has published articles in several publications, including
PC World, Data Training, and Inside First Publisher. He has attended
computer conferences and trade shows in several countries, and is
fluent in nine computer languages.
v
Contents '
vi
C++ By
EXAMPLE
Acknowledgments
Much thanks to Stacy Hiquet and Joseph Wikert at Prentice Hall
(Que) for trusting me completely with the direction and style of this
book. The rest of my editors: Kezia Endsley, Bryan Gambrel, and the
Technical Editor, Tim Moore, kept me on track so the readers can
have an accurate and readable text.
The Tulsa Junior College administration continues to be sup-
portive of my writing. More importantly, Diane Moore, head of our
Business Services Division, Tony Hirad, and Elaine Harris, are
friends who make teaching a joy and not a job.
As always, my beautiful bride Jayne, and my parents Glen and
Bettye Perry, are my closest daily companions. It is for them I work.
Trademark Acknowledgments
Que Corporation has made every attempt to supply trademark
information about company names, products, and services men-
tioned in this book. Trademarks indicated below were derived from
various sources. Que Corporation cannot attest to the accuracy of
this information.
AT&T is a registered trademark of American Telephone &
Telegraph Company.
FORTRAN and COBOL are trademarks of International
Business Machines Corporation (IBM).
Turbo BASIC is a registered trademark of Borland
International, Inc.
Turbo C is a registered trademark of Borland International, Inc.
Microsoft QuickC and MS-DOS are registered trademarks of
Microsoft Corporation.
ANSI is a registered trademark of American National Standards
Institute.
vii
Contents '
viii
C++ By
EXAMPLE
Overview
I Introduction to C++
1 Welcome to C++ ...........................................................................11
2 What Is a Program?......................................................................35
3 Your First C++ Program..............................................................51
4 Variables and Literals ..................................................................69
5 Character Arrays and Strings .....................................................99
6 Preprocessor Directives.............................................................113
7 Simple Input/Output.................................................................133
II Using C++ Operators
8 Using C++ Math Operators and Precedence..........................163
9 Relational Operators ..................................................................185
10 Logical Operators .......................................................................207
11 Additional C++ Operators........................................................221
III C++ Constructs
12 The while Loop...........................................................................245
13 The for Loop................................................................................273
14 Other Loop Options ...................................................................295
15 The switch and goto Statements...............................................311
16 Writing C++ Functions..............................................................331
IV Variable Scope and Modular
Programming
17 Variable Scope.............................................................................353
18 Passing Values ............................................................................379
19 Function Return Values and Prototypes.................................397
20 Default Arguments and Function Overloading.....................415
V Character Input/Output and
String Functions
21 Device and Character Input/Output.......................................431
22 Character, String, and Numeric Functions.............................449
ix
Contents '
VI Arrays and Pointers
23 Introducing Arrays.....................................................................473
24 Array Processing.........................................................................493
25 Multidimensional Arrays..........................................................519
26 Pointers ........................................................................................541
27 Pointers and Arrays ...................................................................557
VII Structures and File Input/Output
28 Structures.....................................................................................583
29 Arrays of Structures ...................................................................605
30 Sequential Files ...........................................................................625
31 Random-Access Files .................................................................645
32 Introduction to Object-Oriented Programming.....................661
VIII References
A Memory Addressing, Binary, and Hexadecimal Review.....679
B Answers to Review Questions..................................................701
C ASCII Table..................................................................................719
D C++ Precedence Table................................................................729
E Keyword and Function Reference............................................733
F The Mailing List Application....................................................737
Glossary .......................................................................................747
Index.............................................................................................761
x