+ All Categories

Bcpp

Date post: 08-May-2015
Category:
Upload: fjollajokull
View: 1,095 times
Download: 2 times
Share this document with a friend
1090
Transcript
  • 1.Why WileyPLUS for Computer Science? WileyPLUS 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 CollegeThe 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

2. 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 UniversityStudents 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 3. Big C++ SECOND EDITIONCay Horstmann SAN JOSE STATE UNIVERSITYTimothy A. Budd OREGON STATE UNIVERSITYJohn Wiley & Sons, Inc. 4. 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 JohnsonThis 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 5. Preface This 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, 6. viPrefacestudents 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 ApplicationsPart 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. 7. PrefaceviiContemporary 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. 8. viiiPrefaceA A: Fundamentals1. IntroductionB: Data Structures & Algorithms C: Advanced Topics & STL D: Object-Oriented Design2. Numbers and ObjectsE: Applications (web only)3. Control Flow4. Functions6. Vectors and Arrays9. Streams5. ClassesE 26. Relational Databases7. Pointers8. Inheritance BC 10. Recursion11. Sorting and SearchingD 14. Operator Overloading12. Lists, Queues, & Stacks22. ObjectOriented Design15. Memory Management23. The Unified Modeling Language13. Sets, Maps, & Priority Queues16. Templates24. Intro to Design Patterns17. Exception Handling18. Name Scope Management20. The Standard Template Library21. The C++0x StandardFigure 1Chapter Dependencies19. Class Hierarchies25. Graphical User Interfaces27. XML 9. PrefaceixPedagog 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. 10. xPreface 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 11. Prefacexicontributions 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 12. xiiPrefacePeter 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 13. ContentsPreface Special Features Chapter1v xxiiIntroduction1.1What Is a Computer?1 2 31.2What Is Programming?1.3The Anatomy of a Computer1.4Translating Human-Readable Programs to Machine Code1.5Programming Languages4 10111.6The Evolution of C++1.7Becoming Familiar with Your Computer1.8Compiling a Simple Program1.9Errors1.10 The Compilation Process2121519 21241.11 AlgorithmsChapter9Numbers and Objects 322.1Number Types2.2Input2.3Assignment2.4Constants512.5Arithmetic5440 4531 14. xivContents622.6Strings2.7Using Objects2.8Displaying Graphical Shapes (Optional)Chapter368 76Control Flow993.1The if Statement3.2Relational Operators1053.3Multiple Alternatives1093.4Nested Branches3.5Boolean Operations3.6The while Loop3.7The for Loop3.8The do Loop3.9Nested Loops100112 115 121 125131 132 1333.10 Processing Inputs 3.11 SimulationsChapter4139Functions1594.1Functions as Black Boxes4.2Implementing Functions4.3Function Comments4.4Return Values4.5Parameters1714.6Side Effects1754.7Procedures1764.8Reference Parameters4.9Variable Scope and Global Variables160 1621661694.10 Stepwise Renement1784.11 Case Study: From Pseudocode to Code 4.12 Walkthroughs 4.13 Preconditions 4.14 Unit Testing 4.15 The Debugger195 200 203 205183186 188 15. xvContentsChapter5Classes2275.1Discovering Classes5.2Interfaces5.3Encapsulation5.4Member Functions5.5Default Constructors5.6Constructors with Parameters5.7Accessing Data Fields5.8Comparing Member Functions with Nonmember Functions5.9Separate CompilationChapter6228231 235 237 241 244249253Vectors and Arrays2656.1Using Vectors to Collect Data Items6.2Working with Vectors6.3Vector Parameters and Return Values6.4Removing and Inserting Vector Elements6.5ArraysChapter7275 277280Pointers305Pointers and Memory Allocation7.2Deallocating Dynamic Memory7.3Common Uses for Pointers7.4Arrays and Pointers7.5Pointers to Character Strings7.6Pointers to Functions82662697.1Chapter250306 311314322 327330Inheritance3418.1Derived Classes8.2Calling the Base-Class Constructor8.3Overriding Member Functions8.4Polymorphism342356350349 16. xviContentsChapter9Streams3759.1Reading and Writing Text Files9.2The Inheritance Hierarchy of Stream Classes9.3Stream Manipulators9.4String Streams9.5Command Line Arguments9.6Random Access376 379382384 388394Chapter1010.1Triangle Numbers10.2Permutations10.3Thinking Recursively10.4Recursive Helper Functions10.5Mutual Recursion10.6The Efciency of RecursionChapter1111.1Selection Sort11.2Proling the Selection Sort Algorithm11.3Analyzing the Performance of the Selection Sort Algorithm11.4Merge Sort11.5Analyzing the Merge Sort Algorithm11.6Searching11.7Library Functions for Sorting and Binary SearchChapter1212.1Linked Lists12.2Implementing Linked LIsts12.3The Efciency of List and Vector Operations12.4Queues and StacksRecursion411 412416 421 424425 430Sorting and Searching443444 448 449451 454460 463Lists, Queues, and Stacks471472493476 490 17. xviiContentsChapter1313.1Sets13.2Binary Search Trees13.3Tree Traversal13.4Maps13.5Priority Queues13.6HeapsChapter1414.1Operator Overloading14.2Case Study: Fractional Numbers14.3Overloading Simple Arithmetic Operators14.4Overloading Comparison Operators14.5Overloading Input and Output14.6Overloading Increment and Decrement Operators14.7Overloading the Assignment Operators14.8Overloading Conversion Operators14.9Overloading the Subscript Operator14.10Overloading the Function Call Operator14.11Case Study: MatricesChapter1515.1Categories of Memory15.2Common Memory Errors15.3Constructors15.4Destructors15.5Reference Counting15.6Case Study: Matrices, ContinuedSets, Maps, and Priority Queues505506 509516521 526529Operator Overloading545546 551 558560562 564568569 572 574577Memory Management589590 594602 613 622 627 18. xviiiContentsChapter16Templates64116.1Template Functions16.2Compile-Time Polymorphism642 64764916.3Template Classes16.4Turning a Class into a Template16.5Nontype Template Parameters16.6Setting Behavior Using Template Parameters16.7Case Study: Matrices, ContinuedChapter17652 655 656659Exception Handling66517.1Handling Exceptional Situations17.2Alternative Mechanisms for Handling Exceptions66617.3Exceptions17.4Case Study: Matrices, ContinuedChapter18668674 689Name Scope Management18.1Encapsulation69818.2Name Scopes69918.3Protected Scope18.4Friends18.5Nested Classes18.6Private Inheritance18.7Name Spaces18.8Case Study: Matrices, ContinuedChapter1919.1Class Inheritance Hierarchies19.2Abstract Classes19.3Obtaining Run-Time Type Information19.4Multiple Inheritance19.5Software Frameworks697706708 711 714716 720Class Hierarchies727 728730 736 743731 19. xixContentsChapter2020.1The STL75220.2Iterators75320.3The Fundamental Containers20.4Container Adapters20.5Associative Containers20.6Case Study: Dijkstras Shortest Algorithm20.7Functions, Generators, and Predicates20.8Generic Algorithms20.9Iterator Adapters20.10Case Study: File Merge SortChapter2121.1C++0x Design Objectives21.2Automatic Type Inference21.3Range-based for Loop21.4New Constructor Features21.5Regular Expressions21.6Lambda Functions21.7Controlling Default Implementations21.8Hash Tables21.9Concepts21.10Other Minor ChangesChapter2222.1The Software Life Cycle22.2CRC Cards22.3Cohesion83322.4Coupling83522.5Relationships Between Classes22.6Implementing Aggregations22.7Case Study: Printing an Invoice22.8Case Study: An Educational GameThe Standard Template Library751758765 767 771 775781 791 792Features of the C++0x Standard805806 807 808 810813 814 815817 817 820Object-Oriented Design 826831837 838 839 851825 20. xxContentsChapter2323.1The Unied Modeling Language23.2Use Cases23.3Sequence Diagrams23.4State Diagrams23.5Case Study: A Voice Mail SystemChapter2424.1Iterators24.2The Pattern Concept91424.3The ADAPTER Pattern91924.4The TEMPLATE METHOD Pattern24.5Function Objects and the STRATEGY Pattern24.6The COMPOSITE Pattern24.7Case Study: Putting Patterns to WorkChapter2525.1The wxWidgets Toolkit25.2Frames25.3Adding a Text Control to the Frame25.4Menus25.5Event Handling25.6Layout Management25.7Painting25.8Mouse Events25.9Dialog Boxes25.10Case Study: A GUI for the Clock GameChapter2626.1Organizing Database Information26.2Queries26.3Installing a Database26.4Database Programming in C++26.5Case Study: Accessing an Invoice DatabaseThe Unied Modeling Language875876879 881883 884An Introduction to Design Patterns 912922928 931Graphical User InterfacesRelational Databases925911 21. xxiContentsChapter2727.1XML Tags and Documents27.2Parsing XML Documents27.3Creating XML Documents27.4Document Type Denitions27.5Parsing with Document Type DenitionsXM LAppendices A B C D E F G H IC++ 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 Comparison951 960 964 967 969 989 996 999 1004Glossary1011Index1026Illustration Credits1055 22. xxiiSpecial FeaturesAlphabetical List of Syntax Boxes Array Initializer List Construction Array Variable Denition 282 Assertion 201 Assignment 48 Auto Initialization 808 Block Statement812102Cast 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 Statement687127Friends 709 Function Call 57 Function Declaration (or Prototype) Function Denition 165 if StatementInput Statement Lambda Function816102 42 815Member Function Call 64 Member Function Denition Multiple Inheritance 738 Name Space Alias 719 Name Space Denition 718 Nested Class Denition 713 new Expression 309239174 23. xxiiiSpecial FeaturesObject 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 Program18Template 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 typeid733Two-Dimensional Array Denition Variable Denition 35 Vector Variable Denition Vector Subscript 269 Virtual Function Denition while Statement123268 363291550 24. xxivTable of Special FeaturesChapter1Quality Tips19 21Buffered Input42Failed Input44Roundoff Errors49Integer DivisionNumbers and ObjectsOmitting Semicolons Misspelling Words2IntroductionCommon Errors58Unbalanced Parentheses59Forgetting Header Files60Trying to Call a Member Function Without an Object 733Confusing = and ==Initialize Variables When You Dene Them36Choose Descriptive Variable Names37Do Not Use Magic Numbers53White Space61Factor Out Common Code61107Brace Layout103Comparison of FloatingPoint Numbers108Compile with Zero Warnings107The Dangling else Problem112Multiple Relational OperatorsControl Flow128117Use for Loops for Their Intended Purpose OnlyConfusing && and || Conditions118Dont Use != to Test the End of a Numeric Range128Innite Loops123Off-by-One Errors124Symmetric and Asymmetric Bounds130Count Iterations130Forgetting a Semicolon171 173Mixing >> and getline Input230Forgetting a Semicolon188File Layout252Dont Combine Vector Access and Index Increment273Make Parallel Vectors into Vectors of Objects279Name the Array Size and Capacity Consistently291234const Correctness173 185Keep Functions ShortClassesUse Meaningful Names for Parameters Minimize Global Variables5FunctionsMissing Return Value Type Mismatch4129240Forgetting to Initialize All Fields in a Constructor 246 Trying to Reset an Object by Calling a Constructor6Vectors and Arrays247Bounds Errors272Omitting the Column Size of a Two-Dimensional Array Parameter292 25. xxvTable of Special FeaturesProductivity HintsBackup CopiesAdvanced TopicsRandom Facts14The ENIAC and the Dawn of Computing1249Combining Assignment and Arithmetic39 8050Enumerated TypesThe Pentium Floating-Point Bug Computer GraphicsNumeric Ranges and Precisions 38 Casts8Standards Organizations54Remainder of Negative Integers 61 Characters and C Strings66120De Morgans Law119The Denver Airport Luggage Handling System138135 136 137Function Declarations136Articial Intelligence110Clearing the Failure State of a Stream103104The switch StatementEnd-of-File DetectionRedirection of Input and OutputThe Selection OperatorThe Loop and a Half ProblemTabs173Constant References180The Explosive Growth of Personal Computers181168The Therac-25 Incidents202Regular Expressions168The First Bug205Commenting Out a Section of Code198Stubs199 Programmer Productivity243Write Functions with Reuse in Mind165Global Search and ReplaceCalling Constructors from Constructors OverloadingInspecting Vectors in the Debugger272247 248Strings Are Vectors of Characters273Passing Vectors by Constant Reference277ProgrammingArt or Science? 257An Early Internet Worm274International Alphabets292 26. xxviTable of Special FeaturesChapter7PointersCommon ErrorsConfusing Pointers with the Data to Which They PointQuality Tips310Declaring Two Pointers on the Same Line312Memory Leaks313Confusing Array and Pointer Declarations325Returning a Pointer to a Local Array325Failing to Allocate Memory328Confusing Character Pointers and Arrays329Private Inheritance348Attempting to Access Private Base-Class FieldsInheritance329Copying Character Pointers8355Forgetting the Base-Class Name355Slicing an Object363Innite Recursion415Tracing Through Recursive Functions9324310Dangling PointersProgram Clearly, Not Cleverly419Inconsistent Operations566Streams10Recursion11Sorting and Searching12Lists, Queues, and Stacks13Sets, Maps, and Priority Queues14Operator OverloadingOnly One Level of Conversion 571 Ambiguous Conversions571Avoid Dependencies on Order of Evaluation 565 Conversion, Coercion, and Casts570 27. xxviiTable of Special FeaturesProductivity HintsAdvanced TopicsRandom FactsThe this Pointer310Electronic Voting Machines320The Address Operator313Embedded Systems332References319Operating Systems364Encryption Algorithms392Databases and Privacy399The Limits of Computation434457The First Programmer459464Cataloging Your Necktie Collection464Polish Notation496The First Algorithm557Using a Pointer to Step Through an Array323Dynamically Allocated Arrays326Protected Access356Virtual Self-Calls364Binary Files398The Quicksort Algorithm Dening an Ordering for Sorting ObjectsDening an Ordering for Container Elements508Constant IteratorsOverload Operators Only to Make Programs Easier to Read Dene Comparisons in Terms of Each Other Dene One Operator in Terms of Another568559Symmetry and Conversion560528Returning Local Objects 559525Discrete Event Simulations561Peeking at the Input563The explicit Keyword572Other Operators576Inline Functions576 28. xxviiiTable of Special FeaturesChapter15Memory ManagementCommon ErrorsQuality TipsForgetting the Dual Use of Single Argument Constructors 605 Default Constructor and Parentheses Confusing Destruction and Deletion607Avoid Buffer Overow Errors597When to Use the System-Dened Copy Constructor 609Not Declaring Destructors Virtual618If Destructor, Then Copy Constructor and Assignment 619617Self Assignment612Include Virtual Destructors 615Observing Constructors626Reference Counting Fails in the Presence of Cycles 62716Exception HandlingInvalid Type Parameters645Templates Dont Preserve Inheritance17TemplatesMove from Concrete to Abstract654Document Template Parameter Requirements 655Forgetting to Check Return Values671Nobody Cares How Fast You Get the Wrong Answer669682Tie Exception Classes to the Standard Library681Throwing Objects versus Throwing PointersExceptions During Construction of Global Variables 686Use Exceptions for Exceptional Cases 687 Throwing an Exception is Not a Sign of Shame18Name Scope ManagementConfusing Scope and Lifetime 704646688Dont Pollute the Global Scope 703 Use Accessor Functions for Protected Access to Data708Friendship is Granted, Not Taken756Assuming the Ending Iterator Is Included in a Range756Forgetting to Erase Removed Elements790745Experimentally Evaluate Execution Times761738Mismatched Iterators742Design Your Own Software Frameworks734Failing to Preserve the Is-a Relationship The Standard Template Library734719Avoid Multiple InheritanceTaking Type of Pointer, Not Object Using Type Tests Instead of Polymorphism20Class Hierarchies713Use Unambiguous Names for Name Spaces19710Manage Encapsulation 29. xxixTable of Special FeaturesProductivity HintsTracing ExecutionAdvanced Topics616Random FactsConstructors Are Always Extensions611Overloading the Memory Management Operators621Templates and Overloading648Forms of Polymorphism648Nested Templates654The Ariane Rocket IncidentOverriding, Shadowing, and ScopesFunctional Programming745702Forward References688705Local Name Space Declaration 719A Forest, Not a Tree730Virtual Function Tables735Memory Allocation Traits765 30. xxxTable of Special FeaturesChapter21Object-Oriented Design23The Unied Modeling Language24An Introduction to Design PatternsQuality TipsFeatures of the C++0x Standard22Common ErrorsConsistencyRelational Databases930Graphical User Interfaces26927Pattern Recognition25Confusing Function Objects and Classes83627XMLJoining Tables Without Specifying a Link Condition15XML Describes Structure, Not Appearance4XML Elements Describe Data Fields, Not ClassesXML Is Stricter Than HTML233Prefer XML Elements over Attributes8Avoid Children with Mixed Elements and Text9Stand on the Shoulders of Others23 31. xxxiTable of Special FeaturesProductivity HintsAdvanced TopicsRandom FactsProgramming LanguagesExtreme ProgrammingGeneric Programming with Inheritance and TemplatesLearning About a New Toolkit6Becoming Familiar with a Complex Tool4Avoid Unnecessary Data Replication9187Dont Replicate Columns in a Table8307Stick with the Standard8209Looking for Help on the Internet22Writing an XML Document30Use DTDs with Your XML Files4330Visual Programming41Transactions33Open Source and Free Software19Let the Database Do the Work2419Helper Functions in an XML ParserCustom Dialog BoxesThe XML Schema Specication36Other XML Technologies43Word Processing and Typesetting Systems Grammars, Parsers, and Compilers4 10 32. This page intentionally left blank 33. Chapter1IntroductionCHAPTER 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 algorithmThis 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. 34. 2CHAPTER1 IntroductionC HAPTER C ONTENTS 1.1 What Is a Computer?21.2 What Is Programming?1.7 Becoming Familiar with Your Computer 123 4RANDOM FACT 1.1: The ENIAC and the Dawn of Computing 81.4 Translating Human-Readable Programs to Machine Code 9 1.5 Programming Languages 1.6 The Evolution of C++1011RANDOM FACT 1.2: Standards Organizations 121.114PRODUCTIVITY HINT 1.1: Backup Copies1.3 The Anatomy of a Computer1.8 Compiling a Simple Program15SYNTAX 1.1: Simple Program 18 COMMON ERROR 1.1: Omitting Semicolons 191.9 Errors19COMMON ERROR 1.2: Misspelling Words 211.10 The Compilation Process 1.11 Algorithms2124W 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. 35. 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.2What 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. 36. 4CHAPTER1.31 IntroductionTh e Anatomy of a ComputerTo 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 toFigure 1Central Processing Unit 37. 1.3 The Anatomy of a Computer5DYNAMIC EXECUTIONDYNAMIC EXECUTIONSYSTEM BUSEXECUTION TRACE CACHETRANSFER CACHE FLOATING POINT/ MULTIMEDIA RAPID EXECUTION ENGINE DYNAMIC EXECUTIONHYPER PIPELINEFigure 2CPU Chip Detailstart 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. 38. 6CHAPTER1 IntroductionFigure 3A Hard DiskThe 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 4A Motherboard 39. 1.3 The Anatomy of a Computer7To 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 diskMouse PortsDisk ControllerCPUGraphics cardKeyboardCD-ROM driveMonitor Sound cardRAMSpeakersNetwork card BusFigure 5Schematic Design of a Personal ComputerInternet 40. 8CHAPTER1 IntroductionR 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 6The ENIAC 41. 1.4 Translating Human-Readable Programs to Machine Code1.49Translating 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 astorage 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 memorylocation 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 becomesComputer programs are stored as machine instructions in a code that depends on the processor type.mov 40000, %eax sub 100, %eax jg 11280That 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: 42. 10CHAPTER1 Introductionmov int_rate, %eax sub 100, %eax jg int_errorIt 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.5P r ogramming Langua ge sProgramming 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. 43. 1.6 The Evolution of C++11Compilers 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 ifCompilers 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.6The 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 44. 12CHAPTER1 Introductionlanguage 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.7B e coming Familiar w ith Yo ur ComputerSet 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 InIf 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 45. 1.7 Becoming Familiar with Your Computer13restricted 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++ CompilerComputer 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 FoldersAs 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 46. 14CHAPTER1 Introductionworked 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 ProgramIn 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 WorkYou 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. 47. 1.8 Compiling a Simple Program15 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.8Compiling 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 > nickels; cin =pennies =buffer = .75n state = failnickels = dimes =Figure 2Processing Input10Unchanged because the next input is not an integer 77. 2.3 Assignment445After cin >> dimes; cin =pennies =buffer = .75n state = failnickels = dimes =Figure 210 Unchanged because cin is in the failed stateProcessing Input, continuedinput 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.3Assignment 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() { 78. 46CHAPTER7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 272 Numbers and Objectscout > 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) ...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. 168. 136CHAPTER3 Control FlowIn 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 breakstatement 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.txtthe 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.txtthe 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 169. 3.10 Processing Inputs137character, 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 fivecin fails here, but end of file not yet encounteredA 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: 170. 138CHAPTER3 Control Flowcout > 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 DenverFigure 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. 171. 3.11 Simulations139built 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.11Simulations 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 12The Buffon Needle Experiment 172. 140CHAPTER3 Control Flowch03/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


Recommended