1058

C++: The Complete Reference, 4th Editionsolutionsproj.net/software/C The Complete Reference 4th Ed.pdf · C++: The Complete Reference, Fourth Edition Herbert Schildt McGraw-Hill/Osborne

  • Upload
    others

  • View
    82

  • Download
    10

Embed Size (px)

Citation preview

  • C++:The Complete Reference,

    Fourth Edition

  • About the Author

    Herbert Schildt is the world’s leading programmingauthor. He is an authority on the C, C++, Java, and C#languages, and is a master Windows programmer. Hisprogramming books have sold more than 3 millioncopies worldwide and have been translated into allmajor foreign languages. He is the author of numerousbestsellers, including C++: The Complete Reference, C#:The Complete Reference, Java 2: The Complete Reference,C: The Complete Reference, C++ from the Ground Up,C++: A Beginner’s Guide, C#: A Beginner’s Guide, andJava 2: A Beginner’s Guide. Schildt holds a master’sdegree 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.

  • C++:The Complete Reference,

    Fourth Edition

    Herbert Schildt

    McGraw-Hill/OsborneNew York Chicago San Francisco

    Lisbon London Madrid Mexico CityMilan New Delhi San Juan

    Seoul Singapore Sydney Toronto

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

    0-07-150239-4

    The 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 the trademark. 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 training programs. For more information, please contact George Hoare, Special Sales, at [email protected] or (212) 904-4069.

    TERMS OF USE

    This 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-Hill’s prior consent. You may use thework for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may beterminated if you fail to comply with these terms.

    THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TOTHE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WAR-RANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrantor guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free.Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, inthe work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed throughthe work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, conse-quential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibil-ity of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in con-tract, tort or otherwise.

    DOI: 10.1036/0072226803

  • We hope you enjoy thisMcGraw-Hill eBook! If

    you’d like more information about this book,its author, or related books and websites,please click here.

    Professional

    Want to learn more?

  • 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

    v

  • 13 Arrays, Pointers, References, and the DynamicAllocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

    14 Function Overloading, Copy Constructors,and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 359

    15 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 . . . . . . . . . . 629

    Part III The Standard Function Library

    25 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 . . . . . . . . . . . . . . . . . . . . . 775

    Part IV The Standard C++ Class Library

    32 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

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

  • Part V Applying C++

    39 Integrating New Classes: A Custom String Class . . . . . 93540 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963A The .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999B C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005

    Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009

    C o n t e n t s a t a G l a n c e vii

  • This page intentionally left blank

  • ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

    Part I

    The Foundation of C++: The C Subset

    1 An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The Origins and History of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4C Is a Middle-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5C 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Modifying the Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Identifier Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    Where Variables Are Declared . . . . . . . . . . . . . . . . . . . . . . . . 18Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

    ix

    For more information about this title, click here

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

    Formal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    The const and volatile Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    Storage Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

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

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

    Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

    Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Type Conversion in Expressions . . . . . . . . . . . . . . . . . . . . . . 53Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Spacing and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Compound Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

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

    if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60The if-else-if Ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62The ? Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63The Conditional Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 66

  • C o n t e n t s xi

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

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

    Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

    Multiple Indirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Initializing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Pointers to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

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

    C'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 argv—Arguments to main( ) . . . . . . . . . . . . . . . . . . . . . . . . 144The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

    Returning from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 148Returning 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 FunctionParameter Declarations . . . . . . 158

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

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

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

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

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

  • C o n t e n t s xiii

    Reading and Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189A 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Suppressing Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

    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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Reading a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

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

    Using fopen( ), getc( ), putc( ), and fclose( ) . . . . . . . . . . . . . 218Using feof( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Working with Strings: fputs( ) and fgets( ) . . . . . . . . . . . . . . 222rewind( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223ferror( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Erasing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Flushing a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

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

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

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

    10 The Preprocessor and Comments . . . . . . . . . . . . . . . . . . 237The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238#define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

    Defining Function-like Macros . . . . . . . . . . . . . . . . . . . . . . . 240#error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241#include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242Conditional Compilation Directives . . . . . . . . . . . . . . . . . . . . . . . . . . 242

    #if, #else, #elif, and #endif . . . . . . . . . . . . . . . . . . . . . . . . . . . 243#ifdef and #ifndef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

    #undef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246Using defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247#line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248#pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248The # and ## Preprocessor Operators . . . . . . . . . . . . . . . . . . . . . . . . . 248Predefined Macro Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250Single-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

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

  • C o n t e n t s xv

    A Closer Look at the I/O Operators . . . . . . . . . . . . . . . . . . . 263Declaring Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264No Default to int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265The bool Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

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

    Introducing C++ Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Operator 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Friend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297Friend 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310Static Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

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

    13 Arrays, Pointers, References, and the DynamicAllocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

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

    Pointers to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329Type Checking C++ Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

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

    The this Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332Pointers to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334Pointers to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339

    Reference Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Passing References to Objects . . . . . . . . . . . . . . . . . . . . . . . . 343Returning References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Independent References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345References to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . 346Restrictions to References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347

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

    Initializing Allocated Memory . . . . . . . . . . . . . . . . . . . . . . . 349Allocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350Allocating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351The nothrow Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356The Placement Form of new . . . . . . . . . . . . . . . . . . . . . . . . . 357

    14 Function Overloading, Copy Constructors,and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 359

    Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

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

    Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366Finding the Address of an Overloaded Function . . . . . . . . . . . . . . . 370The overload Anachronism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371Default Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

    Default Arguments vs. Overloading . . . . . . . . . . . . . . . . . . 376Using Default Arguments Correctly . . . . . . . . . . . . . . . . . . . 377

    Function Overloading and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . 378

    15 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383Creating a Member Operator Function . . . . . . . . . . . . . . . . . . . . . . . 384

    Creating Prefix and Postfix Formsof the Increment and Decrement Operators . . . . . . . . . . 389

    Overloading the Shorthand Operators . . . . . . . . . . . . . . . . . 390Operator Overloading Restrictions . . . . . . . . . . . . . . . . . . . . 390

    Operator Overloading Using a Friend Function . . . . . . . . . . . . . . . . 391Using a Friend to Overload ++ or – – . . . . . . . . . . . . . . . . . . 393Friend Operator Functions Add Flexibility . . . . . . . . . . . . . 396

    Overloading new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398Overloading new and delete for Arrays . . . . . . . . . . . . . . . 403Overloading the nothrow Version of new and delete . . . . 406

  • Overloading Some Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . 407Overloading [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407Overloading ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Overloading –> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

    Overloading the Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 414

    16 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417Base-Class Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418Inheritance and protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . 420

    Protected Base-Class Inheritance . . . . . . . . . . . . . . . . . . . . . . 424Inheriting Multiple Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425Constructors, Destructors, and Inheritance . . . . . . . . . . . . . . . . . . . . 426

    When Constructors and Destructors Are Executed . . . . . . 426Passing Parameters to Base-Class Constructors . . . . . . . . . 430

    Granting Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

    17 Virtual Functions and Polymorphism . . . . . . . . . . . . . . . 443Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444

    Calling a Virtual Function Through a BaseClass Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

    The Virtual Attribute Is Inherited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448Virtual Functions Are Hierarchical . . . . . . . . . . . . . . . . . . . . . . . . . . . 450Pure Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

    Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Using Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Early vs. Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458

    18 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460

    A Function with Two Generic Types . . . . . . . . . . . . . . . . . . . 463Explicitly Overloading a Generic Function . . . . . . . . . . . . . 463Overloading a Function Template . . . . . . . . . . . . . . . . . . . . . 466Using Standard Parameters with Template Functions . . . . 466Generic Function Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 467

    Applying Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468A Generic Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469Compacting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

    Generic 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

    C o n t e n t s xvii

  • The typename and export Keywords . . . . . . . . . . . . . . . . . . . . . . . . . 484The Power of Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

    19 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487Exception Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . 488

    Catching Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494Using Multiple catch Statements . . . . . . . . . . . . . . . . . . . . . . 495

    Handling Derived-Class Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 497Exception Handling Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

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

    Understanding terminate( ) and unexpected( ) . . . . . . . . . . . . . . . . . 503Setting the Terminate and Unexpected Handlers . . . . . . . . 504

    The uncaught_exception( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . 505The exception and bad_exception Classes . . . . . . . . . . . . . . . . . . . . . 506Applying Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506

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

    C++'s Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . 512Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513

    Formatting 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 . . . . . . . . . . . . . . . . . . . 522

    Overloading > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526Creating Your Own Inserters . . . . . . . . . . . . . . . . . . . . . . . . . 526Creating Your Own Extractors . . . . . . . . . . . . . . . . . . . . . . . . 532Creating Your Own Manipulator Functions . . . . . . . . . . . . 535

    21 C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 and the File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540Opening and Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543Unformatted and Binary I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545

    Characters vs. Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545put( ) and get( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546read( ) and write( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548

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

  • C o n t e n t s xix

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

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

    22 Run-Time Type ID and the Casting Operators . . . . . . . 567Run-Time Type Identification (RTTI) . . . . . . . . . . . . . . . . . . . . . . . . . 568The Casting Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578

    23 Namespaces, Conversion Functions,and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . 591

    Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592Namespace Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . 592using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596Unnamed Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598Some Namespace Options . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

    The 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

    The Array-Based Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619Creating an Array-Based Output Stream . . . . . . . . . . . . . . . 619Using an Array as Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621Input/Output Array-Based Streams . . . . . . . . . . . . . . . . . . . 623Using Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624Using Binary I/O with Array-Based Streams . . . . . . . . . . . 625

    Summarizing the Differences Between C and C++ . . . . . . . . . . . . . 626

    24 Introducing the Standard Template Library . . . . . . . . . . 629An Overview of the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

    Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

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

    Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Other STL Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632

    The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633General Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635

    Accessing a Vector Through an Iterator . . . . . . . . . . . . . . . . 639Inserting and Deleting Elements in a Vector . . . . . . . . . . . . 641Storing Class Objects in a Vector . . . . . . . . . . . . . . . . . . . . . . 643

    Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645Understanding end( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649push_front( ) vs. push_back( ) . . . . . . . . . . . . . . . . . . . . . . . . 651Sort a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652Merging One List with Another . . . . . . . . . . . . . . . . . . . . . . 653Storing Class Objects in a List . . . . . . . . . . . . . . . . . . . . . . . . 655

    Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658Storing Class Objects in a Map . . . . . . . . . . . . . . . . . . . . . . . 662

    Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664Removing and Replacing Elements . . . . . . . . . . . . . . . . . . . 670Reversing a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Transforming a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673

    Using Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675Unary and Binary Function Objects . . . . . . . . . . . . . . . . . . . 675Using the Built-in Function Objects . . . . . . . . . . . . . . . . . . . 675Creating a Function Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 678Using Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680

    The string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683Some string Member Functions . . . . . . . . . . . . . . . . . . . . . . . 687Strings Are Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693Putting Strings into Other Containers . . . . . . . . . . . . . . . . . 694

    Final Thoughts on the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

    Part III

    The Standard Function Library

    25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . 699clearerr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700fclose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701feof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701ferror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701fflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702fgetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702fgetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702

  • C o n t e n t s xxi

    fgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . 722

    26 The String and Character Functions . . . . . . . . . . . . . . . . 723isalnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724isalpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724iscntrl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725isdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725isgraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725islower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725isprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726ispunct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726isspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726isupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727

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

    isxdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727memchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727memcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727memcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728memmove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728memset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730strcoll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strcspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strerror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731strncat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732strncmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732strncpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733strpbrk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733strrchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733strspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734strstr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734strtok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734strxfrm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735tolower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735toupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735

    27 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . 737acos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738asin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738atan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739atan2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740cosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740fabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741fmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741frexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741ldexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742log10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742modf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743

  • C o n t e n t s xxiii

    sinh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744tan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744tanh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745

    28 Time, Date, and Localization Functions . . . . . . . . . . . . . 747asctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749ctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749difftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750gmtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750localeconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750localtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752mktime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752setlocale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752strftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

    29 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . 757calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759realloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759

    30 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761abort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763atexit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763atof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763atoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764atol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764bsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766getenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766ldiv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767longjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767mblen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767mbstowcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768mbtowc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769

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

    rand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770setjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770srand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771strtod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771strtol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772strtoul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773va_arg, va_start, and va_end . . . . . . . . . . . . . . . . . . . . . . . . . 773wcstombs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774wctomb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774

    31 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 . . . . . . . . . . . . . . 783

    Part IV

    The Standard C++ Class Library

    32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . . 787The I/O Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788The I/O Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790The Format Flags and I/O Manipulators . . . . . . . . . . . . . . . . . . . . . . 791Several Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793

    The 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794

    Overload > Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794The General-Purpose I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . 795

    bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795eof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797

  • C o n t e n t s xxv

    flush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809

    33 The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . . 811The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812

    bitset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814deque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822multimap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824multiset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829priority_queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834

    34 The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839adjacent_find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840binary_search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

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

    copy_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

  • C o n t e n t s xxvii

    stable_partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857stable_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857swap_ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858unique and unique_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858upper_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859

    35 STL Iterators, Allocators, and Function Objects . . . . . . 861Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862

    The Basic Iterator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862The Low-Level Iterator Classes . . . . . . . . . . . . . . . . . . . . . . . 863The Predefined Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864Two Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872

    Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874Negators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875Adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876

    Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879

    36 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881The basic_string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882The char_traits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894

    37 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897The complex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898The valarray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902

    The slice and gslice Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 917The Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920

    The Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920adjacent_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921inner_product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922partial_sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923

    38 Exception Handling and Miscellaneous Classes . . . . . . 925Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927

    auto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928The pair Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931Other Classes of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931

  • Part V

    Applying C++

    39 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961

    40 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964Parsing Expressions: The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 965Parsing an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966The Parser Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968Dissecting an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969A Simple Expression Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971

    Understanding the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . 977Adding Variables to the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978Syntax Checking in a Recursive-Descent Parser . . . . . . . . . . . . . . . . 988Building a Generic Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989Some Things to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997

    A The .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999The .NET Keyword Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000Preprocessor Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002The attribute Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003Compiling Managed C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003

    B C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005

    Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009

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

  • 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 Microsoft’s Visual C++ and Borland’s 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 was

    xxixCopyright © 2003 by The McGraw-Hill Companies. Click here for terms of use.

  • released, 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.

    What’s 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 Microsoft’s Visual Studio .NET and Borland’s C++ Builder.

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