C++ Primer Plus第六版英文版Developer's LibraryESSENTIAL REFERENCES FOR PROGRAMMING PROFESSIONALSDeveloper's Library books are designed to provide practicing programmers withunique, high-quality references and tutorials on the programming languages andtechnologies they use in their daily workAll books in the Developer's Library are written by expert technology practitionerswho are especially skilled at organizing and presenting information in a way that'suseful for other programmersKey titles include some of the best, most widely acclaimed books within theirtopic areas:PHP G MySQL Web DevelopmentPython essential referenceLuke Welling Laura ThomsonDavid beazleISBN-13:978-0-672-32916-6ISBN-13:978-0-672-32862MySQLPostgresQLPaul duboisKorry douglasISBN-13:978-0-672-32938-8ISBN-13:978-0-672-32756-8Linux Kernel developmentC++ Primer plusRobert loveStephen prataISBN-13:978-0-672-32946-3ISBN-13:978-0-321-77640-2Developer,'s library books are available at most retail and online bookstores as wellas by subscription from Safari Books Online at safari informit. comDeveloper'sLibrarinformit. com/devlibraryC++ Primer plusSixth editⅰonStephen prata六 Addison- WesleyUpper Saddle River,NJ· Boston· Indianapolis· San franciscoNew York· Toronto· Montreal· London· Munich· Paris· MadridCape town· Sydney· Tokyo· Singapore· Mexico cityMany of the designations used by manufacturers and sellers to distinguish their productsAcquisitions Editorare claimed as trademarks. Where those designations appear in this book, and the publish- Mark Taberer was aware of a trademark claim, the designations have been printed with initial capitalDevelopmentletters or in all capitals.EditorThe author and publisher have taken care in the preparation of this book, but make noMichael Thurstonexpressed or implied warranty of any kind and assume no responsibility for errors or omis- Managing Editorsions. No liability is assumed for incidental or consequential damages in connection with or Kristy hartarising out of the use of the information or programs contained hereinProject EditorsThe publisher offers excellent discounts on this book when ordered in quantity for bulk pur- Samanthachases or special sales, which may include electronic versions and/or custom covers andSinkhorncontent particular to your business, training goals, marketing focus, and branding interestsJovana shirleFor more information, please contactCopy EditorU.S. Corporate and Government salesBart reed(800)3823419corpsales@pearsontechgroup.comIndexerLisa StumpfFor sales outside the United States, please contactProofreaderInternational salesLanguageinternational@pearson.comLogistics, LLCVisit us on the Web: informit. com/ aw.Technical reviewerDavid horvathLibrary of Congress Cataloging-in-Publication data is on filePublishingCopyright o 2012 Pearson Education, IncCoordinatorVanessa evansAll rights reserved. Printed in the United States of America. This publication is protected bycopyright, and permission must be obtained from the publisher prior to any prohibited reproCover Designerduction, storage in a retrieval system, or transmission in any form or by any means, elec-Gary Adairtronic,mechanical,photocopying, recording, or likewise. To obtain permission to use materi-Compositoral from this work, please submit a written request to Pearson Education, Inc, Permissions Nonie RatcliffDepartment, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax yourrequest to(201)236-3290SBN-13:978032177640-2SBN-10:0-321776402Text printed in the United states on recycled paper at R. R. Donnelley in Crawfordsville,IndianaSecond printing: January 2012To my parents, with loveContents at a GlanceIntroduction 11 Getting Started with C++ 92 Setting Out to C++ 273 Dealing with Data 654 Compound Types 1155 Loops and Relational Expressions 1956 Branching Statements and Logical Operators 2537 Functions: C++'s Programming Modules 3058 Adventures in Functions 3799 Memory Models and Namespaces 44710 Objects and classes 50511 Working with Classes 56312 Classes and Dynamic Memory Allocation 62713 Class Inheritance 70714 Reusing Code in C++ 7855 Friends, Exceptions, and More 8776 The string class and the StandardTemplate Library 95117 Input, output, and Files 10618 visiting with the New C++ Standard 1153Appendixesa Number bases 1215B C++ Reserved words 1221c The ascll character Set 1225D Operator precedence 1231E other Operators 1235F The string Template Class 1249G The Standard Template Library Methods andFunctions 1271H Selected Readings and Internet Resources 1323I Converting to Iso Standard C++ 1327J Answers to Chapter Reviews 1335Index 1367Table of contentsIntroduction 11 Getting Started with C++ 9Learning C++: What Lies Before You 10The Origins of C++: A Little History 10Portability and standards 15The Mechanics of Creating a Program 18Summary 252 Setting out to C++ 27C++ Initiation 27C++ Statements 41More c++ statements 45Functions 48Summary 61Chapter Review 62Programming Exercises 623 Dealing with Data 65Simple variables 66The const Qualifier 90Floating-Point Numbers 92C++ Arithmetic Operators 97Summary 109Chapter Review 110Programming Exercises 1114 Compound Types 115ntroducing Arrays 116Strings 120Introducing the string Class 131Introducing Structures 140Unions 149Enumerations 150Pointers and the free store 153Pointers, Arrays, and Pointer Arithmetic 167Combinations of Types 184Array Alternatives 186Summary 190Chapter Review 191Programming EXercises 192Contents5 Loops and Relational Expressions 195Introducing for Loops 196The while Loop 224The do while Loop 231The Range-Based for Loop(C++11) 233Loops and Text Input 234Nested Loops and Two-Dimensional Arrays 244Summary 249Chapter Review 250Programming EXercises 2516 Branching Statements and Logical Operators 253The if statement 254Logical Expressions 260The ectype Library of Character Functions 270The ? Operator 273The switch statement 274The break and cont inue statements 280Number-Reading Loops 283Simple File Input/output 287Summary 298Chapter Review 298Programming Exercises 3017 Functions: C++'s Programming Modules 305Function review 306Function Arguments and Passing by Value 313Functions and Arrays 320Functions and Two-Dimensional Arrays 337Functions and C-Style Strings 339Functions and structures 343Functions and string Class objects 353Functions and array objects 355Recursion 357Pointers to functions 361Summary 371Chapter Review 372Programming Exercises 3748 Adventures in Functions 379C++ Inline functions 379Reference variables 383Default Arguments 409Function Overloading 412Function Templates 419