Why WileyPLUS for Computer Science? W ileyPLUS for Computer
Science is a dynamic online environment that motivates students to
spend more time practicing the problems and explore the material
they need to master in order to succeed. I used the homework
problems to practice before quizzes and tests. I re-did the
problems on my own and then checked WileyPLUS to compare answers.
Student Alexandra Leifer, Ithaca College The integrated, online
text offers students a low-cost alternative to the printed text,
enhanced with direct links to specic portions of the text, plus
interactive animations and videos to help students visualize
concepts. Select WileyPLUS courses include LabRat, the programming
assignment tool allows you to choose from hundreds of programming
exercises to automate the process of assigning, completing,
compiling, testing, submitting, and evaluating programming
assignments. When students spend more time practicing, they come to
class better prepared. Try WileyPLUS with LabRat:
www.wileyplus.com/tours See and try WileyPLUS in action! Details
and Demo: www.wileyplus.com
WileyPLUS combines robust course management tools with the
complete online text and all of the interactive teaching &
learning resources you and your students need in one easy-to-use
system. Overall the WileyPLUS package made the material more
interesting than if it was just a book with no additional material
other than what you need to know. Edin Alic, North Dakota State
University Students easily access source code for example problems,
as well as self-study materials and all the exercises and readings
you assign. All instructional materials, including PowerPoints,
illustrations and visual tools, source code, exercises, solutions
and gradebook organized in one easy-to-use system. WileyPLUS made
it a lot easier to study. I got an A! Student Jeremiah Ellis
Mattson, North Dakota State University
Big C++ SECOND EDITION Cay Horstmann SAN JOSE STATE UNIVERSITY
Timothy A. Budd OREGON STATE UNIVERSITY John Wiley & Sons,
Inc.
EXECUTIVE PUBLISHER: ASSOCIATE PUBLISHER: SENIOR EDITORIAL
ASSISTANT: MEDIA EDITOR: SENIOR PRODUCTION EDITOR: SENIOR DESIGNER:
TEXT DESIGNER: COVER DESIGNER: COVER ILLUSTRATOR: PHOTO EDITOR:
PRODUCTION MANAGEMENT: Don Fowley Dan Sayre Carolyn Weisman Lauren
Sapira Ken Santor Madelyn Lesure Nancy Field Howard Grossman Susan
Cyr Lisa Gee Cindy Johnson This book was set in 10.5/12 Stempel
Garamond by Publishing Services and printed and bound by R.R.
Donnelley Crawfordsville. The cover was printed by R.R. Donnelley.
This book is printed on acid-free paper Copyright 2009, 2005 John
Wiley & Sons, Inc. All rights reserved. No part of this
publication may be reproduced, stored in a retrieval system or
transmitted in any form or by any means, electronic, mechanical,
photocopying, recording, scanning or otherwise, except as permitted
under Sections 107 or 108 of the 1976 United States Copyright Act,
without either the prior written permission of the Publisher, or
authorization through payment of the appropriate per-copy fee to
the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA
01923, (978) 750-8400, fax (978) 646-8600. Requests to the
Publisher for permission should be addressed to the Permissions
Department, John Wiley and Sons, Inc., 111 River Street, Hoboken,
NJ 07030-5774, (201) 748-6011, fax (201) 748-6008. To order books,
or for customer service, please call 1-800-CALL-Wiley (225-5945).
ISBN 978-0-470-38328-5 Printed in the United States of America 10 9
8 7 6 5 4 3 2 1
Preface T his book provides a traditional introduction to
computer science, focusing on program development and effective use
of the C++ programming language. It is suitable for motivated
beginners as well as students with prior programming experience.
The book can be used in a variety of settings, such as an
introductory computer science course, a two-semester course
sequence that covers data structures and objectoriented design, or
an advanced course on C++ and its applications. When writing the
book, we were guided by the following principles: Teach computer
science principles and not just C++. We use the C++ programming
language as a vehicle for introducing computer science concepts,
and in this Big book, we cover a large subset of the C++ language.
However, we do not aim to cover all esoteric aspects of C++. This
book focuses on the modern features of the C++ standard, such as
the string class and the STL containers. By minimizing the use of
error-prone and confusing constructs, your students will learn more
computer science and become more productive programmers. Use a
spiral approach to teach complex topics. We do not think it is wise
to overwhelm students when we introduce new topics. Generally, we
start with the essential facts that illustrate a concept, then
cover technical details at a later time. Here is an example. For
efciencys sake, large object parameters should be passed by
constant reference, not by value. But that consideration obscures
more fundamental issues. For that reason, we dont use constant
references when we rst introduce classes. In the next turn of the
spiral, students are comfortable with classes and reference
parameters, and a discussion of efciency is appropriate. We believe
this spiral approach is essential when teaching a language as
complex as C++. Introduce C++ as an object-oriented language.
Objects are introduced in two stages. From Chapter 2 on, students
learn to use objectsin particular, strings, streams, instances of
the simple Time and Employee classes, and graphical shapes.
Students become comfortable with creating objects and calling
member functions as the book continues along a traditional path,
discussing and providing practice with control structures and
functional decomposition. In Chapter 5,
vi Preface students learn how to implement classes and member
functions. From then on, objects and classes are used as the
natural building blocks of computer programs. Keep the order
exible. The book is highly modular. Do you prefer to cover arrays
before classes? Simply switch the chapters. Do you want to cover
streams and les earlier? This is not a problem. We supply a
graphics library, but it is entirely optional. We have found that
students enjoy programming exercises in which numbers and visual
information reinforce each other. But you may not want to spend
class time on it. Students can read the material on their own, or
you can skip it altogether. (See Figure 1 on page viii for the
chapter dependencies.) Offer choices for advanced and applied
topics. This Big book contains so much material that it would be
difcult to cover every chapter, even in an ambitious two-semester
course. The core material (Part A in Figure 1) contains what is
typically covered in a one-semester introductory course: control
structures, functions, arrays, classes, inheritance, and stream
I/O. The advanced material is grouped into four parts to make it
easy to choose a focus that ts your course. N e w in This Editio n
Streamlined Core Chapters This edition has been reorganized to make
it more suitable for a course that aims to cover advanced C++
features, data structures, or object-oriented design. The
introductory material has been condensed, and the material on
control structures has been consolidated into a single chapter.
Modular Design The advanced chapters are now grouped into four
distinct parts: Data structures and algorithms Advanced C++ and the
STL Object-oriented design Applications Part B covers an
introduction to data structures and algorithms, suitable for a
second-semester programming course. Part C focuses on advanced C++
and covers the STL in detail. Part D covers object-oriented design,
UML, and design patterns. Finally, Part E (which is available on
the Web) contains applied material on graphical user interface
programming, databases, and XML that you may nd useful as a
capstone. Except as shown in Figure 1, the parts are independent of
each other. The web-only chapters are part of the WileyPLUS eBook
and may also be downloaded from the books web site at
www.wiley.com/college/horstmann. If you are interested in adopting
a custom print edition that incorporates portions of the online
material, please contact your local Wiley representative.
Preface vii Contemporary C++ This book continues to focus on
the modern features of the C++ standard, such as the string class
and the STL containers. This edition prepares students for the
future with a new Chapter 21 on the features of the upcoming C++0x
standard (included in Part C). A large number of extended examples
and 15 case studies show how C++ features are used in complete and
useful programs. Optional Graphics Programming The graphics
programming coverage is provided because many students enjoy
writing programs that create drawings, and because graphical shapes
are splendid examples of objects. However, this edition makes it
easier to skip this focus if desired. Chapter 2 introduces the
simple graphics library provided with this book as another type of
object students may program with. Thereafter, exercises that make
use of the graphics library are clearly identied at chapter end. An
optional Chapter 25 covers graphical user interfaces and the
wxWidgets library. WileyPLUS The rst two pages of this book
describe an innovative online tool for teachers and students:
WileyPLUS. WileyPLUS can be adopted with the book, or as an
alternative to the printed text for about half the cost of print.
WileyPLUS integrates all of the instructor and student web
resources into an online version of this text. For more information
and a demo, please visit the web site listed on pages iii, or talk
to your Wiley representative. (To locate your local representative,
visit www.wiley.com/ college and click on Whos My Rep.) Student and
Instructor Resources in WileyPLUS This edition offers enhanced
electronic supplements. In particular, the test bank has been
completely revised to provide a signicant number of multiple-choice
questions that are suitable for self-check assignments and quizzes.
The following resources for students and instructors can be found
in the WileyPLUS course for this book. Web chapters and source code
are also available on the Web at www.wiley.com/college/horstmann.
Solutions to all exercises (for instructors only) A test bank (for
instructors only) A laboratory manual Lecture slides that summarize
each chapter and include code listings and gures Source code for
all examples in the book, the Employee and Time classes, and the
optional graphics library The programming style guide (Appendix A)
in modiable electronic form Help with common compilers, the
graphics library, and wxWidgets.
viii Preface A A: Fundamentals 1. Introduction B: Data
Structures & Algorithms C: Advanced Topics & STL D:
Object-Oriented Design 2. Numbers and Objects E: Applications (web
only) 3. Control Flow 4. Functions 6. Vectors and Arrays 9. Streams
5. Classes E 26. Relational Databases 7. Pointers 8. Inheritance B
C 10. Recursion 11. Sorting and Searching D 14. Operator
Overloading 12. Lists, Queues, & Stacks 22. ObjectOriented
Design 15. Memory Management 23. The Unified Modeling Language 13.
Sets, Maps, & Priority Queues 16. Templates 24. Intro to Design
Patterns 17. Exception Handling 18. Name Scope Management 20. The
Standard Template Library 21. The C++0x Standard Figure 1 Chapter
Dependencies 19. Class Hierarchies 25. Graphical User Interfaces
27. XML
Preface ix Pedagog ical Structure This edition builds on the
pedagogical elements in the last edition and offers additional aids
for the reader. Each chapter begins with the customary overview of
chapter objectives and motivational introduction. A listing of the
chapter contents then provides a quick reference to the special
features in the chapter. Throughout each chapter, margin notes show
where new concepts Margin notes mark and are introduced and provide
an outline of key ideas. These notes are reinforce new concepts
summarized at the end of the chapter as a chapter review. and are
summarized at The program listings are carefully designed for easy
reading. chapter end. Comments are typeset in a separate font that
is easier to read than the monospaced computer font. Functions are
set off by a subtle outline. Keywords, strings, and numbers are
color-coded consistently as they would be in a development
environment. (The code for all program listings in the book (plus
any additional les needed for each example) is available in the
WileyPLUS course for the book.) Special Features Throughout the
chapters, special features set off topics for added exibility and
easy reference. Syntax boxes highlight new syntactical constructs
and their purpose. An alphabetical list of these constructs can be
found on page xxii. Five additional features, entitled Common
Error, Productivity Hint, Quality Tip, Advanced Topic, and Random
Fact, are identied with the icons below and set off so they dont
interrupt the ow of the main material. Some of these are quite
short; others extend over a page. Each topic is given the space
that is needed for a full and convincing explanationinstead of
being forced into a oneparagraph tip. You can use the tables on
pages xxivxxxi to see the features in each chapter and the page
numbers where they can be found. Here is a list of the special
features and their icons. Common Errors describe the kinds of
errors that students often make, with an explanation of why the
errors occur, and what to do about them. Most students quickly
discover the Common Error sections and read them on their own.
Quality Tips explain good programming practices. Since most of them
require an initial investment of effort, these notes carefully
motivate the reason behind the advice and explain why the effort
will be repaid later. Productivity Hints teach students how to use
their tools more effectively, familiarizing them with tricks of the
trade such as keyboard shortcuts, global search and replace, or
automation of common tasks with scripts.
x Preface Advanced Topics cover nonessential or more difcult
material. Some of these topics introduce alternative syntactical
constructions that are not necessarily technically advanced. In
many cases, the book uses one particular language construct but
explains alternatives as Advanced Topics. Instructors and students
should feel free to use those constructs in their own programs if
they prefer them. It has, however, been our experience that many
students are grateful for the keep it simple approach, because it
greatly reduces the number of gratuitous decisions they have to
make. Random Facts provide historical and social information on
computing, as required to fulll the historical and social context
requirements of the ACM curriculum guidelines, as well as capsule
reviews of advanced computer science topics. Appendices Appendix A
contains a style guide for use with this book. We have found it
highly benecial to require a consistent style for all assignments.
We realize that our style may be different from yours. If you have
strong feelings about a particular issue, or if this style guide
conicts with local customs, feel free to modify it. The style guide
is available in electronic form for this purpose. Appendices B and
C contain summaries of the C++ keywords and operators. Appendix D
lists character escape sequences and ASCII character code values.
Appendix E documents all of the library functions and classes used
in this book. Appendices F and G contain a discussion of binary and
hexadecimal numbers, and the C++ bit and shift operations. Appendix
H contains a summary of the UML features that are used in this
book. Appendix I compares C++ and Java. This appendix should be
helpful to students with a prior background in Java. A c
knowledgments Many thanks to Dan Sayre, Lauren Sapira, Lisa Gee,
and Carolyn Weisman at John Wiley & Sons, and to the team at
Publishing Services for their hard work and support for this book
project. An especially deep acknowledgment and thanks to Cindy
Johnson, who, through enormous patience and attention to detail,
made this book a reality. Several individuals assisted in the
creation of the online resources for this edition. We would like to
thank Kurt Schmidt, Drexel University, and Diya Biswas, Maria
Kolakowska, and John OMeara for a great set of lecture slides. We
are grateful to Fred Annexstein, University of Cincinnati, Steven
Kollmansberger, South Puget Sound Community College, and Gwen
Walton, Florida Southern College, for their
Preface xi contributions to the solutions. And thank you to
John Russo, Wentworth Institute of Technology, for working with us
to prepare the labs that accompany the book. We are very grateful
to the many individuals who reviewed this and the prior edition of
the book, made many valuable suggestions, and brought an
embarrassingly large number of errors and omissions to our
attention. They include: Charles Allison, Utah Valley State College
Vladimir Akis, California State University, Los Angeles Richard
Borie, University of Alabama, Tuscaloosa Ramzi Bualuan, Notre Dame
University Drew Coles, Boston University Roger DeBry, Utah Valley
State College Joseph DeLibero, Arizona State University Martin S.
Dulberg, North Carolina State University Jeremy Frens, Calvin
College Timothy Henry, University of Rhode Island Robert Jarman,
Augusta State University Jerzy Jaromczyk, University of Kentucky
Debbie Kaneko, Old Dominion University Vitit Kantabutra, Idaho
State University Stan Lippman, Microsoft Corporation Brian Malloy,
Clemson University Stephen Murrell, University of Miami Jeffery
Popyack, Drexel University John Russo, Wentworth Institute of
Technology Kurt Schmidt, Drexel University William Shay, University
of Wisconsin, Green Bay Joseph R. Shinnerl, University of
California, Los Angeles Deborah Silver, Rutgers University John
Sterling, Polytechnic University Gwen Walton, Florida Southern
College Joel Weinstein, New England University Lillian Witzke,
Milwaukee School of Engineering Our gratitude also to those who
took time to tell us about their C++ course and whose advice shaped
this new edition: Fred Annexstein, University of Cincinnati Noah D.
Barnette, Virginia Tech Stefano Basagni, Northeastern
University
xii Preface Peter Breznay, University of Wisconsin, Green Bay
Subramaniam Dharmarajan, Arizona State University Stephen Gilbert,
Orange Coast College Barbara Guillott, Louisiana State University
Mir Behrad Khamesee, University of Waterloo Sung-Sik Kwon, North
Carolina Central University W. James MacLean, University of Toronto
Ethan V. Munson, University of Wisconsin, Milwaukee Kurt Schmidt,
Drexel University Michele A. Starkey, Mount Saint Mary College
William Stockwell, University of Central Oklahoma Jonathan
Tolstedt, North Dakota State University David P. Voorhees, Le Moyne
College Salih Yurttas, Texas A&M University
Contents Preface Special Features Chapter 1 v xxii Introduction
1.1 What Is a Computer? 1 2 3 1.2 What Is Programming? 1.3 The
Anatomy of a Computer 1.4 Translating Human-Readable Programs to
Machine Code 1.5 Programming Languages 4 10 11 1.6 The Evolution of
C++ 1.7 Becoming Familiar with Your Computer 1.8 Compiling a Simple
Program 1.9 Errors 1.10 The Compilation Process 2 12 15 19 21 24
1.11 Algorithms Chapter 9 Numbers and Objects 32 2.1 Number Types
2.2 Input 2.3 Assignment 2.4 Constants 51 2.5 Arithmetic 54 40 45
31
xiv Contents 62 2.6 Strings 2.7 Using Objects 2.8 Displaying
Graphical Shapes (Optional) Chapter 3 68 76 Control Flow 99 3.1 The
if Statement 3.2 Relational Operators 105 3.3 Multiple Alternatives
109 3.4 Nested Branches 3.5 Boolean Operations 3.6 The while Loop
3.7 The for Loop 3.8 The do Loop 3.9 Nested Loops 100 112 115 121
125 131 132 133 3.10 Processing Inputs 3.11 Simulations Chapter 4
139 Functions 159 4.1 Functions as Black Boxes 4.2 Implementing
Functions 4.3 Function Comments 4.4 Return Values 4.5 Parameters
171 4.6 Side Effects 175 4.7 Procedures 176 4.8 Reference
Parameters 4.9 Variable Scope and Global Variables 160 162 166 169
4.10 Stepwise Renement 178 4.11 Case Study: From Pseudocode to Code
4.12 Walkthroughs 4.13 Preconditions 4.14 Unit Testing 4.15 The
Debugger 195 200 203 205 183 186 188
xv Contents Chapter 5 Classes 227 5.1 Discovering Classes 5.2
Interfaces 5.3 Encapsulation 5.4 Member Functions 5.5 Default
Constructors 5.6 Constructors with Parameters 5.7 Accessing Data
Fields 5.8 Comparing Member Functions with Nonmember Functions 5.9
Separate Compilation Chapter 6 228 231 235 237 241 244 249 253
Vectors and Arrays 265 6.1 Using Vectors to Collect Data Items 6.2
Working with Vectors 6.3 Vector Parameters and Return Values 6.4
Removing and Inserting Vector Elements 6.5 Arrays Chapter 7 275 277
280 Pointers 305 Pointers and Memory Allocation 7.2 Deallocating
Dynamic Memory 7.3 Common Uses for Pointers 7.4 Arrays and Pointers
7.5 Pointers to Character Strings 7.6 Pointers to Functions 8 266
269 7.1 Chapter 250 306 311 314 322 327 330 Inheritance 341 8.1
Derived Classes 8.2 Calling the Base-Class Constructor 8.3
Overriding Member Functions 8.4 Polymorphism 342 356 350 349
xvi Contents Chapter 9 Streams 375 9.1 Reading and Writing Text
Files 9.2 The Inheritance Hierarchy of Stream Classes 9.3 Stream
Manipulators 9.4 String Streams 9.5 Command Line Arguments 9.6
Random Access 376 379 382 384 388 394 Chapter 10 10.1 Triangle
Numbers 10.2 Permutations 10.3 Thinking Recursively 10.4 Recursive
Helper Functions 10.5 Mutual Recursion 10.6 The Efciency of
Recursion Chapter 11 11.1 Selection Sort 11.2 Proling the Selection
Sort Algorithm 11.3 Analyzing the Performance of the Selection Sort
Algorithm 11.4 Merge Sort 11.5 Analyzing the Merge Sort Algorithm
11.6 Searching 11.7 Library Functions for Sorting and Binary Search
Chapter 12 12.1 Linked Lists 12.2 Implementing Linked LIsts 12.3
The Efciency of List and Vector Operations 12.4 Queues and Stacks
Recursion 411 412 416 421 424 425 430 Sorting and Searching 443 444
448 449 451 454 460 463 Lists, Queues, and Stacks 471 472 493 476
490
xvii Contents Chapter 13 13.1 Sets 13.2 Binary Search Trees
13.3 Tree Traversal 13.4 Maps 13.5 Priority Queues 13.6 Heaps
Chapter 14 14.1 Operator Overloading 14.2 Case Study: Fractional
Numbers 14.3 Overloading Simple Arithmetic Operators 14.4
Overloading Comparison Operators 14.5 Overloading Input and Output
14.6 Overloading Increment and Decrement Operators 14.7 Overloading
the Assignment Operators 14.8 Overloading Conversion Operators 14.9
Overloading the Subscript Operator 14.10 Overloading the Function
Call Operator 14.11 Case Study: Matrices Chapter 15 15.1 Categories
of Memory 15.2 Common Memory Errors 15.3 Constructors 15.4
Destructors 15.5 Reference Counting 15.6 Case Study: Matrices,
Continued Sets, Maps, and Priority Queues 505 506 509 516 521 526
529 Operator Overloading 545 546 551 558 560 562 564 568 569 572
574 577 Memory Management 589 590 594 602 613 622 627
xviii Contents Chapter 16 Templates 641 16.1 Template Functions
16.2 Compile-Time Polymorphism 642 647 649 16.3 Template Classes
16.4 Turning a Class into a Template 16.5 Nontype Template
Parameters 16.6 Setting Behavior Using Template Parameters 16.7
Case Study: Matrices, Continued Chapter 17 652 655 656 659
Exception Handling 665 17.1 Handling Exceptional Situations 17.2
Alternative Mechanisms for Handling Exceptions 666 17.3 Exceptions
17.4 Case Study: Matrices, Continued Chapter 18 668 674 689 Name
Scope Management 18.1 Encapsulation 698 18.2 Name Scopes 699 18.3
Protected Scope 18.4 Friends 18.5 Nested Classes 18.6 Private
Inheritance 18.7 Name Spaces 18.8 Case Study: Matrices, Continued
Chapter 19 19.1 Class Inheritance Hierarchies 19.2 Abstract Classes
19.3 Obtaining Run-Time Type Information 19.4 Multiple Inheritance
19.5 Software Frameworks 697 706 708 711 714 716 720 Class
Hierarchies 727 728 730 736 743 731
xix Contents Chapter 20 20.1 The STL 752 20.2 Iterators 753
20.3 The Fundamental Containers 20.4 Container Adapters 20.5
Associative Containers 20.6 Case Study: Dijkstras Shortest
Algorithm 20.7 Functions, Generators, and Predicates 20.8 Generic
Algorithms 20.9 Iterator Adapters 20.10 Case Study: File Merge Sort
Chapter 21 21.1 C++0x Design Objectives 21.2 Automatic Type
Inference 21.3 Range-based for Loop 21.4 New Constructor Features
21.5 Regular Expressions 21.6 Lambda Functions 21.7 Controlling
Default Implementations 21.8 Hash Tables 21.9 Concepts 21.10 Other
Minor Changes Chapter 22 22.1 The Software Life Cycle 22.2 CRC
Cards 22.3 Cohesion 833 22.4 Coupling 835 22.5 Relationships
Between Classes 22.6 Implementing Aggregations 22.7 Case Study:
Printing an Invoice 22.8 Case Study: An Educational Game The
Standard Template Library 751 758 765 767 771 775 781 791 792
Features of the C++0x Standard 805 806 807 808 810 813 814 815 817
817 820 Object-Oriented Design 826 831 837 838 839 851 825
xx Contents Chapter 23 23.1 The Unied Modeling Language 23.2
Use Cases 23.3 Sequence Diagrams 23.4 State Diagrams 23.5 Case
Study: A Voice Mail System Chapter 24 24.1 Iterators 24.2 The
Pattern Concept 914 24.3 The ADAPTER Pattern 919 24.4 The TEMPLATE
METHOD Pattern 24.5 Function Objects and the STRATEGY Pattern 24.6
The COMPOSITE Pattern 24.7 Case Study: Putting Patterns to Work
Chapter 25 25.1 The wxWidgets Toolkit 25.2 Frames 25.3 Adding a
Text Control to the Frame 25.4 Menus 25.5 Event Handling 25.6
Layout Management 25.7 Painting 25.8 Mouse Events 25.9 Dialog Boxes
25.10 Case Study: A GUI for the Clock Game Chapter 26 26.1
Organizing Database Information 26.2 Queries 26.3 Installing a
Database 26.4 Database Programming in C++ 26.5 Case Study:
Accessing an Invoice Database The Unied Modeling Language 875 876
879 881 883 884 An Introduction to Design Patterns 912 922 928 931
Graphical User Interfaces Relational Databases 925 911
xxi Contents Chapter 27 27.1 XML Tags and Documents 27.2
Parsing XML Documents 27.3 Creating XML Documents 27.4 Document
Type Denitions 27.5 Parsing with Document Type Denitions XM L
Appendices A B C D E F G H I C++ Language Coding Guidelines Keyword
Summary Operator Summary Character Codes C++ Library Summary Number
Systems Bit and Shift Operations U M L Summary A C++ / Java
Comparison 951 960 964 967 969 989 996 999 1004 Glossary 1011 Index
1026 Illustration Credits 1055
xxii Special Features Alphabetical List of Syntax Boxes Array
Initializer List Construction Array Variable Denition 282 Assertion
201 Assignment 48 Auto Initialization 808 Block Statement 812 102
Cast 50 Class Denition 234 Comment 35 Concept Denition 818 Constant
Denition 53 Constant Reference Parameter 181 Constructor Chaining
811 Constructor Denition 246 Constructor with Base-Class
Initializer 350 Constructor with Field Initializer List 248 Copy
Constructor 609 Default Constructor 607 Default/Deleted
Implementations delete Expression 312 Derived Class Denition 348
Destructor Denition 614 do Statement 132 Dynamic Cast 732 Exception
Specication for Statement 687 127 Friends 709 Function Call 57
Function Declaration (or Prototype) Function Denition 165 if
Statement Input Statement Lambda Function 816 102 42 815 Member
Function Call 64 Member Function Denition Multiple Inheritance 738
Name Space Alias 719 Name Space Denition 718 Nested Class Denition
713 new Expression 309 239 174
xxiii Special Features Object Construction 69 Object Variable
Denition 70 Output Statement 34 Overloaded Operator Denition 548
Overloaded Operator Member Function Denition Pointer Dereferencing
309 Pointer Variable Denition 309 Private Inheritance 716 Protected
Members 707 Pure Virtual Member Function 731 Range-based for Loop
809 Reference Parameter 180 return Statement 171 Simple Program 18
Template Class Concept Binding 819 Template Class Denition 651
Template Function Concept Binding 818 Template Function Denition
644 Template Member Function Denition 652 Throwing an Exception 675
try Block 676 Type Duplication 808 typedef Statement 332 typeid 733
Two-Dimensional Array Denition Variable Denition 35 Vector Variable
Denition Vector Subscript 269 Virtual Function Denition while
Statement 123 268 363 291 550
xxiv Table of Special Features Chapter 1 Quality Tips 19 21
Buffered Input 42 Failed Input 44 Roundoff Errors 49 Integer
Division Numbers and Objects Omitting Semicolons Misspelling Words
2 Introduction Common Errors 58 Unbalanced Parentheses 59
Forgetting Header Files 60 Trying to Call a Member Function Without
an Object 73 3 Confusing = and == Initialize Variables When You
Dene Them 36 Choose Descriptive Variable Names 37 Do Not Use Magic
Numbers 53 White Space 61 Factor Out Common Code 61 107 Brace
Layout 103 Comparison of FloatingPoint Numbers 108 Compile with
Zero Warnings 107 The Dangling else Problem 112 Multiple Relational
Operators Control Flow 128 117 Use for Loops for Their Intended
Purpose Only Confusing && and || Conditions 118 Dont Use !=
to Test the End of a Numeric Range 128 Innite Loops 123 Off-by-One
Errors 124 Symmetric and Asymmetric Bounds 130 Count Iterations 130
Forgetting a Semicolon 171 173 Mixing >> and getline Input
230 Forgetting a Semicolon 188 File Layout 252 Dont Combine Vector
Access and Index Increment 273 Make Parallel Vectors into Vectors
of Objects 279 Name the Array Size and Capacity Consistently 291
234 const Correctness 173 185 Keep Functions Short Classes Use
Meaningful Names for Parameters Minimize Global Variables 5
Functions Missing Return Value Type Mismatch 4 129 240 Forgetting
to Initialize All Fields in a Constructor 246 Trying to Reset an
Object by Calling a Constructor 6 Vectors and Arrays 247 Bounds
Errors 272 Omitting the Column Size of a Two-Dimensional Array
Parameter 292
xxv Table of Special Features Productivity Hints Backup Copies
Advanced Topics Random Facts 14 The ENIAC and the Dawn of Computing
12 49 Combining Assignment and Arithmetic 39 80 50 Enumerated Types
The Pentium Floating-Point Bug Computer Graphics Numeric Ranges and
Precisions 38 Casts 8 Standards Organizations 54 Remainder of
Negative Integers 61 Characters and C Strings 66 120 De Morgans Law
119 The Denver Airport Luggage Handling System 138 135 136 137
Function Declarations 136 Articial Intelligence 110 Clearing the
Failure State of a Stream 103 104 The switch Statement End-of-File
Detection Redirection of Input and Output The Selection Operator
The Loop and a Half Problem Tabs 173 Constant References 180 The
Explosive Growth of Personal Computers 181 168 The Therac-25
Incidents 202 Regular Expressions 168 The First Bug 205 Commenting
Out a Section of Code 198 Stubs 199 Programmer Productivity 243
Write Functions with Reuse in Mind 165 Global Search and Replace
Calling Constructors from Constructors Overloading Inspecting
Vectors in the Debugger 272 247 248 Strings Are Vectors of
Characters 273 Passing Vectors by Constant Reference 277
ProgrammingArt or Science? 257 An Early Internet Worm 274
International Alphabets 292
xxvi Table of Special Features Chapter 7 Pointers Common Errors
Confusing Pointers with the Data to Which They Point Quality Tips
310 Declaring Two Pointers on the Same Line 312 Memory Leaks 313
Confusing Array and Pointer Declarations 325 Returning a Pointer to
a Local Array 325 Failing to Allocate Memory 328 Confusing
Character Pointers and Arrays 329 Private Inheritance 348
Attempting to Access Private Base-Class Fields Inheritance 329
Copying Character Pointers 8 355 Forgetting the Base-Class Name 355
Slicing an Object 363 Innite Recursion 415 Tracing Through
Recursive Functions 9 324 310 Dangling Pointers Program Clearly,
Not Cleverly 419 Inconsistent Operations 566 Streams 10 Recursion
11 Sorting and Searching 12 Lists, Queues, and Stacks 13 Sets,
Maps, and Priority Queues 14 Operator Overloading Only One Level of
Conversion 571 Ambiguous Conversions 571 Avoid Dependencies on
Order of Evaluation 565 Conversion, Coercion, and Casts 570
xxvii Table of Special Features Productivity Hints Advanced
Topics Random Facts The this Pointer 310 Electronic Voting Machines
320 The Address Operator 313 Embedded Systems 332 References 319
Operating Systems 364 Encryption Algorithms 392 Databases and
Privacy 399 The Limits of Computation 434 457 The First Programmer
459 464 Cataloging Your Necktie Collection 464 Polish Notation 496
The First Algorithm 557 Using a Pointer to Step Through an Array
323 Dynamically Allocated Arrays 326 Protected Access 356 Virtual
Self-Calls 364 Binary Files 398 The Quicksort Algorithm Dening an
Ordering for Sorting Objects Dening an Ordering for Container
Elements 508 Constant Iterators Overload Operators Only to Make
Programs Easier to Read Dene Comparisons in Terms of Each Other
Dene One Operator in Terms of Another 568 559 Symmetry and
Conversion 560 528 Returning Local Objects 559 525 Discrete Event
Simulations 561 Peeking at the Input 563 The explicit Keyword 572
Other Operators 576 Inline Functions 576
xxviii Table of Special Features Chapter 15 Memory Management
Common Errors Quality Tips Forgetting the Dual Use of Single
Argument Constructors 605 Default Constructor and Parentheses
Confusing Destruction and Deletion 607 Avoid Buffer Overow Errors
597 When to Use the System-Dened Copy Constructor 609 Not Declaring
Destructors Virtual 618 If Destructor, Then Copy Constructor and
Assignment 619 617 Self Assignment 612 Include Virtual Destructors
615 Observing Constructors 626 Reference Counting Fails in the
Presence of Cycles 627 16 Exception Handling Invalid Type
Parameters 645 Templates Dont Preserve Inheritance 17 Templates
Move from Concrete to Abstract 654 Document Template Parameter
Requirements 655 Forgetting to Check Return Values 671 Nobody Cares
How Fast You Get the Wrong Answer 669 682 Tie Exception Classes to
the Standard Library 681 Throwing Objects versus Throwing Pointers
Exceptions During Construction of Global Variables 686 Use
Exceptions for Exceptional Cases 687 Throwing an Exception is Not a
Sign of Shame 18 Name Scope Management Confusing Scope and Lifetime
704 646 688 Dont Pollute the Global Scope 703 Use Accessor
Functions for Protected Access to Data 708 Friendship is Granted,
Not Taken 756 Assuming the Ending Iterator Is Included in a Range
756 Forgetting to Erase Removed Elements 790 745 Experimentally
Evaluate Execution Times 761 738 Mismatched Iterators 742 Design
Your Own Software Frameworks 734 Failing to Preserve the Is-a
Relationship The Standard Template Library 734 719 Avoid Multiple
Inheritance Taking Type of Pointer, Not Object Using Type Tests
Instead of Polymorphism 20 Class Hierarchies 713 Use Unambiguous
Names for Name Spaces 19 710 Manage Encapsulation
xxix Table of Special Features Productivity Hints Tracing
Execution Advanced Topics 616 Random Facts Constructors Are Always
Extensions 611 Overloading the Memory Management Operators 621
Templates and Overloading 648 Forms of Polymorphism 648 Nested
Templates 654 The Ariane Rocket Incident Overriding, Shadowing, and
Scopes Functional Programming 745 702 Forward References 688 705
Local Name Space Declaration 719 A Forest, Not a Tree 730 Virtual
Function Tables 735 Memory Allocation Traits 765
xxx Table of Special Features Chapter 21 Object-Oriented Design
23 The Unied Modeling Language 24 An Introduction to Design
Patterns Quality Tips Features of the C++0x Standard 22 Common
Errors Consistency Relational Databases 930 Graphical User
Interfaces 26 927 Pattern Recognition 25 Confusing Function Objects
and Classes 836 27 XML Joining Tables Without Specifying a Link
Condition 15 XML Describes Structure, Not Appearance 4 XML Elements
Describe Data Fields, Not Classes XML Is Stricter Than HTML 23 3
Prefer XML Elements over Attributes 8 Avoid Children with Mixed
Elements and Text 9 Stand on the Shoulders of Others 23
xxxi Table of Special Features Productivity Hints Advanced
Topics Random Facts Programming Languages Extreme Programming
Generic Programming with Inheritance and Templates Learning About a
New Toolkit 6 Becoming Familiar with a Complex Tool 4 Avoid
Unnecessary Data Replication 918 7 Dont Replicate Columns in a
Table 830 7 Stick with the Standard 820 9 Looking for Help on the
Internet 22 Writing an XML Document 30 Use DTDs with Your XML Files
43 30 Visual Programming 41 Transactions 33 Open Source and Free
Software 19 Let the Database Do the Work 24 19 Helper Functions in
an XML Parser Custom Dialog Boxes The XML Schema Specication 36
Other XML Technologies 43 Word Processing and Typesetting Systems
Grammars, Parsers, and Compilers 4 10
This page intentionally left blank
Chapter 1 Introduction CHAPTER GOALS To understand the activity
of programming To learn about the architecture of computers To
learn about machine languages and higher-level programming
languages To become familiar with your compiler To compile and run
your rst C++ program To recognize syntax and logic errors To
understand the notion of an algorithm This chapter contains a brief
introduction to the architecture of computers and an overview of
programming languages. You will learn about the activity of
programming: how to write and run your rst C++ program, how to
diagnose and x programming errors, and how to plan your programming
activities.
2 CHAPTER 1 Introduction C HAPTER C ONTENTS 1.1 What Is a
Computer? 2 1.2 What Is Programming? 1.7 Becoming Familiar with
Your Computer 12 3 4 RANDOM FACT 1.1: The ENIAC and the Dawn of
Computing 8 1.4 Translating Human-Readable Programs to Machine Code
9 1.5 Programming Languages 1.6 The Evolution of C++ 10 11 RANDOM
FACT 1.2: Standards Organizations 12 1.1 14 PRODUCTIVITY HINT 1.1:
Backup Copies 1.3 The Anatomy of a Computer 1.8 Compiling a Simple
Program 15 SYNTAX 1.1: Simple Program 18 COMMON ERROR 1.1: Omitting
Semicolons 19 1.9 Errors 19 COMMON ERROR 1.2: Misspelling Words 21
1.10 The Compilation Process 1.11 Algorithms 21 24 W h at Is a
Computer? You have probably used a computer for work or fun. Many
people use computers for everyday tasks such as balancing a
checkbook or writing a term paper. Computers are good for such
tasks. They can handle repetitive chores, such as totaling up
numbers or placing words on a page, without getting bored or
exhausted. More importantly, the computer presents the checkbook or
the term paper on the screen and lets you x mistakes easily.
Computers make good game machines because they can play sequences
of sounds and pictures, involving the human user in the process.
What makes all this possible is not only the computer. The
comDifferent tasks require puter must be programmed to perform
these tasks. One program different programs. balances checkbooks; a
different program, probably designed and constructed by a different
company, processes words; and a third program plays a game. The
computer itself is a machine that stores data (numbers, words,
pictures), interacts with devices (the monitor, the sound system,
the printer), and executes programs. Programs are sequences of
instructions and decisions that the computer carries out to achieve
a task. Todays computer programs are so sophisticated that it is
hard to believe that they are composed of extremely primitive
operations. A typical operation may be one of the following.
Computers execute very basic operations in rapid succession. Put a
red dot at this screen position. Get a number from this location in
memory.
1.2 What Is Programming? 3 Add up these two numbers. If this
value is negative, continue the program at a certain instruction.
The computer user has the illusion of smooth interaction because a
program contains a huge number of such operations, and because the
computer can execute them at great speed. The exibility of a
computer is quite an amazing phenomenon. The same machine can
balance your checkbook, print your term paper, and play a game. In
contrast, other machines carry out a much narrower range of tasks;
a car drives and a toaster toasts. Computers can carry out a wide
range of tasks because they execute different programs, each of
which directs the computer to work on a specic task. 1.2 What Is
Programm in g? A computer program tells a computer, in minute
detail, the sequence of steps that are needed to fulll a task. The
act of designing and implementing these programs is called computer
programming. In this book, you will learn how to program a
computerthat is, how to direct the computer to execute tasks. To
use a computer you do not need to do any programming. When you
write a term paper with a word processor, that program has been
programmed by the manufacturer and is ready for you to use. That is
only to be expectedyou can drive a car without being a mechanic and
toast bread without being an electrician. Most people who use
computers every day never need to do any programming. Since you are
reading this introductory computer science book, it may well be
your career goal to become a professional computer scientist or
software engineer. Programming is not the only skill required of a
computer scientist or software engineer; indeed, programming is not
the only skill required to create successful computer programs.
Nevertheless, the activity of programming is central to computer
science. It is also a fascinating and pleasurable activity that
continues to attract and motivate bright students. The discipline
of computer science is particularly fortunate that it can make such
an interesting activity the foundation of the learning path. To
write a computer game with motion and sound effects or a word
processor that supports fancy fonts and pictures is a complex task
that requires a team of many highly skilled programmers. Your rst
programming efforts will be more mundane. The concepts and skills
you learn in this book form an important foundation, and you should
not be disappointed if your rst programs do not rival the
sophisticated software that is familiar to you. Actually, you will
nd that there is an immense thrill even in simple programming
tasks. It is an amazing experience to see the computer carry out a
task precisely and quickly that would take you hours of drudgery,
to make small changes in a program that lead to immediate
improvements, and to see the computer become an extension of your
mental powers. Programmers produce computer programs to make the
computer solve new tasks.
4 CHAPTER 1.3 1 Introduction Th e Anatomy of a Computer To
understand the programming process, you need to have a rudimentary
understanding of the building blocks that make up a computer. We
will look at a personal computer. Larger computers have faster,
larger, or more powerful components, but they have fundamentally
the same design. At the heart of the computer lies the central
processing unit (CPU) The central processing (see Figure 1). It
consists of a single chip, or a small number of chips. unit (CPU)
executes one A computer chip (integrated circuit) is a component
with a plastic or operation at a time. metal housing, metal
connectors, and inside wiring made principally from silicon. For a
CPU chip, the inside wiring is enormously complicated. For example,
the Pentium chip (a popular CPU for personal computers at the time
of this writing) is composed of several million structural
elements, called transistors. Figure 2 shows a magnied detail view
of a CPU chip. The CPU Data values can be performs program control,
arithmetic, and data movement. That is, brought into the CPU for
the CPU locates and executes the program instructions; it carries
out processing from storage arithmetic operations such as addition,
subtraction, multiplication, or from input devices and division; it
fetches data from external memory or devices and such as the
keyboard, stores data back. All data must travel through the CPU
whenever it the mouse, or a is moved from one location to another.
(There are a few technical communications link. exceptions to this
rule; some devices can interact directly with memory.) The computer
stores data and programs in memory. There are two Storage devices
include kinds of memory. Primary storage is fast but expensive; it
is made random-access memory from memory chips: so-called
random-access memory (RAM) and (RAM) and secondary read-only memory
(ROM). Read-only memory contains certain prostorage. grams that
must always be presentfor example, the code needed to Figure 1
Central Processing Unit
1.3 The Anatomy of a Computer 5 DYNAMIC EXECUTION DYNAMIC
EXECUTION SYSTEM BUS EXECUTION TRACE CACHE TRANSFER CACHE FLOATING
POINT/ MULTIMEDIA RAPID EXECUTION ENGINE DYNAMIC EXECUTION HYPER
PIPELINE Figure 2 CPU Chip Detail start the computer. Random-access
memory might have been better called readwrite memory, because the
CPU can read data from it and write data back to it. That makes RAM
suitable to hold changing data and programs that do not have to be
available permanently. RAM memory has two disadvantages. It is
comparatively expensive, and it loses all its data when the power
is turned off. Secondary storage, usually a hard disk (see Figure
3), provides less expensive storage that persists without
electricity. A hard disk consists of rotating platters, which are
coated with a magnetic material, and read/write heads, which can
detect and change the magnetic ux on the platters. Programs and
data are typically stored on the hard disk and loaded into RAM when
the program starts. The program then updates the data in RAM and
writes the modied data back to the hard disk.
6 CHAPTER 1 Introduction Figure 3 A Hard Disk The central
processing unit, RAM memory, and the electronics controlling the
hard disk and other devices are interconnected through a set of
electrical lines called a bus. Data travels along the bus from the
system memory and peripheral devices to the CPU and back. Figure 4
shows a motherboard, which contains the CPU, the RAM, and card
slots, through which cards that control peripheral devices connect
to the bus. Figure 4 A Motherboard
1.3 The Anatomy of a Computer 7 To interact with a human user,
a computer requires peripheral devices. The computer transmits
information to the user through a display screen, speakers, and
printers. The user can enter information and directions to the
computer by using a keyboard or a pointing device such as a mouse.
Some computers are self-contained units, whereas others are
interconnected through networks. Through the network cabling, the
computer can read data and programs from central storage locations
or send data to other computers. For the user of a networked
computer it may not even be obvious which data reside on the
computer itself and which are transmitted through the network.
Figure 5 gives a schematic overview of the architecture of a
personal computer. Program instructions and data (such as text,
numbers, audio, or video) are stored on the hard disk, on a CD-ROM,
or elsewhere on the network. When a program is started, it is
brought into RAM memory, from where the CPU can read it. The CPU
reads the program one instruction at a time. As directed by these
instructions, the CPU reads data, modies it, and writes it back to
RAM memory or the hard disk. Some program instructions will cause
the CPU to place dots on the display screen or printer or to
vibrate the speaker. As these actions happen many times over and at
great speed, the human user will perceive images and sound. Some
program instructions read user input from the keyboard or mouse.
The program analyzes the nature of these inputs and then executes
the next appropriate instructions. Printer Hard disk Mouse Ports
Disk Controller CPU Graphics card Keyboard CD-ROM drive Monitor
Sound card RAM Speakers Network card Bus Figure 5 Schematic Design
of a Personal Computer Internet
8 CHAPTER 1 Introduction R A N D O M F A C T 1 .1 The ENIAC and
the Dawn of Computing The ENIAC (electronic numerical integrator
and computer) was the rst usable electronic computer. It was
designed by J. Presper Eckert and John Mauchly at the University of
Pennsylvania and was completed in 1946two years before transistors
were invented. The computer was housed in a large room and
consisted of many cabinets containing about 18,000 vacuum tubes
(see Figure 6). Vacuum tubes burned out at the rate of several
tubes per day. An attendant with a shopping cart full of tubes
constantly made the rounds and replaced defective ones. The
computer was programmed by connecting wires on panels. Each wiring
conguration would set up the computer for a particular problem. To
have the computer work on a different problem, the wires had to be
replugged. Work on the ENIAC was supported by the U.S. Navy, which
was interested in computations of ballistic tables that would give
the trajectory of a projectile, depending on the wind resistance,
initial velocity, and atmospheric conditions. To compute the
trajectories, one must nd the numerical solutions of certain
differential equations; hence the name numerical integrator. Before
machines like ENIAC were developed, humans did this kind of work,
and until the 1950s the word computer referred to these people. The
ENIAC was later used for peaceful purposes such as the tabulation
of U.S. Census data. Figure 6 The ENIAC
1.4 Translating Human-Readable Programs to Machine Code 1.4 9
Translating Hum a n-Rea da ble Prog ra m s to Machine Code On the
most basic level, computer instructions are extremely primitive.
The processor executes machine instructions. A typical sequence of
machine instructions is 1. Move the contents of memory location
40000 into register eax. (A register is a storage location in the
CPU.) 2. Subtract the value 100 from register eax. 3. If the result
is positive, continue with the instruction that is stored in memory
location 11280. Actually, machine instructions are encoded as
numbers so that they can be stored in memory. On a Pentium
processor, this sequence of instruction is encoded as the sequence
of numbers 161 40000 45 100 127 11280 On a processor from a
different manufacturer, the encoding would be quite different. When
this kind of processor fetches this sequence of numbers, it decodes
them and executes the associated sequence of commands. How can we
communicate the command sequence to the computer? The simplest
method is to place the actual numbers into the computer memory.
This is, in fact, how the very earliest computers worked. However,
a long program is composed of thousands of individual commands, and
it is a tedious and error-prone affair to look up the numeric codes
for all commands and place the codes manually into memory. As we
said before, computers are really good at automating tedious and
error-prone activities, and it did not take long for computer
programmers to realize that the computers themselves could be
harnessed to help in the programming process. The rst step was to
assign short names to the commands. For example, mov denotes move,
sub subtract, and jg jump if greater than 0. Using these commands,
the instruction sequence becomes Computer programs are stored as
machine instructions in a code that depends on the processor type.
mov 40000, %eax sub 100, %eax jg 11280 That is much easier to read
for humans. To get the instruction sequences accepted by the
computer, though, the names must be translated into the machine
codes. This is the task of another computer program: a so-called
assembler. It takes the sequence of characters mov %eax, translates
it into the command code 161, and carries out similar operations on
the other commands. Assemblers have another feature: they can give
names to memory locations as well as to instructions. Our program
sequence might be checking that some interest rate is greater than
100 percent, and the interest rate might be stored in memory
location 40000. It is usually not important where a value is
stored; any available memory location will do. By using symbolic
names instead of memory addresses, the program gets even easier to
read:
10 CHAPTER 1 Introduction mov int_rate, %eax sub 100, %eax jg
int_error It is the job of the assembler program to nd suitable
numeric values for the symbolic names and to put those values into
the generated code sequence. Assembler instructions were a major
advance over programming with raw machine codes, but they suffer
from two problems. It still takes a great many instructions to
achieve even the simplest goals, and the exact instruction sequence
differs from one processor to another. For example, the above
sequence of assembly instructions must be rewritten for the Sun
SPARC processor, which poses a real problem for people who invest a
lot of time and money producing a software package. If a computer
becomes obsolete, the program must be completely rewritten to run
on the replacement system. In the mid-1950s, higher-level
programming languages began to appear. In these languages, the
programmer expresses the idea behind the task that needs to be
performed, and a special computer program, a so-called compiler,
translates the higherlevel description into machine instructions
for a particular processor. For example, in C++, the high-level
programming language that we will use in this book, you might give
the following instruction: if (int_rate > 100) cout 100) and
translate that into 161 40000 45 100 127 11280 Compilers are quite
sophisticated programs. They have to translate logical statements,
such as the if, into sequences of computations, tests, and jumps,
and they must nd memory locations for variables like int_rate. In
this book, we will generally take the existence of a compiler for
granted. If you become a professional computer scientist, you may
well learn more about compiler-writing techniques later in your
studies. Higher-level languages are independent of the underlying
hardHigher-level languages ware. For example, the instruction if
(int_rate > 100) does not rely are independent of on particular
machine instructions. In fact, it will compile to differthe
processor. ent code on an Intel Pentium and a Sun SPARC processor.
1.5 P r ogramming Langua ge s Programming languages are designed by
computer scientists for a variety of purposes. Programming
languages are independent of specic computer architecture, but they
are human creations. As such, they follow certain conventions. To
ease the translation process, those conventions are much stricter
than they are for human languages. When you talk to another person,
and you scramble or omit a word or two, your conversation partner
will usually still understand what you have to say.
1.6 The Evolution of C++ 11 Compilers are less forgiving. For
example, if you omit the quotation mark close to the end of the
instruction, if (int_rate > 100) message_box("Interest rate
error); the C++ compiler will get quite confused and complain that
it cannot translate an instruction containing this error. This is
actually a good thing. If the compiler were to try to guess what
you did wrong and try to x it, it might not guess your intentions
correctly. In that case, the resulting program would do the wrong
thing quite possibly with disastrous effects, if that program
controlled a device on whose functions someones well-being
depended. When a compiler reads programming instructions in a
programming language, it will translate them into machine code only
if the input follows the language conventions exactly. Just as
there are many human languages, there are many programming
languages. Consider the instruction if (int_rate > 100) cout 100
then System.Console.Write("Interest rate error") end if Compilers
are language-specic. The C++ compiler will translate only C++ code,
whereas a Visual Basic compiler will reject anything but legal
Visual Basic code. For example, if a C++ compiler reads the
instruction if int_rate > 100 then ..., it will complain,
because the condition of the if statement isnt surrounded by
parentheses ( ), and the compiler doesnt expect the word then. The
choice of the layout for a language construct such as the if
statement is somewhat arbitrary. The designers of different
languages make different tradeoffs among readability, easy
translation, and consistency with other constructs. 1.6 The
Evolution of C++ C++ is built upon the C programming language,
which was developed to be translated efciently into fast machine
code, with a minimum of housekeeping overhead. C++ builds on C by
adding features for object-oriented programming, a programming
style that enables modeling of real-world objects. The initial
version of the C language was designed about 1972, but features
were added to it over the years. Because different compiler writers
added different features, the language actually sprouted various
dialects. Some programming instructions were understood by one
compiler but rejected by another. Such divergence is a major
obstacle to a programmer who wants to move code from one computer
to another. An effort got underway to iron out the differences and
come up with a standard version of C. The design process ended in
1989 with the completion of the ANSI (American National Standards
Institute) standard. In the meantime, Bjarne Stroustrup of AT&T
added features of the language Simula (an object-oriented
12 CHAPTER 1 Introduction language designed for carrying out
simulations) to C. The resulting language was called C++. From 1985
until today, C++ has grown by the addition of many features. A
standardization process culminated in the publication of the
international C++ standard in 1998. A minor update to the standard
was issued in 2003, and a major revision is expected to come to
fruition around 2009. At this time, C++ is the most commonly used
language for develC++ is a general-purpose oping system software
such as databases and operating systems. Just language that is in
as importantly, C++ is increasingly used for programming
embedwidespread use for ded systems, small computers that control
devices such as automosystems and embedded bile engines or cellular
telephones. programming. R A N D O M F A C T 1 .2 Standards
Organizations Two organizations, the American National Standards
Institute (ANSI) and the International Organization for
Standardization (ISO), have jointly developed the denitive standard
for the C++ language. Why have standards? You encounter the benets
of standardization every day. When you buy a light bulb, you can be
assured that it ts in the socket without having to measure the
socket at home and the bulb in the store. In fact, you may have
experienced how painful the lack of standards can be if you have
ever purchased a ashlight with nonstandard bulbs. Replacement bulbs
for such a ashlight can be difcult and expensive to obtain. The
ANSI and ISO standards organizations are associations of industry
professionals who develop standards for everything from car tires
and credit card shapes to programming languages. Having a standard
for a programming language such as C++ means that you can take a
program that you developed on one system with one manufacturers
compiler to a different system and be assured that it will continue
to work. To nd out more about standards organizations, check out
the following Web sites: www.ansi.org and www.iso.ch. 1.7 B e
coming Familiar w ith Yo ur Computer Set aside some time to become
familiar with the computer system and the C++ compiler that you
will use for your class work. As you use this book, you may well be
doing your work on an unfamiliar computer system. You should spend
some time making yourself familiar with the computer. Because
computer systems vary widely, this book can only give an outline of
the steps you need to follow. Using a new and unfamiliar computer
system can be frustrating. Look for training courses that your
campus offers, or just ask a friend to give you a brief tour. Step
1 Log In If you use your own home computer, you dont need to worry
about logging in. Computers in a lab, however, are usually not open
to everyone. Access is usually
1.7 Becoming Familiar with Your Computer 13 restricted to those
who paid the necessary fees and who can be trusted not to mess up
the conguration. You will likely need an account number and a
password to gain access to the system. Step 2 Locate the C++
Compiler Computer systems differ greatly in this regard. Some
systems let you start the compiler by selecting an icon or menu. On
other systems you must use the keyboard to type a command to launch
the compiler. On many personal computers there is a socalled
integrated environment in which you can write and test your
programs. On other computers you must rst launch one program that
functions like a word processor, in which you can enter your C++
instructions; then launch another program to translate them to
machine code; and then run the resulting machine code. Step 3
Understand Files and Folders As a programmer, you will write C++
programs, try them out, and improve them. You will be provided a
place on the computer to store them, and you need to nd out where
that place is. You will store your programs in les. A C++ le is a
container of C++ instructions. Files have names, and the rules for
legal names differ from one system to another. Some systems allow
spaces in le names; others dont. Some distinguish between upper-
and lowercase letters; others dont. Most C++ compilers require that
C++ les end in an extension .cpp, .cc, or .C; for example,
test.cpp. Files are stored in folders or directories. These le
containers can be nested. A folder can contain les as well as other
folders, which themselves can contain more les and folders (see
Figure 7). This hierarchy can be quite large, especially on net-
Figure 7 A Directory Hierarchy
14 CHAPTER 1 Introduction worked computers where some of the
les may be on your local disk, others elsewhere on the network.
While you need not be concerned with every branch of the hierarchy,
you should familiarize yourself with your local environment.
Different systems have different ways of showing les and
directories. Some use a graphical display and let you move around
by clicking the mouse on folder icons. In other systems, you must
enter commands to visit or inspect different locations. Step 4
Write a Simple Program In the next section, we will introduce a
very simple program. You will need to learn how to type it in, run
it, and x mistakes. Step 5 Save Your Work You will spend many hours
typing C++ programs in and improving them. The resulting program
les have some value, and you should treat them as you would other
important property. A conscientious safety strategy is particularly
important for computer les. They are more fragile than paper
documents or other more tangible objects. It is easy to delete a le
by accident, and occasionally les are lost because of a computer
malfunction. Unless you keep another copy, you must retype the
contents. Because you are unlikely to remember the entire le, you
will likely nd yourself spending almost as much time as you did to
enter and improve it in the rst place. This lost time may cause you
to miss deadlines. It is therefore crucially important that you
learn how to safeguard les and get in the habit of doing so before
disaster strikes. You can make safety or backup copies of les by
saving copies on a memory stick or on another computer. Develop a
strategy for keeping backup copies of your work before disaster
strikes. P R O D U C T I V I T Y H I N T 1 .1 Backup Copies Backing
up les on a memory stick is a an easy and convenient storage method
for many people. Another increasingly popular form of backup is
Internet le storage. Here are a few pointers to keep in mind. Back
up often. Backing up a le takes only a few seconds, and you will
hate yourself if you have to spend many hours recreating work that
you could have saved easily. I recommend that you back up your work
once every thirty minutes. Rotate backups. Use more than one
directory for backups, and rotate them. That is, rst back up onto
the rst directory. Then back up onto the second directory. Then use
the third, and then go back to the rst. That way you always have
three recent backups. If your recent changes made matters worse,
you can then go back to the older version. Back up source les only.
The compiler translates the les that you write into les consisting
of machine code. There is no need to back up the machine code les,
since you can recreate them easily by running the compiler again.
Focus your backup activity on those les that represent your effort.
That way your backup disks wont ll up with les that you dont
need.
1.8 Compiling a Simple Program 15 Pay attention to the backup
direction. Backing up involves copying les from one place to
another. It is important that you do this rightthat is, copy from
your work location to the backup location. If you do it the wrong
way, you will overwrite a newer le with an older version. Check
your backups once in a while. Double-check that your backups are
where you think they are. There is nothing more frustrating than to
nd out that the backups are not there when you need them. Relax,
then restore. When you lose a le and need to restore it from
backup, you are likely to be in an unhappy, nervous state. Take a
deep breath and think through the recovery process before you
start. It is not uncommon for an agitated computer user to wipe out
the last backup when trying to restore a damaged le. 1.8 Compiling
a Sim p le P ro gra m You are now ready to write and run your rst
C++ program. The traditional choice for the very rst program in a
new programming language is a program that displays a simple
greeting: Hello, World! We follow that tradition. Here is the
Hello, World! program in C++. ch01/hello.cpp 1 2 3 4 5 6 7 8 9
#include using namespace std; int main() { cout > pennies; cin =
pennies = buffer = .75n state = good 10 nickels = dimes = 3 After
cin >> nickels; cin = pennies = buffer = .75n state = fail
nickels = dimes = Figure 2 Processing Input 10 Unchanged because
the next input is not an integer
2.3 Assignment 4 45 After cin >> dimes; cin = pennies =
buffer = .75n state = fail nickels = dimes = Figure 2 10 Unchanged
because cin is in the failed state Processing Input, continued
input statements will be ignored. (See Step 4 in Figure 2.)
Unfortunately, there is no warning beep or error message that
alerts the user to this problem. You will learn in Chapter 3 how to
recognize and solve input problems. Of course, that is a necessary
skill for building programs that can survive untrained or careless
users. At this point we must just ask you to type in the right kind
of responses to the input prompts. 2.3 Assignment The contents of a
variable can vary over time. To change the value of a variable, you
use an assignment statement (see Syntax 2.5 on page 48). The left
hand of an assignment statement consists of a variable. The right
hand is an expression that has a value. That value is stored in the
variable, overwriting its previous contents. For example, consider
this statement: An assignment statement stores a new value in a
variable, replacing the previously stored value. total = pennies *
0.01; The value of the expression pennies * 0.01 is stored in
total, replacing the previous value of total. The following program
demonstrates the use of assignment statements. Let us compute the
value of the coins by keeping a running total. First, ask for the
number of pennies and set the total to the value of the pennies.
Then ask for the number of nickels and add the value of the nickels
to the total. Then do the same for the dimes and quarters. Here is
the program. ch02/coins3.cpp 1 2 3 4 5 6 #include using namespace
std; int main() {
46 CHAPTER 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 2 Numbers and Objects cout > count; double total =
count * 0.01; cout > count; total = count * 0.05 + total; cout
> count; total = count * 0.10 + total; cout > count; total =
count * 0.25 + total; cout s3; double average = (s1 + s2 + s3) / 3;
// Error cout first >> middle >> last; string initials
= first.substr(0, 1) + middle.substr(0, 1) + last.substr(0, 1);
cout > x; } while (x 0 Copy the input to x True Figure 10
Flowchart of a do Loop value 0? False
132 CHAPTER 3 Control Flow The do loop is the right choice for
this situation. We must rst read the input before we can decide
whether it passes the test. S Y N T A X 3 .5 do Statement do
statement while (condition); Example: do x = sqrt(x); while (x
>= 10); Purpose: Execute the statement, then test the condition,
and repeat the statement while the condition remains true. 3.9 N e
sted Loops In Section 3.4, you saw how to nest two if statements.
Similarly, complex iterations sometimes require nested loops: a
loop inside another loop statement. As an example, consider the
task of printing a triangle shape such as this one: [] [][] [][][]
[][][][] The rst row contains one box, the second row contains two
boxes, and so on. To print a triangle consisting of n rows, we use
a loop: for (int i = 1; i > y rst executes cin >> x, which
reads input into x and again yields cin, which is combined with y.
The operation cin >> y then reads y.) Because the expression
cin >> x has cin as its value, and you can use a stream as
the condition of an if statement, you can use the following test:
if (cin >> x) ... This means Read x, and if that didnt make
cin fail, then continue. That idiom is compelling for loops: cout
> values) { sum = sum + input; count++; } The loop is executed
while the input succeeds. A D V A N C E D T O P I C 3 .4 The
Loop-and-a-Half Problem Some programmers dislike loops that are
controlled by a Boolean variable, such as: bool more = true; while
(more) { cin >> input; if (cin.fail()) more = false; else {
process input } } The true test for loop termination is in the
middle of the loop, not at the top. This is called a loop and a
half because one must go halfway into the loop before knowing
whether one needs to terminate. As an alternative, you can use the
break keyword. while (true) { cin >> input; if (cin.fail())
break; process input } The break statement breaks out of the
enclosing loop, independent of the loop condition.
136 CHAPTER 3 Control Flow In the loop-and-a-half case, break
statements can be benecial. But in other situations, the break
statement can lead to code that is confusing or plainly wrong. We
do not use the break statement in this book. P R O D U C T I V I T
Y H I N T 3 .2 Redirection of Input and Output Use input
redirection to read input from a le. Use output redirection to
capture program output in a le. Consider the program that computes
the average value of an input sequence. If you use such a program,
then it is quite likely that you already have the values in a le,
and it seems a shame that you have to type them all in again. The
command line interface of your operating system provides a way to
link a le to the input of a program, as if all the characters in
the le had actually been typed by a user. If you type average <
numbers.txt the program is executed. Its input instructions no
longer expect input from the keyboard. All input commands get their
input from the le numbers.txt. This process is called input
redirection. Input redirection is an excellent tool for testing
programs. When you develop a program and x its bugs, it is boring
to keep entering the same input every time you run the program.
Spend a few minutes putting the inputs into a le, and use
redirection. You can also redirect output. In this program, that is
not terribly useful. If you run average < numbers.txt >
output.txt the le output.txt contains a single line such as
Average: 15. However, redirecting output is obviously useful for
programs that produce lots of output. You can print the le
containing the output or edit it before you turn it in for grading.
A D V A N C E D T O P I C 3 .5 End-of-File Detection When reading
input from a le, it is not common to use a sentinel value. Instead,
simply detect the end of the le. When the end of the le has been
reached, the next input will fail. Simply place all your data into
a le, use input redirection, and read the data with a loop while
(cin >> input) { process input } This mechanism works equally
well for reading numbers and strings. However, there is one trick
you need to know if you process strings and occasionally supply
input from the keyboard. When reading input from the console, you
indicate the end of input with a special
3.10 Processing Inputs 137 character, Ctrl + Z on a Windows
system or Ctrl + D on UNIX, after you have entered all values. That
value signals to cin that the end of the console le has been
reached. Sometimes, you may want to know whether input has failed
because the end of the le has been reached or because one of the
input values was bad. After determining that the stream has failed,
call the eof method: if (cin.fail()) { if (cin.eof()) { // End of
le has been reached } else { // Bad input } } Be careful that you
call eof only after the input stream has failed. The following loop
does not work: while (more) { cin >> value; if (cin.eof()) //
Dont! { more = false; } else { sum = sum + value; } } If the stream
input fails for another reason (usually because a non-number was
encountered in the input), then all further input operations fail,
and the end of the le is never reached. The loop then becomes an
innite loop. For example, consider the input 3 n 4 n f i v e cin
fails here, but end of file not yet encountered A D V A N C E D T O
P I C 3 .6 Clearing the Failure State of a Stream Once a stream
fails, all further input operations also fail. If you want to read
two number sequences and use a letter as a sentinel, you need to
clear the failure state after reading the rst sentinel. Call the
clear method:
138 CHAPTER 3 Control Flow cout > values) { process input }
cin.clear(); Suppose the user has entered 30 10 5 Q. The input of Q
has caused the failure. Because only successfully processed
characters are removed from the input, the Q character is still
present. Read it into a dummy variable: string sentinel; cin
>> sentinel; Now you can go on and read more inputs. R A N D
O M F A C T 3 .2 The Denver Airport Luggage Handling System Making
decisions is an essential part of any computer program. Nowhere can
this be seen more than with a computer system that helps sort
luggage at an airport. After scanning the luggage identication
codes, the system sorts the items and routes them to different
conveyor belts. Human operators then place the items onto trucks.
When the city of Denver Figure 11 The Denver airport originally had
a fully automatic system for moving luggage, replacing human
operators with robotic carts. Unfortunately, the system never
worked and was dismantled before the airport was opened.
3.11 Simulations 139 built a huge airport to replace an
outdated and congested facility, the luggage system contractor went
a step further. The new system was designed to replace the human
operators with robotic carts. Unfortunately, the system plainly did
not work. It was plagued by mechanical problems, such as luggage
falling onto the tracks and jamming carts. Just as frustrating were
the software glitches. Carts would uselessly accumulate at some
locations when they were needed elsewhere. The airport had been
scheduled for opening in 1993, but without a functioning luggage
system, the opening was delayed for over a year while the
contractor tried to x the problems. The contractor never succeeded,
and ultimately a manual system was installed. The delay cost the
city and airlines close to a billion dollars, and the contractor,
once the leading luggage systems vendor in the United States, went
bankrupt. Clearly, it is very risky to build a large system based
on a technology that has never been tried on a smaller scale. As
robots and the software that controls them will get better over
time, they will take on a larger share of luggage handling in the
future. But it is likely that this will happen in an incremental
fashion. 3.11 Simulations In a simulation we generate random events
and evaluate their outcomes. Here is a typical problem that can be
decided by running a simulation, the Buffon needle experiment,
devised by Comte Georges-Louis Leclerc de Buffon (17071788), a
French naturalist. A needle of length 1 inch is dropped onto paper
that is ruled with lines 2 inches apart. If the needle drops onto a
line, we count it as a hit. Buffon conjectured that the quotient
tries/hits approximates . (See Figure 12.) Now, how can you run
this experiment in the computer? You dont actually want to build a
robot that drops needles on paper. The C++ library has a random
number generator, which produces numbers that appear to be
completely random. Calling rand() yields a random integer between 0
and RAND_MAX (which is an implementation-dependent constant,
typically 32767 or 2147483647). The rand function is dened in the
cstdlib header. The following program calls the rand function ten
times. In a simulation program, you use the computer to simulate an
activity. You can introduce randomness by calling the random number
generator. Figure 12 The Buffon Needle Experiment
140 CHAPTER 3 Control Flow ch03/random.cpp 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 #include #include using namespace std; int main() {
int i; for (i = 1; i