C++PrimerPlus
Primer Plus C++ Stephen Prata Fifth EditionC++ Primer plusASSOCIATE PUBLISHERMichael StephensCopyright o 2005 by Sams PublishingACQUISITIONS EDITORAll rights reserved. No part of this book shall be reproduced, stored in a retrieval systemLoretta Yatesor transmitted by any means, electronic, mechanical, photocopying, recording, or otherwiseDEVELOPMENT EDITORwithout written permission from the publisher. No patent liability is assumed with respectSonglin Qiuto the use of the information contained hereinugh every precaution has been takenin the preparation of this book, the publisher and author assume no responsibility for errorsMANAGING EDITORor omissions. Nor is any liability assumed for damages resulting from the use of the inforCharlotte Clapmation contained hereinPROJECT EDITORInternational Standard Book Number: 0-672-32697-3George E. NedeffLibrary of Congress Catalog Card Number: 2004095067COPY EDITORPrinted in the United states of americaKitty JarrettFirst Printing: November. 2004INDEXER07060504Erika millenPROOFREADERtrademarksSuzanne thomasTECHNICAL EDITORAll terms mentioned in this book that are known to be trademarks or service marks haveDavid horvathbeen appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of anyPUBLISHINGtrademark or service markCOORDINATORCindy teetersWarning and DisclaimerMULTIMEDIA DEVELOPERDan ScherfEvery effort has been made to make this book as complete and as accurate as possible butBOOK DESIGNERno warranty or fitness is implied. The information provided is on an"as is"basisGary adairBulk salesSams Publishing offers excellent discounts on this book when ordered in quantity for bulkpurchases or special sales. For more information, please contactU.S. Corporate and Government Sales1-800-382-3419corpsales@pearsontechgroup.comFor sales outside of the U.S., please contactInternational sales1317-428-3341international@pearsontechgroup comCONTENTS AT A GLANCEINTRODUCTIONCHAPTeR 1 Getting StartedCHAPTER 2 Setting Out to C++29CHAPTER 3 Dealing with DataCHAPTER 4 Compound Types109CHAPTER 5 Loops and Relational Expressions177ChaPTer 6 Branching Statements and Logical Operators231CHAPTER 7 Functions: C++'s Programming module279ChaPter 8 Adventures in Functions337CHAPTER 9 Memory Models and Namespaces393ChaPTer 10 Objects and Classes445CHAPTER 1 1 Working with Classes501CHAPTER 12 Classes and Dynamic Memory Allocation...561CHAPTER 13 Class Inheritance633CHAPTER 14 Reusing Code in C++701CHAPTER 15 Friends, Exceptions, and More787CHAPTER 16 The string Class and the Standard Template Library857CHAPTER 17 Input, Output, and Files951APPENDIX A Number bases1041APPENDIX B C++ Reserved Words1047APPENDIX C The ASCii Character Set1051APPENDIX D Operator Precedence...1057APPENDIX E Other Operators1063APPENDIX F The string Template Class1075APPENDIX G The STL Methods and Functions1095APPENDIX H Selected Readings and Internet Resources1129APPENDIX I Converting to ANSi/ISO Standard C++1133APPENDIX J Answers to Review Questions1141INDEX1165TABLE OF CONTENTSINTRODUCTIONCHAPTER 1: Getting StartedLearning C++: What Lies Before YouThe Origins of C++: A Little HistorThe C language13C Programming PhilosophyThe C++ Shift: Object-Oriented Programming14C++ and Generic ProgrammingThe genesis of c++16Portability and standards17The Mechanics of creating a ProgramCreating the Source Code file20Compilation and linki22Summary27CHAPTER 2: Setting Out to C++29C++ Initiation29The main( Function31C++ Comments34The C++ Preprocessor and the iostream File35Header filenames36Namespaaces37C++ Output with cout38C++ Source Code Formatting.41C++ Statements43Declaration Statements and variables.43Assignment statements45A New Trick for cout46More c++ statements47Using cin47Concatenating with cout48cin and cout: A Touch of Class48Functions50Using a Function That Has a Return Value50Function variations54User-Defined functions55Using a User-Defined Function That Has a Return Value58Placing the using Directive in Multifunction Programsvi C++ PRimer PlUS, FIFTH editionummar)Review questions63Programming exercisesCHAPTER 3: Dealing with DataSimple variables66Names for variables66Integer TypesThe short, int, and long Integer TypesUnsigned Types73Choosing an Integer Type75Integer constantsHow C++ Decides what Type a Constant Is78The char Type: Characters and Small IntegersThe bool Type87The const QualifierFloating-Point NumbersWriting Floating-Point NumbersFloating-Point Types91Floating-Point Constants93Advantages and Disadvantages of Floating-Point NumbersC++ Arithmetic Operators95Order of Operation: Operator Precedence and AssociativityDivision diversions97The Modulus OperatorType conversions100ummary105Review questions106Programming Exercises107CHAPTER 4: Compound Types109Introducing Arrays110Program Notes..112Initialization Rules for arraysStrings..114Concatenating String Constants116Using Strings in an Array..116Adventures in String Input118Reading String Input a Line at a TimeMixing String and Numeric Input..124CONTENTs viiIntroducing the string Class125Assignment, Concatenation, and Appending126More string Class Operations127More on string Class 1/O129Introducing Structures131Using a Structure in a Program133Can a Structure Use a string Class Member?135Other Structure Properties136Arrays of Structures137Bit Fields in Structures139ions139Enumerations41Setting enumerator values142Value ranges for enumerations143Pointers and the Free Store144Declaring and Initializing Pointers147Pointer danger149Pointers and numbers150Allocating Memory with new150Freeing memory with delete152Using new to Create Dynamic arrays153Pointers, Arrays, and Pointer Arithmetic156Program Notes157Pointers and Strings162Using new to Create dynamic structures166Automatic Storage, Static Storage, and Dynamic Storage170Summary172Review Questions173Programming exercises174CHAPTER 5: Loops and Relational Expressions177Introducing for Loopsfor Loop Parts179Back to the for Loop185Changing the Step size187Inside Strings with the for Loop188The Increment(+) and Decrement (--)Operators189Side effects and Sequence points190Prefixing Versus Postfixing191The Increment/Decrement Operators and Pointers191Combination Assignment Operators192viii C++ PRimer PlUS, FIFTH edItionCompound Statements, or BlocksThe Comma Operator (or More Syntax Tricks)195Relational expressions198A Mistake You'll Probably make..199Comparing C-Style Strings201Comparing string Class Strings204ne205Program Notes207for versus while207Just a Moment--Building a Time-Delay Loop209The do while Loop..211Loops and Text Input213Using Unadorned cin for Input..214cin. get(char)to the rescue215Which cin. get(?..216The End-of-File Condition217Yet Another Version of cin. get(220Nested Loops and Two-Dimensional Arrays223Initializing a Two-Dimensional array225Summary227Review Questions228Programming exercises229CHAPTER 6: Branching Statements and Logical Operators231The if Statement231The if else Statement233Formatting if else Statements235The if else if else Construction236Logical Expressions238The Logical OR Operator238The Logical AND Operator: &239The logical not operator244ogical Operator Facts246Alternative representations247The ectype Library of Character Functions247The Operator250The switch Stateme251Using Enumerators as labels255switch and if else256The break and continue Statements256P1258CONTENTSⅸNumber-Reading Loops.259Program Notes262Simple File Input/Outpr262Text i/o and text files263g to a Text F264Reading from a Text File268Summary.273Review Qu274Programming Exercises276CHAPTER 7: Functions: C++'s Programming Modules279Function review280Defining a Function281Prototyping and Calling a Function283Function Arguments and Passing by value286Multiple arguments288Another Two-Argument Function290Functions and arrays293How Pointers Enable Array-Processing Functions294The Implications of Using Arrays as Arguments295More Array Function Examples297Functions Using Array ranges303Pointers and const305Functions and Two-Dimensional arrays308Functions and C-Style Strings309Functions with C-Style String ArgumentsFunctions That Return C-Style strings312Functions and structures313Passing and Returning Structures314Another Example of Using Functions with Structures316Passing Structure Addresses320Functions and string Class Objects322Recursion324Recursion with a Single recursive call324Recursion with Multiple recursive calls326Pointers to functions327Function Pointer basics328A Function Pointer Example330Summary332Review Questions333Programming Exercises334
用户评论