1041

C++ complete reference

Embed Size (px)

DESCRIPTION

By Herbert Schildt

Citation preview

  • C++:The Complete Reference

    Third Edition

  • About the AuthorHerb Schildt is the leading authority on C andC++ and a best-selling author whose bookshave sold more than 1.5 million copies. Hisacclaimed C and C++books include TeachYourself C, C++ from the Ground Up, TeachYourself C++, C++: The Complete Reference,Borland C++: The Complete Reference, and C++Programmer's Reference to name a few.

  • C++:The Complete Reference

    Third Edition

    Herbert Schildt

    OsborneMcGraw-HillBerkeley New York St. Louis San Francisco

    Auckland Bogot Hamburg London MadridMexico City Milan Montreal New Delhi Panama City

    Paris So Paulo Singapore SydneyTokyo Toronto

  • Copyright 1998 by McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permittedunder the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means,or stored in a database or retrieval system, without the prior written permission of the publisher.

    0-07-213293-0

    The material in this eBook also appears in the print version of this title: 0-07-882476-1

    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 corporate train-ing 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 thework. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieveone copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon,transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hills prior consent. You mayuse the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the workmay be terminated if you fail to comply with these terms.

    THE WORK IS PROVIDED AS IS. McGRAW-HILLAND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES ASTO THE 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 IMPLIEDWARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do notwarrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted orerror free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardlessof cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any informationaccessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, spe-cial, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has beenadvised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claimor cause arises in contract, tort or otherwise.

    DOI: 10.1036/0072132930

    abc McGraw-Hill

  • Contents at a GlancePart I The Foundation of C++: The C Subset

    1 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

    10 The Preprocessor and Comments . . . . . . . . . . . . . . . . . . . 237

    Part II C++

    11 An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25512 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28913 Arrays, Pointers, References and the Dynamic

    Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 32714 Function Overloading, Copy Constructors,

    and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . 36115 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

    v

  • 16 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41917 Virtual Functions and Polymorphism . . . . . . . . . . . . . . 44518 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46119 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48920 C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . 51121 C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54122 Run-Time Type ID and the Casting Operators . . . . . . . . 56923 Namespaces, Conversion Functions,and Other

    Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59324 Introducing the Standard Template Library . . . . . . . . . . 625

    Part III The Standard Function Library

    25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . 69526 The String and Character Functions . . . . . . . . . . . . . . . . . 71927 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . 73328 Time, Date, and Localization Functions . . . . . . . . . . . . . . 74329 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . . 75330 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75731 The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . . 771

    Part IV The Standard C++ Class Library

    32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . . . 78333 The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . 80734 The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83535 STL Iterators, Allocators, and Function Objects . . . . . . . 85736 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87737 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89338 Exception Handling and Miscellaneous Classes . . . . . . . 921

    Part V Applying C++

    39 Integrating New Classes: A Custom String Class . . . . . . 93140 An Object-Oriented Expression Parser . . . . . . . . . . . . . . . 959

    Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995

    vi C + + : T h e C o m p l e t e R e f e r e n c e

  • Contents

    Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

    Part I

    The Foundation of C++: The C Subset

    1 An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The Origins of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4C Is a Middle-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4C Is a Structured Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6C Is a Programmer's Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8The Form of a C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The Library and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Separate Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Understanding the .C and .CPP File Extensions . . . . . . . . . . . . . . . . 12

    2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13The Five Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    vii

  • Modifying the Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Identifier Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    Where Variables Are Declared . . . . . . . . . . . . . . . . . . . . . . . 17Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Formal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    Access Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    Storage Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

    Variable Initializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    Hexadecimal and Octal Constants . . . . . . . . . . . . . . . . . . . . 32String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Backslash Character Constants . . . . . . . . . . . . . . . . . . . . . . . 33

    Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Type Conversion in Assignments . . . . . . . . . . . . . . . . . . . . . 35Multiple Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . 39Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47The & and * Pointer Operators . . . . . . . . . . . . . . . . . . . . . . . 47The Compile-Time Operator sizeof . . . . . . . . . . . . . . . . . . . 49The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50The Dot (.) and Arrow (>) Operators . . . . . . . . . . . . . . . . . 51The [ ] and ( ) Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Precedence Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

    Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Type Conversion in Expressions . . . . . . . . . . . . . . . . . . . . . . 53Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Spacing and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

    Shorthand Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

    3 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57True and False in C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

    viii C + + : T h e C o m p l e t e R e f e r e n c e

  • Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60The if-else-if Ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62The ? Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63The Conditional Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 66switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Nested switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

    Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70for Loop Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72The Infinite Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76for Loops with No Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . 77The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

    Declaring Variables within Selection and Iteration Statements . . . 81Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

    The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83The exit( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

    Expression Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Block Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

    4 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

    Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Indexing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

    Unsized Array Initializations . . . . . . . . . . . . . . . . . . . . . . . . . 106A Tic-Tac-Toe Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

    5 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113What Are Pointers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Pointer Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115The Pointer Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Pointer Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

    C o n t e n t s ix

  • Pointer Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Pointer Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

    Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

    Multiple Indirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Initializing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Pointers to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126C's Dynamic Allocation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Problems with Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

    6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137The General Form of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Scope Rules of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

    Call by Value, Call by Reference . . . . . . . . . . . . . . . . . . . . . . 139Creating a Call by Reference . . . . . . . . . . . . . . . . . . . . . . . . . 140Calling Functions with Arrays . . . . . . . . . . . . . . . . . . . . . . . 142

    argc and argvArguments to main( ) . . . . . . . . . . . . . . . . . . . . . . . . 144The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

    Returning from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 147Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149Returning Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Functions of Type void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152What Does main( ) Return? . . . . . . . . . . . . . . . . . . . . . . . . . . 153

    Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

    Standard Library Function Prototypes . . . . . . . . . . . . . . . . . 157Declaring Variable-Length Parameter Lists . . . . . . . . . . . . . . . . . . . . 158Old-Style Versus Modern Function Parameter Declarations . . . . . 158Implementation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

    Parameters and General-Purpose Functions . . . . . . . . . . . . 159Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

    7 Structures, Unions, Enumerations, and User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

    Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Accessing Structure Members . . . . . . . . . . . . . . . . . . . . . . . . 165Structure Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

    Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Passing Structures to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

    Passing Structure Members to Functions . . . . . . . . . . . . . . . 167Passing Entire Structures to Functions . . . . . . . . . . . . . . . . . 167

    x C + + : T h e C o m p l e t e R e f e r e n c e

  • Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Declaring a Structure Pointer . . . . . . . . . . . . . . . . . . . . . . . . . 170Using Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

    Arrays and Structures Within Structures . . . . . . . . . . . . . . . . . . . . . . 173Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180Using sizeof to Ensure Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

    8 C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187An Important Application Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188Reading and Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

    A Problem with getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Alternatives to getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

    Reading and Writing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Formatted Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195printf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

    Printing 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

    scanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

    Suppressing Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

    C o n t e n t s xi

  • 9 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211C Versus C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Streams and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

    Text Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Binary Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

    Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213File System Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

    The File Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Writing a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218Reading a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218Using fopen( ), getc( ), putc( ), and fclose( ) . . . . . . . . . . . . . 218Using feof( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Working with Strings: fputs( ) and fgets( ) . . . . . . . . . . . . . 222rewind( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223ferror( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Erasing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Flushing a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

    fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Using fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

    fseek( ) and Random-Access I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229fprintf( ) and fscanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231The Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

    The Console I/O Connection . . . . . . . . . . . . . . . . . . . . . . . . . 234Using freopen( ) to Redirect the Standard Streams . . . . . . . . . . . . . 235

    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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

    xii C + + : T h e C o m p l e t e R e f e r e n c e

  • C-Style Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

    Part II

    C++

    11 An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255The Origins of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256What Is Object-Oriented Programming? . . . . . . . . . . . . . . . . . . . . . . 257

    Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

    Some C++ Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259A Sample C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260A Closer Look at the I/O Operators . . . . . . . . . . . . . . . . . . . 263Declaring Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264No Default to int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265The bool Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

    Old-Style vs. Modern C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266The New C++ Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Working with an Old Compiler . . . . . . . . . . . . . . . . . . . . . . 270

    Introducing C++ Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283The C++ Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287The General Form of a C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . 288

    12 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Structures and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . 293Unions and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

    Anonymous Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297Friend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Friend Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

    Defining Inline Functions Within a Class . . . . . . . . . . . . . . 306Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

    Constructors with One Parameter: A Special Case . . . . . . 309Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

    Static Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

    C o n t e n t s xiii

  • Static Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315When Constructors and Destructors Are Executed . . . . . . . . . . . . . 317The Scope Resolution Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320Local Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320Passing Objects to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323Object Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

    13 Arrays, Pointers, References, and the Dynamic AllocationOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

    Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328Creating Initialized vs. Uninitialized Arrays . . . . . . . . . . . 330

    Pointers to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331Type Checking C++ Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333The this Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334Pointers to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336Pointers to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

    Reference Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341Passing References to Objects . . . . . . . . . . . . . . . . . . . . . . . . 345Returning References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346Independent References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347References to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . 348Restrictions to References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

    A Matter of Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349C++'s Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . 349

    Initializing Allocated Memory . . . . . . . . . . . . . . . . . . . . . . . 351Allocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352Allocating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353The nothrow Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

    The Placement Forms of new and delete . . . . . . . . . . . . . . . . . . . . . . 359

    14 Function Overloading, Copy Constructors, and DefaultArguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361

    Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362Overloading Constructor Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 364

    Overloading a Constructor to Gain Flexibility . . . . . . . . . . 364Allowing Both Initialized and Uninitialized Objects . . . . . 366

    Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368Finding the Address of an Overloaded Function . . . . . . . . . . . . . . . 372The overload Anachronism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

    xiv C + + : T h e C o m p l e t e R e f e r e n c e

  • Default Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374Default Arguments vs. Overloading . . . . . . . . . . . . . . . . . . 378Using Default Arguments Correctly . . . . . . . . . . . . . . . . . . . 380

    Function Overloading and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . 380

    15 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385Creating a Member Operator Function . . . . . . . . . . . . . . . . . . . . . . . 386

    Creating Prefix and Postfix Forms of theIncrement and Decrement Operators . . . . . . . . . . . . . . . 391

    Overloading the Shorthand Operators . . . . . . . . . . . . . . . . . 392Operator Overloading Restrictions . . . . . . . . . . . . . . . . . . . . 392

    Operator Overloading Using a Friend Function . . . . . . . . . . . . . . . . 393Using a Friend to Overload ++ or . . . . . . . . . . . . . . . . . . 395Friend Operator Functions Add Flexibility . . . . . . . . . . . . . 398

    Overloading new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400Overloading new and delete for Arrays . . . . . . . . . . . . . . . 405Overloading the nothrow Version of new and delete . . . . 408

    Overloading Some Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . 409Overloading [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409Overloading ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

    Overloading the Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 416

    16 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419Base-Class Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420Inheritance and protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . 422

    Protected Base-Class Inheritance . . . . . . . . . . . . . . . . . . . . . 426Inheriting Multiple Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427Constructors, Destructors, and Inheritance . . . . . . . . . . . . . . . . . . . . 428

    When Constructor and DestructorFunctions Are Executed . . . . . . . . . . . . . . . . . . . . . . . . . . . 428

    Passing Parameters to Base-Class Constructors . . . . . . . . . 432Granting Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439

    17 Virtual Functions and Polymorphism . . . . . . . . . . . . . . . 445Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446

    Calling a Virtual Function Through a Base ClassReference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

    The Virtual Attribute Is Inherited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450Virtual Functions Are Hierarchical . . . . . . . . . . . . . . . . . . . . . . . . . . . 452Pure Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455

    Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457

    C o n t e n t s xv

  • Using Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457Early vs. Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460

    18 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462

    A Function with Two Generic Types . . . . . . . . . . . . . . . . . . 465Explicitly Overloading a Generic Function . . . . . . . . . . . . . 465Overloading a Function Template . . . . . . . . . . . . . . . . . . . . 468Using Standard Parameters with Template Functions . . . 468Generic Function Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 469

    Applying Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470A Generic Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471Compacting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472

    Generic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474An Example with Two Generic Data Types . . . . . . . . . . . . 478Applying Template Classes: A Generic Array Class . . . . . 479Using Non-Type Arguments with Generic Classes . . . . . . 481Using Default Arguments with Template Classes . . . . . . . 483Explicit Class Specializations . . . . . . . . . . . . . . . . . . . . . . . . . 485

    The typename and export Keywords . . . . . . . . . . . . . . . . . . . . . . . . . 486The Power of Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487

    19 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489Exception Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . 490

    Catching Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496Using Multiple catch Statements . . . . . . . . . . . . . . . . . . . . . . 497

    Handling Derived-Class Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 499Exception Handling Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500

    Catching All Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500Restricting Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502Rethrowing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504

    Understanding terminate( ) and unexpected( ) . . . . . . . . . . . . . . . . . 505Setting the Terminate and Unexpected Handlers . . . . . . . . 506

    The uncaught_exception( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . 507The exception and bad_exception Classes . . . . . . . . . . . . . . . . . . . . . 508Applying Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

    20 The C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . . 511Old vs. Modern C++ I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512C++ Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513The C++ Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513

    C++'s Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . 514Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

    xvi C + + : T h e C o m p l e t e R e f e r e n c e

  • Formatting Using the ios Members . . . . . . . . . . . . . . . . . . . . 515Setting the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516Clearing Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517An Overloaded Form of setf( ) . . . . . . . . . . . . . . . . . . . . . . . . 518Examining the Formatting Flags . . . . . . . . . . . . . . . . . . . . . . 520Setting All Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521Using width( ), precision( ), and fill( ) . . . . . . . . . . . . . . . . . 522Using Manipulators to Format I/O . . . . . . . . . . . . . . . . . . . 524

    Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528Creating Your Own Inserters . . . . . . . . . . . . . . . . . . . . . . . . . 528Creating Your Own Extractors . . . . . . . . . . . . . . . . . . . . . . . 534

    Creating Your Own Manipulator Functions . . . . . . . . . . . . . . . . . . . 537

    21 C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 and the File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542Opening and Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545Unformatted and Binary I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547

    Characters vs. Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547put( ) and get( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548read( ) and write( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550

    More get( ) Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553getline( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553Detecting EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555The ignore( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557peek( ) and putback( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558flush( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559

    Obtaining the Current File Position . . . . . . . . . . . . . . . . . . . 563I/O Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563Customized I/O and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565

    22 Run-Time Type ID and the Casting Operators . . . . . . . 569Run-Time Type Identification (RTTI) . . . . . . . . . . . . . . . . . . . . . . . . . 570

    A Simple Application of Run-Time Type ID . . . . . . . . . . . . 576typeid Can Be Applied to Template Classes . . . . . . . . . . . . 578

    The Casting Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580

    Replacing typeid with dynamic_cast . . . . . . . . . . . . . . . . . . 584Using dynamic_cast with Template Classes . . . . . . . . . . . . 586const_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588static_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590

    reinterpret_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590

    C o n t e n t s xvii

  • 23 Namespaces, Conversion Functions,and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . 593

    Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594Namespace Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . 594using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598Unnamed Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600Some Namespace Options . . . . . . . . . . . . . . . . . . . . . . . . . . . 601

    The std Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603Creating Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605const Member Functions and mutable . . . . . . . . . . . . . . . . . . . . . . . . 609Volatile Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611Explicit Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612Using the asm Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613Linkage Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614Array-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615

    The Array-Based Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616Creating an Array-Based Output Stream . . . . . . . . . . . . . . . 616Using an Array as Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618Input/Output Array-Based Streams . . . . . . . . . . . . . . . . . . 620

    Using Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621Using Binary I/O with Array-Based Streams . . . . . . . . . . . . . . . . . . 622Summarizing the Differences

    Between C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623

    24 Introducing the Standard Template Library . . . . . . . . . 625An Overview of the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626

    Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627Other STL Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628

    The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629General Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

    Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Accessing a Vector Through an Iterator . . . . . . . . . . . . . . . . 635Inserting and Deleting Elements in a Vector . . . . . . . . . . . . 637Storing Class Objects in a Vector . . . . . . . . . . . . . . . . . . . . . . 639

    Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641Understanding end( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645push_front( ) vs push_back( ) . . . . . . . . . . . . . . . . . . . . . . . . 647Sort a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648Merging One List with Another . . . . . . . . . . . . . . . . . . . . . . 649Storing Class Objects in a List . . . . . . . . . . . . . . . . . . . . . . . . 651

    Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654

    xviii C + + : T h e C o m p l e t e R e f e r e n c e

  • Storing Class Objects In a Map . . . . . . . . . . . . . . . . . . . . . . . 658Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660

    Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660Removing and Replacing Elements . . . . . . . . . . . . . . . . . . . 666Reversing a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668Transforming a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669

    Using Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671Unary and Binary Function Objects . . . . . . . . . . . . . . . . . . . 671Using the Built-in Function Objects . . . . . . . . . . . . . . . . . . . 671Creating a Function Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 674Using Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676

    The string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679Some string Member Functions . . . . . . . . . . . . . . . . . . . . . . . 683Strings Are Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689Putting Strings into Other Containers . . . . . . . . . . . . . . . . . 690

    Final Thoughts on the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691

    Part III

    The Standard Function Library

    25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . 695clearerr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696fclose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697feof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697ferror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697fflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698fgetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698fgetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698fgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699fopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699fprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701fputc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701fputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702fread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702freopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702fscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703fseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703fsetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704ftell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704fwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705getc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705getchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706gets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706

    C o n t e n t s xix

  • perror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707putc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710putchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711rewind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711setbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715setvbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716sscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716tmpfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716tmpnam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717ungetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717vprintf, vfprintf, and vsprintf . . . . . . . . . . . . . . . . . . . . . . . . 718

    26 The String and Character Functions . . . . . . . . . . . . . . . . 719isalnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720isalpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720iscntrl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721isdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721isgraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721islower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721isprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722ispunct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722isspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722isupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723isxdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723memchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723memcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723memcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724memmove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724memset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725strchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726strcoll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727strcspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727strerror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727

    xx C + + : T h e C o m p l e t e R e f e r e n c e

  • strncat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728strncmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728strncpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strpbrk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strrchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730strstr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730strtok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730strxfrm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731tolower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731toupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731

    27 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . 733acos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734asin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734atan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735atan2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736cosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736fabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737fmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737frexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737ldexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738log10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738modf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739sinh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740tan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740tanh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741

    28 Time, Date, and Localization Functions . . . . . . . . . . . . . 743asctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745ctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745difftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746gmtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746localeconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746

    C o n t e n t s xxi

  • localtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748mktime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748setlocale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748strftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750

    29 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . 753calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755realloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755

    30 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757abort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759atexit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759atof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759atoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760atol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760bsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762getenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762ldiv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763longjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763mblen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763mbstowcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764mbtowc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765rand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766setjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766srand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767strtod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767strtol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768strtoul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769va_arg, va_start, and va_end . . . . . . . . . . . . . . . . . . . . . . . . . 769wcstombs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770wctomb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

    xxii C + + : T h e C o m p l e t e R e f e r e n c e

  • 31 The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . . 771The Wide-Character Classification Functions . . . . . . . . . . . . . . . . . . 772The Wide-Character I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 775The Wide-Character String Functions . . . . . . . . . . . . . . . . . . . . . . . . 775Wide-Character String Conversion Functions . . . . . . . . . . . . . . . . . . 775Wide-Character Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778Multibyte/Wide-Character Conversion Functions . . . . . . . . . . . . . 779

    Part IV

    The Standard C++ Class Library

    32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . . 783The I/O Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784The I/O Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786The Format Flags and I/O Manipulators . . . . . . . . . . . . . . . . . . . . . . 787Several Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789

    The streamsize and streamoff Types . . . . . . . . . . . . . . . . . . 789The streampos and wstreampos Types . . . . . . . . . . . . . . . . 789The pos_type and off_type Types . . . . . . . . . . . . . . . . . . . . . 789The openmode Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789The iostate Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790The seekdir type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790The failure Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790

    Overload > Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790The General-Purpose I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . 791

    bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791eof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793flush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793fstream, ifstream, and ofstream . . . . . . . . . . . . . . . . . . . . . . . 793gcount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794getline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796ignore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796peek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798

    C o n t e n t s xxiii

  • put . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798putback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798rdstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799readsome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799seekg and seekp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800setf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801setstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802stringstream, istringstream, ostringstream . . . . . . . . . . . . . 802sync_with_stdio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803tellg and tellp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804unsetf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805

    33 The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . . 807The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808

    bitset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810deque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818multimap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820multiset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825priority_queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830

    34 The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835adjacent_find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836binary_search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837copy_backward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837count_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838equal_range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838fill and fill_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839find_end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839find_first_of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

    xxiv C + + : T h e C o m p l e t e R e f e r e n c e

  • find_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840for_each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840generate and generate_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841inplace_merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841iter_swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841lexicographical_compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842lower_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842make_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843max_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844min_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844mismatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844next_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845nth_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845partial_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845partial_sort_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846pop_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846prev_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847push_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847random_shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847remove, remove_if, remove_copy, and remove_copy_if . . 848replace, replace_copy, replace_if, and replace_copy_if . . . 848reverse and reverse_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849rotate and rotate_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850search_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850set_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850set_intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851set_symmetric_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851set_union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852sort_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852stable_partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853stable_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853swap_ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854unique and unique_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854upper_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855

    C o n t e n t s xxv

  • 35 STL Iterators, Allocators, and Function Objects . . . . . . 857Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858

    The Basic Iterator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858The Low-Level Iterator Classes . . . . . . . . . . . . . . . . . . . . . . . 859The Predefined Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860Two Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868

    Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870Negators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871Adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875

    36 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877The basic_string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 878The char_traits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890

    37 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893The complex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894The valarray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898

    The slice and gslice Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 913The Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 916

    The Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 916accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 916adjacent_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917inner_product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918partial_sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919

    38 Exception Handling and Miscellaneous Classes . . . . . . 921Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923

    auto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924The pair Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927Other Classes of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927

    Part V

    Applying C++

    39 Integrating New Classes: A Custom String Class . . . . . 931The StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932

    xxvi C + + : T h e C o m p l e t e R e f e r e n c e

  • The Constructor and Destructor Functions . . . . . . . . . . . . . . . . . . . . 934I/O on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935The Assignment Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938Substring Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941The Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943Miscellaneous String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944The Entire StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945Using the StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954Creating and Integrating New Types in General . . . . . . . . . . . . . . . 957A Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957

    40 An Object-Oriented Expression Parser . . . . . . . . . . . . . . 959Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960Parsing Expressions: The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 961Parsing an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962The Parser Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964Dissecting an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965A Simple Expression Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967

    Understanding the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . 973Adding Variables to the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974Syntax Checking in a Recursive-Descent Parser . . . . . . . . . . . . . . . . 984Building a Generic Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985Some Things to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993

    Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995

    C o n t e n t s xxvii

  • This page intentionally left blank.

  • Preface

    This is the third edition of C++: The Complete Reference. In the years that have transpiredsince the second edition, C++ has undergone many changes. Perhaps the most importantis that it is now a standardized language. In November of 1997, the ANSI/ISOcommittee charged with the task of standardizing C++, passed out of committee anInternational Standard for C++. This event marked the end of a very long, and attimes contentious, process. As a member of the ANSI/ISO C++ committee, I watchedthe progress of the emerging standard, following each debate and argument. Near theend, there was a world-wide, daily dialogue, conducted via e-mail, in which the prosand cons of this or that issue were put forth, and finally resolved. While the processwas longer and more exhausting than anyone at first envisioned, the result was worththe trouble. We now have a standard for what is, without question, the most importantprogramming language in the world.

    During standardization, several new features were added to C++. Some arerelatively small. Others, like the STL (Standard Template Library) have ramificationsthat will affect the course of programming for years to come. The net effect of theadditions was that the scope and range of the language were greatly expanded. Forexample, because of the addition of the numerics library, C++ can be more convenientlyused for numeric processing. Of course, the information contained in this edition

    xxix

  • reflects the International Standard for C++ defined by the ANSI/ISO committee,including its new features.

    What's New in the Third EditionThe third edition of C++: The Complete Reference is greatly expanded beyond itspredecessor. In fact, the length of the book has nearly doubled! The main reason forthis is that the third edition now includes comprehensive coverage of both the standardfunction library and the standard class library. Neither of these were sufficiently welldefined when the second edition was being prepared to warrant inclusion. With thestandardization of C++ being complete, these topics can finally be added.

    Aside from these major additions, the third edition also includes a substantialamount of new material scattered throughout the book. Most is the result of featuresthat have been added to C++ since the previous edition was prepared. New orexpanded coverage includes the following topics: the Standard Template Library,run-time type ID (RTTI), the new casting operators, new features of templates,namespaces, new-style headers, and the modern-style I/O system. Also, somefundamental changes to the way new and delete are implemented are described andseveral new keywords are discussed.

    Frankly, if you have not taken a close look at C++ for the past few years, you willbe surprised at how much it has grown and how many new features have been added.It's not the same old C++ that you learned years ago.

    What's 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 Subset The foundation of C++ The C++ language The Standard Function Library The Standard Class Library Sample C++ applications

    Part 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

    xxx C + + : T h e C o m p l e t e R e f e r e n c e

  • experienced C programmer can simply turn to the sections of this book that cover theC++-specific features.

    Part Two discusses in detail the extensions and enhancements to C added by C++.These include its object-oriented features such as classes, constructors, destructors, andtemplates. Thus, Part Two covers those constructs that "make C++, C++."

    Part Three describes the standard function library and Part Four examines thestandard class library, including the STL (Standard Template Library). Part Five showstwo 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 coverage of the many new features added by the International Standardespecially useful.

    If You're Using WindowsIf your computer uses Windows, then you have chosen the right language. C++ iscompletely at home with Windows programming. However, none of the programsin this book are Windows programs. Instead, they are console-based programs. Thereason for this is easy to understand: Windows programs are, by their nature, large andcomplex. The overhead required to create even a minimal Windows skeletal programis 50 to 70 lines of code. To write Windows programs that demonstrate the features ofC++ would require hundreds of lines of code each. Put simply, Windows is not anappropriate environment in which to discuss the features of a programming language.However, you can still use a Windows-based com