+ All Categories
Home > Education > C ++ the_complete_reference_4th_ed

C ++ the_complete_reference_4th_ed

Date post: 12-May-2015
Category:
Upload: siangzalian
View: 1,357 times
Download: 2 times
Share this document with a friend
Popular Tags:
1058
Transcript
  • 1.C++:The Complete Reference, Fourth Edition

2. About the AuthorHerbert Schildt is the worlds leading programmingauthor. He is an authority on the C, C++, Java, and C#languages, and is a master Windows programmer. Hisprogramming books have sold more than 3 millioncopies worldwide and have been translated into allmajor foreign languages. He is the author of numerousbestsellers, including C++: The Complete Reference, C#:The Complete Reference, Java 2: The Complete Reference,C: The Complete Reference, C++ from the Ground Up,C++: A Beginners Guide, C#: A Beginners Guide, andJava 2: A Beginners Guide. Schildt holds a mastersdegree in computer science from the University ofIllinois. He can be reached at his consulting office at(217) 586-4683.Copyright 2003 by The McGraw-Hill Companies. Click here for terms of use. 3. C++:The Complete Reference, Fourth Edition Herbert SchildtMcGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico CityMilan New Delhi San JuanSeoul Singapore Sydney Toronto 4. Copyright 2003 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as per-mitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by anymeans, or stored in a database or retrieval system, without the prior written permission of the publisher.0-07-150239-4The material in this eBook also appears in the print version of this title: 0-07-222680-3.All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarkedname, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of thetrademark. Where such designations appear in this book, they have been printed with initial caps.McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporatetraining programs. For more information, please contact George Hoare, Special Sales, at [email protected] or(212) 904-4069.TERMS OF USEThis is a copyrighted work and The McGraw-Hill Companies, Inc. (McGraw-Hill) and its licensors reserve all rights in and to the work.Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve onecopy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, trans-mit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hills prior consent. You may use thework for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may beterminated if you fail to comply with these terms.THE WORK IS PROVIDED AS IS. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TOTHE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WAR-RANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrantor guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free.Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, inthe work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed throughthe work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, conse-quential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibil-ity of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in con-tract, tort or otherwise.DOI: 10.1036/0072226803 5. ProfessionalWant to learn more?We hope you enjoy thisMcGraw-Hill eBook! Ifyoud like more information about this book,its author, or related books and websites,please click here. 6. Contents at a Glance Part I The Foundation of C++: The C Subset1 An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .574 Arrays and Null-Terminated Strings . . . . . . . . . . . . . . . .895 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1377 Structures, Unions, Enumerations,and User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . 161 8C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187 9File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21110The Preprocessor and Comments . . . . . . . . . . . . . . . . . .237Part II C++11An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25512Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289 v 7. vi C++: The Complete Reference13 Arrays, Pointers, References, and the Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . .32514 Function Overloading, Copy Constructors, and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 35915 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . .38316 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41717 Virtual Functions and Polymorphism . . . . . . . . . . . . . . .44318 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45919 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48720 The C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . . 50921 C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53922 Run-Time Type ID and the Casting Operators . . . . . . .56723 Namespaces, Conversion Functions, and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . 59124 Introducing the Standard Template Library . . . . . . . . . . 629Part III The Standard Function Library25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . 69926 The String and Character Functions . . . . . . . . . . . . . . . .72327 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . .73728 Time, Date, and Localization Functions . . . . . . . . . . . . .74729 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . .75730 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76131 The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . .775Part IV The Standard C++ Class Library32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . .78733 The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . . 81134 The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83935 STL Iterators, Allocators, and Function Objects . . . . . . 86136 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88137 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89738 Exception Handling and Miscellaneous Classes . . . . . .925 8. Contents at a Glance viiPart V Applying C++39Integrating New Classes: A Custom String Class . . . . . 93540Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963 AThe .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999 BC++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009 9. This page intentionally left blank 10. For more information about this title, click here ContentsIntroduction .............................................xxixPart IThe Foundation of C++: The C Subset1 An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3The Origins and History of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4C Is a Middle-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5C Is a Structured Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6C Is a Programmers Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8The Form of a C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The Library and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10Separate Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Understanding the .C and .CPP File Extensions . . . . . . . . . . . . . . . .122 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13The Five Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14Modifying the Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15Identifier Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 Where Variables Are Declared . . . . . . . . . . . . . . . . . . . . . . . .18 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 ix 11. x C++: The Complete ReferenceFormal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21The const and volatile Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Storage Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Variable Initializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32Hexadecimal and Octal Constants . . . . . . . . . . . . . . . . . . . .33String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Backslash Character Constants . . . . . . . . . . . . . . . . . . . . . . .33Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .35Type Conversion in Assignments . . . . . . . . . . . . . . . . . . . . . 35Multiple Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . .38Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . 40Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47The & and * Pointer Operators . . . . . . . . . . . . . . . . . . . . . . .48The Compile-Time Operator sizeof . . . . . . . . . . . . . . . . . . . 50The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50The Dot (.) and Arrow (>) Operators . . . . . . . . . . . . . . . . . 51The [ ] and ( ) Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51Precedence Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52Type Conversion in Expressions . . . . . . . . . . . . . . . . . . . . . . 53Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54Spacing and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . .55Compound Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57True and False in C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60The if-else-if Ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62The ? Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63The Conditional Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 66 12. Contentsxi switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Nested switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . .70Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 for Loop Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Infinite Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 for Loops with No Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . .77 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Declaring Variables Within Selection and Iteration Statements . . .81Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83 The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The exit( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86Expression Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88Block Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884 Arrays and Null-Terminated Strings . . . . . . . . . . . . . . . .89Single-Dimension Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90Generating a Pointer to an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Passing Single-Dimension Arrays to Functions . . . . . . . . . . . . . . . . 92Null-Terminated Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Indexing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105Unsized Array Initializations . . . . . . . . . . . . . . . . . . . . . . . . . 106A Tic-Tac-Toe Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1085 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113What Are Pointers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114Pointer Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115The Pointer Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Pointer Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Pointer Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117Pointer Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122Multiple Indirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123Initializing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Pointers to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 13. xii C++: The Complete ReferenceCs Dynamic Allocation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .129Problems with Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1316 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137The General Form of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138Scope Rules of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139Call by Value, Call by Reference . . . . . . . . . . . . . . . . . . . . . .139Creating a Call by Reference . . . . . . . . . . . . . . . . . . . . . . . . .140Calling Functions with Arrays . . . . . . . . . . . . . . . . . . . . . . . 142argc and argvArguments to main( ) . . . . . . . . . . . . . . . . . . . . . . . .144The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147Returning from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 148Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149Returning Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151Functions of Type void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152What Does main( ) Return? . . . . . . . . . . . . . . . . . . . . . . . . . . 153Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Standard Library Function Prototypes . . . . . . . . . . . . . . . . .157Declaring Variable-Length Parameter Lists . . . . . . . . . . . . . . . . . . . . 158Old-Style Versus Modern FunctionParameter Declarations . . . . . .1587 Structures, Unions, Enumerations,and User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . .161Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Accessing Structure Members . . . . . . . . . . . . . . . . . . . . . . . .165 Structure Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166Passing Structures to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Passing Structure Members to Functions . . . . . . . . . . . . . . . 167 Passing Entire Structures to Functions . . . . . . . . . . . . . . . . . 168Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169 Declaring a Structure Pointer . . . . . . . . . . . . . . . . . . . . . . . . .170 Using Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Arrays and Structures Within Structures . . . . . . . . . . . . . . . . . . . . . . 173Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180Using sizeof to Ensure Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . .183typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1848 C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187An Important Application Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 14. ContentsxiiiReading and Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189 A Problem with getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . .190 Alternatives to getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Reading and Writing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Formatted Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195printf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Printing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196 Printing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Displaying an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198 The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 The Minimum Field Width Specifier . . . . . . . . . . . . . . . . . .199 The Precision Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Handling Other Data Types . . . . . . . . . . . . . . . . . . . . . . . . . .202 The * and # Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202scanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Inputting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Inputting Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . .205 Reading Individual Characters Using scanf( ) . . . . . . . . . . 205 Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Inputting an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Using a Scanset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206 Discarding Unwanted White Space . . . . . . . . . . . . . . . . . . .207 Non-White-Space Characters in the Control String . . . . . . 208 You Must Pass scanf( ) Addresses . . . . . . . . . . . . . . . . . . . . . 208 Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Suppressing Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2099 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211C Versus C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Streams and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Text Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Binary Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213File System Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 The File Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Writing a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 Reading a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 15. xiv C++: The Complete Reference Using fopen( ), getc( ), putc( ), and fclose( ) . . . . . . . . . . . . .218 Using feof( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 Working with Strings: fputs( ) and fgets( ) . . . . . . . . . . . . . .222 rewind( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223 ferror( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224 Erasing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 Flushing a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 Using fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . 227fseek( ) and Random-Access I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . .229fprintf( ) and fscanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230The Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 The Console I/O Connection . . . . . . . . . . . . . . . . . . . . . . . . . 233 Using freopen( ) to Redirect the Standard Streams . . . . . .234 10 The Preprocessor and Comments . . . . . . . . . . . . . . . . . . 237The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238#define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Defining Function-like Macros . . . . . . . . . . . . . . . . . . . . . . .240#error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241#include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242Conditional Compilation Directives . . . . . . . . . . . . . . . . . . . . . . . . . .242 #if, #else, #elif, and #endif . . . . . . . . . . . . . . . . . . . . . . . . . . .243 #ifdef and #ifndef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245#undef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246Using defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247#line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248#pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248The # and ## Preprocessor Operators . . . . . . . . . . . . . . . . . . . . . . . . . 248Predefined Macro Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250Single-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252Part II C++ 11 An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255The Origins of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256What Is Object-Oriented Programming? . . . . . . . . . . . . . . . . . . . . . .257Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Some C++ Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259A Sample C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 16. Contentsxv A Closer Look at the I/O Operators . . . . . . . . . . . . . . . . . . .263 Declaring Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 No Default to int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 The bool Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266 Old-Style vs. Modern C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267 The New C++ Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269 Working with an Old Compiler . . . . . . . . . . . . . . . . . . . . . . .270 Introducing C++ Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .278 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 The C++ Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287 The General Form of a C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . 28812 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Structures and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . .293 Unions and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295Anonymous Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Friend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Friend Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302 Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303Defining Inline Functions Within a Class . . . . . . . . . . . . . . 306 Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307Constructors with One Parameter: A Special Case . . . . . . .309 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310Static Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310Static Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 When Constructors and Destructors Are Executed . . . . . . . . . . . . .317 The Scope Resolution Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 Local Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 Passing Objects to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Object Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32413 Arrays, Pointers, References, and the Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . .325 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Creating Initialized vs. Uninitialized Arrays . . . . . . . . . . . 328 Pointers to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Type Checking C++ Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .332 17. xvi C++: The Complete ReferenceThe this Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332Pointers to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .334Pointers to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Reference Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Passing References to Objects . . . . . . . . . . . . . . . . . . . . . . . .343Returning References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Independent References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345References to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . .346Restrictions to References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347A Matter of Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347C++s Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . 347Initializing Allocated Memory . . . . . . . . . . . . . . . . . . . . . . .349Allocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350Allocating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351The nothrow Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . .356The Placement Form of new . . . . . . . . . . . . . . . . . . . . . . . . .357 14 Function Overloading, Copy Constructors,and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . .359Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362Overloading a Constructor to Gain Flexibility . . . . . . . . . .362Allowing Both Initialized and Uninitialized Objects . . . . .364Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366Finding the Address of an Overloaded Function . . . . . . . . . . . . . . .370The overload Anachronism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371Default Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371Default Arguments vs. Overloading . . . . . . . . . . . . . . . . . .376Using Default Arguments Correctly . . . . . . . . . . . . . . . . . . .377Function Overloading and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . 378 15 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383Creating a Member Operator Function . . . . . . . . . . . . . . . . . . . . . . .384Creating Prefix and Postfix Forms of the Increment and Decrement Operators . . . . . . . . . .389Overloading the Shorthand Operators . . . . . . . . . . . . . . . . .390Operator Overloading Restrictions . . . . . . . . . . . . . . . . . . . .390Operator Overloading Using a Friend Function . . . . . . . . . . . . . . . . 391Using a Friend to Overload ++ or . . . . . . . . . . . . . . . . . . 393Friend Operator Functions Add Flexibility . . . . . . . . . . . . .396Overloading new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398Overloading new and delete for Arrays . . . . . . . . . . . . . . .403Overloading the nothrow Version of new and delete . . . .406 18. Contentsxvii Overloading Some Special Operators . . . . . . . . . . . . . . . . . . . . . . . . .407Overloading [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .407Overloading ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .411Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 Overloading the Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .41416 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 Base-Class Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Inheritance and protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . 420Protected Base-Class Inheritance . . . . . . . . . . . . . . . . . . . . . . 424 Inheriting Multiple Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .425 Constructors, Destructors, and Inheritance . . . . . . . . . . . . . . . . . . . .426When Constructors and Destructors Are Executed . . . . . . 426Passing Parameters to Base-Class Constructors . . . . . . . . .430 Granting Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434 Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43717 Virtual Functions and Polymorphism . . . . . . . . . . . . . . .443 Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 Calling a Virtual Function Through a BaseClass Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .447 The Virtual Attribute Is Inherited . . . . . . . . . . . . . . . . . . . . . . . . . . . .448 Virtual Functions Are Hierarchical . . . . . . . . . . . . . . . . . . . . . . . . . . .450 Pure Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .453 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .455 Using Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Early vs. Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45818 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 A Function with Two Generic Types . . . . . . . . . . . . . . . . . . . 463 Explicitly Overloading a Generic Function . . . . . . . . . . . . . 463 Overloading a Function Template . . . . . . . . . . . . . . . . . . . . . 466 Using Standard Parameters with Template Functions . . . . 466 Generic Function Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 467 Applying Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468 A Generic Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469 Compacting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 Generic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 An Example with Two Generic Data Types . . . . . . . . . . . . .476 Applying Template Classes: A Generic Array Class . . . . .477 Using Non-Type Arguments with Generic Classes . . . . . . 479 Using Default Arguments with Template Classes . . . . . . . 481 Explicit Class Specializations . . . . . . . . . . . . . . . . . . . . . . . . .483 19. xviii C++: The Complete ReferenceThe typename and export Keywords . . . . . . . . . . . . . . . . . . . . . . . . .484The Power of Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485 19 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .487Exception Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . 488Catching Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494Using Multiple catch Statements . . . . . . . . . . . . . . . . . . . . . . 495Handling Derived-Class Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 497Exception Handling Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498Catching All Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498Restricting Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .500Rethrowing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502Understanding terminate( ) and unexpected( ) . . . . . . . . . . . . . . . . .503Setting the Terminate and Unexpected Handlers . . . . . . . . 504The uncaught_exception( ) Function . . . . . . . . . . . . . . . . . . . . . . . . .505The exception and bad_exception Classes . . . . . . . . . . . . . . . . . . . . . 506Applying Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 20 The C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . . 509Old vs. Modern C++ I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .510C++ Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511The C++ Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .511C++s Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . .512Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513Formatting Using the ios Members . . . . . . . . . . . . . . . . . . . .513Setting the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . .514Clearing Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515An Overloaded Form of setf( ) . . . . . . . . . . . . . . . . . . . . . . . . 516Examining the Formatting Flags . . . . . . . . . . . . . . . . . . . . . .518Setting All Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519Using width( ), precision( ), and fill( ) . . . . . . . . . . . . . . . . . 520Using Manipulators to Format I/O . . . . . . . . . . . . . . . . . . .522Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526Creating Your Own Inserters . . . . . . . . . . . . . . . . . . . . . . . . . 526Creating Your Own Extractors . . . . . . . . . . . . . . . . . . . . . . . .532Creating Your Own Manipulator Functions . . . . . . . . . . . . 535 21 C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539 and the File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .540Opening and Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .540Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543Unformatted and Binary I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .545Characters vs. Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .545put( ) and get( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546read( ) and write( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .548 20. Contentsxix More get( ) Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .551 getline( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 Detecting EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .553 The ignore( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 peek( ) and putback( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556 flush( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556 Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .557Obtaining the Current File Position . . . . . . . . . . . . . . . . . . . 561 I/O Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561 Customized I/O and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56322 Run-Time Type ID and the Casting Operators . . . . . . . 567 Run-Time Type Identification (RTTI) . . . . . . . . . . . . . . . . . . . . . . . . .568 The Casting Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .578 dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57823 Namespaces, Conversion Functions,and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . 591 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592Namespace Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . .592using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596Unnamed Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .598Some Namespace Options . . . . . . . . . . . . . . . . . . . . . . . . . . .599 The std Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .601 Creating Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .603 const Member Functions and mutable . . . . . . . . . . . . . . . . . . . . . . . . 607 Volatile Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .609 Explicit Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .610 The Member Initialization Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611 Using the asm Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .616 Linkage Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .617 Array-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .618The Array-Based Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619Creating an Array-Based Output Stream . . . . . . . . . . . . . . . 619Using an Array as Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621Input/Output Array-Based Streams . . . . . . . . . . . . . . . . . . .623Using Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .624Using Binary I/O with Array-Based Streams . . . . . . . . . . . 625 Summarizing the Differences Between C and C++ . . . . . . . . . . . . .62624 Introducing the Standard Template Library . . . . . . . . . .629 An Overview of the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 21. xx C++: The Complete ReferenceAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .631Other STL Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632 The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 General Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635Accessing a Vector Through an Iterator . . . . . . . . . . . . . . . . 639Inserting and Deleting Elements in a Vector . . . . . . . . . . . .641Storing Class Objects in a Vector . . . . . . . . . . . . . . . . . . . . . .643 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645Understanding end( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649push_front( ) vs. push_back( ) . . . . . . . . . . . . . . . . . . . . . . . . 651Sort a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .652Merging One List with Another . . . . . . . . . . . . . . . . . . . . . .653Storing Class Objects in a List . . . . . . . . . . . . . . . . . . . . . . . .655 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .658Storing Class Objects in a Map . . . . . . . . . . . . . . . . . . . . . . . 662 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .664Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664Removing and Replacing Elements . . . . . . . . . . . . . . . . . . .670Reversing a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Transforming a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . .673 Using Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .675Unary and Binary Function Objects . . . . . . . . . . . . . . . . . . .675Using the Built-in Function Objects . . . . . . . . . . . . . . . . . . .675Creating a Function Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 678Using Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .680 The string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .683Some string Member Functions . . . . . . . . . . . . . . . . . . . . . . . 687Strings Are Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693Putting Strings into Other Containers . . . . . . . . . . . . . . . . .694 Final Thoughts on the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695 Part IIIThe Standard Function Library25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . 699clearerr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700fclose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701feof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701ferror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701fflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702fgetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .702fgetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .702 22. Contentsxxi fgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703 fopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703 fprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705 fputc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705 fputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706 fread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706 freopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706 fscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .707 fseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 fsetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708 ftell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708 fwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .709 getc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .709 getchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710 gets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .710 perror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .710 printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .711 putc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .714 putchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714 puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .714 remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .715 rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .715 rewind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .715 scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 setbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .719 setvbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719 sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720 sscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .720 tmpfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720 tmpnam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .721 ungetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .721 vprintf, vfprintf, and vsprintf . . . . . . . . . . . . . . . . . . . . . . . . 72226 The String and Character Functions . . . . . . . . . . . . . . . .723 isalnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 isalpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 iscntrl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725 isdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725 isgraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725 islower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725 isprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 ispunct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 isspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 isupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727 23. xxii C++: The Complete Reference isxdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .727 memchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .727 memcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .727 memcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .728 memmove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728 memset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .729 strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .729 strchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .729 strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .730 strcoll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730 strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .731 strcspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731 strerror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .731 strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .731 strncat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732 strncmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732 strncpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 strpbrk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 strrchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 strspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .734 strstr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .734 strtok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .734 strxfrm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 tolower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 toupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73527 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . .737 acos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738 asin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738 atan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .739 atan2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739 ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .739 cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740 cosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .740 exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740 fabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .741 floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741 fmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .741 frexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741 ldexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742 log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742 log10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742 modf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .743 pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743 sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743 24. Contentsxxiii sinh ............................................... 744 sqrt ............................................... 744 tan............................................... 744 tanh.............................................. 74528 Time, Date, and Localization Functions . . . . . . . . . . . . . 747 asctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .748 clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .749 ctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .749 difftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750 gmtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750 localeconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750 localtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .752 mktime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752 setlocale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .752 strftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753 time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75429 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . 757 calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758 free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758 malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759 realloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75930 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .761 abort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .762 abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .762 assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763 atexit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763 atof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763 atoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764 atol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764 bsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .764 div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .765 exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766 getenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766 labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766 ldiv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767 longjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .767 mblen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .767 mbstowcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768 mbtowc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768 qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .768 raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .769 25. xxiv C++: The Complete Referencerand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770setjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770srand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .771strtod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771strtol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772strtoul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .772system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773va_arg, va_start, and va_end . . . . . . . . . . . . . . . . . . . . . . . . . 773wcstombs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774wctomb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77431 The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . .775 The Wide-Character Classification Functions . . . . . . . . . . . . . . . . . . 776 The Wide-Character I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . .779 The Wide-Character String Functions . . . . . . . . . . . . . . . . . . . . . . . . . 779 Wide-Character String Conversion Functions . . . . . . . . . . . . . . . . . .779 Wide-Character Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .782 Multibyte/Wide-Character Conversion Functions . . . . . . . . . . . . . . 783 Part IVThe Standard C++ Class Library32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . .787 The I/O Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788 The I/O Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790 The Format Flags and I/O Manipulators . . . . . . . . . . . . . . . . . . . . . . 791 Several Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .793 The streamsize and streamoff Types . . . . . . . . . . . . . . . . . . .793 The streampos and wstreampos Types . . . . . . . . . . . . . . . .793 The pos_type and off_type Types . . . . . . . . . . . . . . . . . . . . . 793 The openmode Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793 The iostate Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .794 The seekdir Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .794 The failure Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794 Overload > Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .794 The General-Purpose I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . 795 bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795 clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795 eof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795 exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .796 fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .796 fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .796 flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797 26. Contentsxxvflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797fstream, ifstream, and ofstream . . . . . . . . . . . . . . . . . . . . . . . 797gcount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .798get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798getline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .800ignore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .800open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .800peek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .801precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802put . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802putback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802rdstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .803readsome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .803seekg and seekp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804setf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805setstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806stringstream, istringstream, ostringstream . . . . . . . . . . . . .806sync_with_stdio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807tellg and tellp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808unsetf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .808width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80933 The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . .811 The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .812bitset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .814deque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .819map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822multimap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .824multiset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .827queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829priority_queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .830set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83434 The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839adjacent_find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840binary_search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .841 27. xxvi C++: The Complete Reference copy_backward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .841 count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .841 count_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842 equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .842 equal_range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .842 fill and fill_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .843 find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 find_end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 find_first_of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .843 find_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .844 for_each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844 generate and generate_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . .844 includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845 inplace_merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .845 iter_swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .845 lexicographical_compare . . . . . . . . . . . . . . . . . . . . . . . . . . . .846 lower_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .846 make_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .846 max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .847 max_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .847 merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .847 min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .848 min_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .848 mismatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848 next_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849 nth_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .849 partial_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849 partial_sort_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .850 partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .850 pop_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850 prev_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 push_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .851 random_shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 remove, remove_if, remove_copy, and remove_copy_if . 852 replace, replace_copy, replace_if, and replace_copy_if . . . 852 reverse and reverse_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853 rotate and rotate_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853 search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854 search_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854 set_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854 set_intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855 set_symmetric_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855 set_union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .856 sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856 sort_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .856 28. Contentsxxviistable_partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857stable_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .857swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857swap_ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .858transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .858unique and unique_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858upper_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85935 STL Iterators, Allocators, and Function Objects . . . . . . 861 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862The Basic Iterator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862The Low-Level Iterator Classes . . . . . . . . . . . . . . . . . . . . . . . 863The Predefined Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864Two Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872 Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .872Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .874Negators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875Adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876 Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87936 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .881 The basic_string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .882 The char_traits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89437 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897 The complex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898 The valarray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .902 The slice and gslice Classes . . . . . . . . . . . . . . . . . . . . . . . . . .917 The Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .920 The Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .920 accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .920 adjacent_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921 inner_product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922 partial_sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92338 Exception Handling and Miscellaneous Classes . . . . . .925 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .926 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927 auto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .928 The pair Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .930 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .931 Other Classes of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931 29. xxviii C++: The Complete Reference Part VApplying C++39 Integrating New Classes: A Custom String Class . . . . .935 The StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936 The Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . .938 I/O on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .939 The Assignment Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .941 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942 Substring Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945 The Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .947 Miscellaneous String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .948 The Entire StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .949 Using the StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958 Creating and Integrating New Types in General . . . . . . . . . . . . . . . 961 A Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96140 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964 Parsing Expressions: The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . .965 Parsing an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966 The Parser Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .968 Dissecting an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .969 A Simple Expression Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .971 Understanding the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . .977 Adding Variables to the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .978 Syntax Checking in a Recursive-Descent Parser . . . . . . . . . . . . . . . . 988 Building a Generic Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989 Some Things to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .997 A The .NET Managed Extensions to C++ . . . . . . . . . . . . . .999 The .NET Keyword Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000 Preprocessor Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002 The attribute Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003 Compiling Managed C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003 B C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009 30. f there is one language that defines modern programming, it is C++. Its syntax,Istyle, and philosophy have set the standard by which all other languages are judged. Furthermore, C++ is the universal language of programming. When analgorithm or technique is described, it is usually done so using the C++ syntax. Thelong-term success of C++ has also left a lasting impression on computer languagedevelopment. For example, both Java and C# are descended from C++. Frankly, to bea professional programmer implies proficiency in C++. It is the one language that noprogrammer can afford to ignore.This is the fourth edition of C++: The Complete Reference. It fully describes anddemonstrates the keywords, syntax, functions, classes, and features that define the C++language. More specifically, this book fully describes Standard C++. This is the versionof C++ defined by the ANSI/ISO Standard for C++ and it is the version of C++ that issupported by all major compilers, including Microsofts Visual C++ and Borlands C++Builder. Thus, the information in this book is applicable to all modern programmingenvironments.In the time that has passed since the previous edition of this book, there havebeen no changes to the C++ language. There have, however, been big changes to thecomputing environment. For example, a new standard for C, called C99, was created,Java became the dominant language for Web programming, the .NET Framework wasxxixCopyright 2003 by The McGraw-Hill Companies. Click here for terms of use. 31. xxx C++: The Complete Referencereleased, and C# was invented. Through all the changes of the past few years, one thinghas remained constant: the staying power of C++. C++ has been, is, and will remain thepreeminent language for the development of high-performance software well into theforeseeable future.Whats New in the Fourth EditionThe overall structure and organization of the fourth edition is similar to the third edition.Thus, if you have been using the third edition, you will feel right at home with the fourthedition. Most of the changes to the fourth edition involve updating and expanding thecoverage throughout. In some cases, additional details were added. In other cases, thepresentation of a topic was improved. In still other situations, descriptions weremodernized to reflect the current programming environment. Several new sections were alsoadded. In Part One, the relationship of C++ to the new C standard, called C99, is notedwhere appropriate.Two appendices were also added. The first described the extended keywordsdefined by Microsoft that are used for creating managed code for the .NET Framework.The second shows off an area of personal interest: robotics. Robotics has long been ahobby of mine and I thought that many readers would find my experimental robot tobe of interest. Most of the software that drives it is, of course, written in C++!Finally, all code examples were retested against the current crop of compilers,including Microsofts Visual Studio .NET and Borlands C++ Builder.Whats InsideThis books covers in detail all aspects of the C++ language, including its foundation, C.The book is divided into these five parts: The C SubsetThe foundation of C++ The C++ language The Standard Function Library The Standard Class Library Sample C++ applicationsPart One provides a comprehensive discussion of the C subset of C++. As mostreaders will know, C is the foundation upon which C++ was built. It is the C subsetthat defines the bedrock features of C++, including such things as for loops and ifstatements. It also defines the essential nature of C++s block structure, pointers, andfunctions. Since many readers are already familiar with and proficient in C, discussingthe C subset separately in Part One prevents the knowledgeable C programmer fromhaving to wade through reams of information he or she already knows. Instead, the 32. xxxiexperienced C programmer can simply turn to the sections of this book that cover theC++-specific features.Part Two discusses in detail the features that move beyond the C foundation anddefine the C++ language These include its object-oriented features such as classes,constructors, destructors, RTTI, and templates. Thus, Part Two covers those constructsthat make C++, C++.Part Three describes the standard function library and Part Four examines thestandard class library, including the STL (Standard Template Library). Part Fiveshows two practical examples of applying C++ and object-oriented programming.A Book for All ProgrammersThis C++ reference is designed for all C++ programmers, regardless of their experiencelevel. It does assume, however, a reader able to create at least a simple program. If youare just learning C++, this book will make an excellent companion to any C++ tutorialand serve as a source of answers to your specific questions. Experienced C++ pros willfind the in-depth coverage of C++s more advanced features especially useful.If Youre Using WindowsIf your computer uses Windows, then you have chosen the right language. C++ iscompletely at home with Windows programming. However, none of the programs inthis book are Windows programs. Instead, they are console-based programs. The reason forthis is easy to understand: Windows programs are, by their nature, large and complex. Theoverhead required to create even a minimal Windows skeletal program is 50 to 70 linesof code. To write Windows programs that demonstrate the features of C++ wouldrequire hundreds of lines of code each. Put simply, Windows is not an appropriateenvironment in which to discuss the features of a programming language. However,you can still use a Windows-based compiler to compile the programs in this bookbecause the compiler will automatically create a console session in which to executeyour program.Dont Forget: Code on the WebRemember, the source code for all of the programs in this book is available free-of-charge on the Web at www.osborne.com. Downloading this code prevents you fromhaving to type in the examples. 33. xxxii C++: The Complete ReferenceFor Further StudyC++: The Complete Reference is your gateway to the Herb Schildt series ofprogramming books. Here are some others that you will find of interest.To learn more about C++, try C++: A Beginners Guide C++ from the Ground Up Teach Yourself C++ STL Programming from the Ground Up C++ Programmers ReferenceTo learn about Java programming, we recommend the following: Java 2: A Beginners Guide Java 2: The Complete Reference Java 2 Programmers ReferenceTo learn about C#, Herb offers these books: C#: A Beginners Guide C#: The Complete ReferenceTo learn about Windows programming we suggest the following Schildt books: Windows 98 Programming from the Ground Up Windows 2000 Programming from the Ground Up MFC Programming from the Ground Up The Windows Programming Annotated ArchivesIf you want to learn about the C language, which is the foundation of all modernprogramming, then the following titles will be of interest. C: The Complete Reference Teach Yourself C When you need solid answers, fast, turn to Herbert Schildt,the recognized authority on programming. 34. Part IThe Foundation of C++:The C SubsetThis book divides its description of the C++ language into two parts.Part One discusses the C-like features of C++. This is commonlyreferred to as the C subset of C++. Part Two describes those featuresspecific to C++. Together, these parts describe the entire C++ language.Copyright 2003 by The McGraw-Hill Companies. Click here for terms of use. 35. As you may know, C++ was built upon the foundation of C. In fact, C++ includesthe entire C lan


Recommended