c++ : the complete reference by herbert schildt (4th edition)

1058

Upload: pran-krishna-das

Post on 06-May-2015

3.646 views

Category:

Engineering


709 download

DESCRIPTION

C++ : The Complete Reference by Herbert Schildt (4th Edition) Note: I share this book for educational purpose only. If you really like this book please buy it and help the authors, they deserve it. All The Best. :) If this book is helpful to you then don't forget to like, share and comment. Facebook: www.facebook.com/pran93

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 masterWindows 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 EditionHerbert SchildtMcGraw-Hill/OsborneNew York Chicago San FranciscoLisbon 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-mittedunder 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-RANTIESOF 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-quentialor 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-ityof 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 GlancePart 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 . . . . . . . . . . . . . . . . . . . . . . . . 1618 C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1879 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21110 The Preprocessor and Comments . . . . . . . . . . . . . . . . . . 237Part II C++11 An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25512 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289v 7. vi C + + : T h e C o m p l e t e R e f e r e n c e13 Arrays, Pointers, References, and the DynamicAllocation 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. Part V Applying C++C o n t e n t s a t a G l a n c e vii39 Integrating New Classes: A Custom String Class . . . . . 93540 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963A The .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999B C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009 9. This page intentionally left blank 10. For more information about this title, click hereContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Where Variables Are Declared . . . . . . . . . . . . . . . . . . . . . . . . 18Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18ix 11. x C + + : T h e C o m p l e t e R e f e r e n c eFormal 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. C o n t e n t s xiswitch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Nested switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70for Loop Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72The Infinite Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76for Loops with No Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . 77The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Declaring Variables Within Selection and Iteration Statements . . . 81Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83The exit( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85The 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 + + : T h e C o m p l e t e R e f e r e n c eC's 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Accessing Structure Members . . . . . . . . . . . . . . . . . . . . . . . . 165Structure Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Passing Structures to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Passing Structure Members to Functions . . . . . . . . . . . . . . . 167Passing Entire Structures to Functions . . . . . . . . . . . . . . . . . 168Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Declaring a Structure Pointer . . . . . . . . . . . . . . . . . . . . . . . . . 170Using 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. C o n t e n t s xiiiReading and Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189A Problem with getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Alternatives to getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Reading and Writing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Formatted Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195printf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Printing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Printing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Displaying an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199The Minimum Field Width Specifier . . . . . . . . . . . . . . . . . . 199The Precision Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Handling Other Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 202The * and # Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202scanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Inputting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Inputting Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . . 205Reading Individual Characters Using scanf( ) . . . . . . . . . . 205Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Inputting an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Using a Scanset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Discarding Unwanted White Space . . . . . . . . . . . . . . . . . . . 207Non-White-Space Characters in the Control String . . . . . . 208You Must Pass scanf( ) Addresses . . . . . . . . . . . . . . . . . . . . . 208Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Suppressing Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2099 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211C Versus C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Streams and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Text Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Binary Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213File System Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214The File Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Writing a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Reading a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 15. xiv C + + : T h e C o m p l e t e R e f e r e n c eUsing fopen( ), getc( ), putc( ), and fclose( ) . . . . . . . . . . . . . 218Using feof( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Working with Strings: fputs( ) and fgets( ) . . . . . . . . . . . . . . 222rewind( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223ferror( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Erasing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Flushing a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Using fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . 227fseek( ) and Random-Access I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229fprintf( ) and fscanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230The Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232The Console I/O Connection . . . . . . . . . . . . . . . . . . . . . . . . . 233Using freopen( ) to Redirect the Standard Streams . . . . . . 23410 The Preprocessor and Comments . . . . . . . . . . . . . . . . . . 237The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238#define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238Defining 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 IIC++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. C o n t e n t s xvA Closer Look at the I/O Operators . . . . . . . . . . . . . . . . . . . 263Declaring Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264No Default to int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265The bool Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266Old-Style vs. Modern C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267The New C++ Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Working with an Old Compiler . . . . . . . . . . . . . . . . . . . . . . . 270Introducing C++ Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283The C++ Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287The General Form of a C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . 28812 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Structures and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . 293Unions and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295Anonymous Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Friend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297Friend Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303Defining Inline Functions Within a Class . . . . . . . . . . . . . . 306Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307Constructors with One Parameter: A Special Case . . . . . . . 309Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310Static Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310Static Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315When Constructors and Destructors Are Executed . . . . . . . . . . . . . 317The Scope Resolution Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Local Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320Passing Objects to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323Object Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32413 Arrays, Pointers, References, and the DynamicAllocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 325Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326Creating Initialized vs. Uninitialized Arrays . . . . . . . . . . . 328Pointers to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329Type Checking C++ Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 17. xvi C + + : T h e C o m p l e t e R e f e r e n c eThe 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 . . . . . . . . . . . . . . . . . . . . . . . . . 35714 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 . . . . . . . . . . . . . . . . . . . . . . . . 37815 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383Creating a Member Operator Function . . . . . . . . . . . . . . . . . . . . . . . 384Creating Prefix and Postfix Formsof 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. C o n t e n t s xviiOverloading Some Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . 407Overloading [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407Overloading ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Overloading the Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 41416 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417Base-Class Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418Inheritance and protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . 420Protected Base-Class Inheritance . . . . . . . . . . . . . . . . . . . . . . 424Inheriting Multiple Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425Constructors, Destructors, and Inheritance . . . . . . . . . . . . . . . . . . . . 426When Constructors and Destructors Are Executed . . . . . . 426Passing Parameters to Base-Class Constructors . . . . . . . . . 430Granting Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43717 Virtual Functions and Polymorphism . . . . . . . . . . . . . . . 443Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444Calling a Virtual Function Through a BaseClass Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447The Virtual Attribute Is Inherited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448Virtual Functions Are Hierarchical . . . . . . . . . . . . . . . . . . . . . . . . . . . 450Pure Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Using Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Early vs. Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45818 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460A Function with Two Generic Types . . . . . . . . . . . . . . . . . . . 463Explicitly Overloading a Generic Function . . . . . . . . . . . . . 463Overloading a Function Template . . . . . . . . . . . . . . . . . . . . . 466Using Standard Parameters with Template Functions . . . . 466Generic Function Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 467Applying Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468A Generic Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469Compacting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470Generic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472An Example with Two Generic Data Types . . . . . . . . . . . . . 476Applying Template Classes: A Generic Array Class . . . . . 477Using Non-Type Arguments with Generic Classes . . . . . . 479Using Default Arguments with Template Classes . . . . . . . 481Explicit Class Specializations . . . . . . . . . . . . . . . . . . . . . . . . . 483 19. xviii C + + : T h e C o m p l e t e R e f e r e n c eThe typename and export Keywords . . . . . . . . . . . . . . . . . . . . . . . . . 484The Power of Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48519 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50620 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 . . . . . . . . . . . . 53521 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. C o n t e n t s xixMore get( ) Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551getline( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551Detecting EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553The ignore( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555peek( ) and putback( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556flush( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557Obtaining the Current File Position . . . . . . . . . . . . . . . . . . . 561I/O Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561Customized I/O and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56322 Run-Time Type ID and the Casting Operators . . . . . . . 567Run-Time Type Identification (RTTI) . . . . . . . . . . . . . . . . . . . . . . . . . 568The Casting Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57823 Namespaces, Conversion Functions,and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . 591Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592Namespace Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . 592using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596Unnamed Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598Some Namespace Options . . . . . . . . . . . . . . . . . . . . . . . . . . . 599The std Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601Creating Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603const Member Functions and mutable . . . . . . . . . . . . . . . . . . . . . . . . 607Volatile Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609Explicit Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610The Member Initialization Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611Using the asm Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616Linkage Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617Array-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 . . . . . . . . . . . 625Summarizing the Differences Between C and C++ . . . . . . . . . . . . . 62624 Introducing the Standard Template Library . . . . . . . . . . 629An Overview of the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 21. xx C + + : T h e C o m p l e t e R e f e r e n c eAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Other STL Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633General Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635Accessing a Vector Through an Iterator . . . . . . . . . . . . . . . . 639Inserting and Deleting Elements in a Vector . . . . . . . . . . . . 641Storing Class Objects in a Vector . . . . . . . . . . . . . . . . . . . . . . 643Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645Understanding end( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649push_front( ) vs. push_back( ) . . . . . . . . . . . . . . . . . . . . . . . . 651Sort a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652Merging One List with Another . . . . . . . . . . . . . . . . . . . . . . 653Storing Class Objects in a List . . . . . . . . . . . . . . . . . . . . . . . . 655Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658Storing Class Objects in a Map . . . . . . . . . . . . . . . . . . . . . . . 662Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664Removing and Replacing Elements . . . . . . . . . . . . . . . . . . . 670Reversing a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Transforming a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673Using Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675Unary and Binary Function Objects . . . . . . . . . . . . . . . . . . . 675Using the Built-in Function Objects . . . . . . . . . . . . . . . . . . . 675Creating a Function Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 678Using Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680The string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683Some string Member Functions . . . . . . . . . . . . . . . . . . . . . . . 687Strings Are Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693Putting Strings into Other Containers . . . . . . . . . . . . . . . . . 694Final Thoughts on the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695Part IIIThe Standard Function Library25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . 699clearerr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700fclose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701feof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701ferror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701fflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702fgetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702fgetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702 22. C o n t e n t s xxifgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703fopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703fprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705fputc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705fputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706fread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706freopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706fscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707fseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707fsetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708ftell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708fwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709getc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709getchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710gets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710perror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711putc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714putchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715rewind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715setbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719setvbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720sscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720tmpfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720tmpnam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721ungetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721vprintf, vfprintf, and vsprintf . . . . . . . . . . . . . . . . . . . . . . . . 72226 The String and Character Functions . . . . . . . . . . . . . . . . 723isalnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724isalpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724iscntrl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725isdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725isgraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725islower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725isprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726ispunct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726isspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726isupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727 23. xxii C + + : T h e C o m p l e t e R e f e r e n c eisxdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727memchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727memcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727memcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728memmove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728memset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730strcoll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strcspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strerror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strncat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732strncmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732strncpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733strpbrk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733strrchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733strspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734strstr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734strtok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734strxfrm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735tolower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735toupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73527 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . 737acos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738asin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738atan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739atan2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740cosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740fabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741fmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741frexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741ldexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742log10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742modf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743 24. C o n t e n t s xxiiisinh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744tan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744tanh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74528 Time, Date, and Localization Functions . . . . . . . . . . . . . 747asctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749ctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749difftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750gmtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750localeconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750localtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752mktime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752setlocale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752strftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75429 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . 757calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759realloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75930 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761abort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763atexit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763atof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763atoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764atol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764bsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766getenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766ldiv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767longjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767mblen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767mbstowcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768mbtowc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 25. xxiv C + + : T h e C o m p l e t e R e f e r e n c erand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770setjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770srand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771strtod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771strtol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772strtoul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773va_arg, va_start, and va_end . . . . . . . . . . . . . . . . . . . . . . . . . 773wcstombs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774wctomb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77431 The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . . 775The Wide-Character Classification Functions . . . . . . . . . . . . . . . . . . 776The Wide-Character I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 779The Wide-Character String Functions . . . . . . . . . . . . . . . . . . . . . . . . . 779Wide-Character String Conversion Functions . . . . . . . . . . . . . . . . . . 779Wide-Character Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782Multibyte/Wide-Character Conversion Functions . . . . . . . . . . . . . . 783Part IVThe Standard C++ Class Library32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . . 787The I/O Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788The I/O Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790The Format Flags and I/O Manipulators . . . . . . . . . . . . . . . . . . . . . . 791Several Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793The streamsize and streamoff Types . . . . . . . . . . . . . . . . . . . 793The streampos and wstreampos Types . . . . . . . . . . . . . . . . 793The pos_type and off_type Types . . . . . . . . . . . . . . . . . . . . . 793The openmode Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793The iostate Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794The seekdir Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794The failure Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794Overload > Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794The General-Purpose I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . 795bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795eof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797 26. C o n t e n t s xxvflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . 811The 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 + + : T h e C o m p l e t e R e f e r e n c ecopy_backward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841count_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842equal_range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842fill and fill_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843find_end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843find_first_of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843find_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844for_each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844generate and generate_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845inplace_merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845iter_swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845lexicographical_compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846lower_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846make_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847max_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848min_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848mismatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848next_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849nth_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849partial_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849partial_sort_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850pop_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850prev_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851push_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851random_shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851remove, remove_if, remove_copy, and remove_copy_if . 852replace, replace_copy, replace_if, and replace_copy_if . . . 852reverse and reverse_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853rotate and rotate_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854search_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854set_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854set_intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855set_symmetric_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855set_union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856sort_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856 28. C o n t e n t s xxviistable_partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857stable_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857swap_ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858unique and unique_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858upper_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85935 STL Iterators, Allocators, and Function Objects . . . . . . 861Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862The Basic Iterator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862The Low-Level Iterator Classes . . . . . . . . . . . . . . . . . . . . . . . 863The Predefined Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864Two Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874Negators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875Adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87936 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881The basic_string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882The char_traits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89437 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897The complex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898The valarray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902The slice and gslice Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 917The Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920The Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920adjacent_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921inner_product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922partial_sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92338 Exception Handling and Miscellaneous Classes . . . . . . 925Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927auto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928The pair Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931Other Classes of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931 29. Part VApplying C++xxviii C + + : T h e C o m p l e t e R e f e r e n c e39 Integrating New Classes: A Custom String Class . . . . . 935The StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936The Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . 938I/O on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939The Assignment Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942Substring Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945The Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947Miscellaneous String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948The Entire StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949Using the StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958Creating and Integrating New Types in General . . . . . . . . . . . . . . . 961A Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96140 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964Parsing Expressions: The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 965Parsing an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966The Parser Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968Dissecting an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969A Simple Expression Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971Understanding the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . 977Adding Variables to the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978Syntax Checking in a Recursive-Descent Parser . . . . . . . . . . . . . . . . 988Building a Generic Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989Some Things to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997A The .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999The .NET Keyword Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000Preprocessor Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002The attribute Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003Compiling Managed C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003B C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009 30. If there is one language that defines modern programming, it is C++. Its syntax,style, and philosophy have set the standard by which all other languages arejudged. 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 + + : T h e C o m p l e t e R e f e r e n c ereleased, 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. experienced 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 areWindows 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-chargeon the Web at www.osborne.com. Downloading this code prevents you fromhaving to type in the examples.xxxi 33. xxxii C + + : T h e C o m p l e t e R e f e r e n c eFor 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++, tryC++: A Beginners GuideC++ from the Ground UpTeach Yourself C++STL Programming from the Ground UpC++ Programmers ReferenceTo learn about Java programming, we recommend the following:Java 2: A Beginners GuideJava 2: The Complete ReferenceJava 2 Programmers ReferenceTo learn about C#, Herb offers these books:C#: A Beginners GuideC#: The Complete ReferenceTo learn about Windows programming we suggest the following Schildt books:Windows 98 Programming from the Ground UpWindows 2000 Programming from the Ground UpMFC Programming from the Ground UpThe 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 ReferenceTeach Yourself CWhen you need solid answers, fast, turn to Herbert