C++ Primer, Fourth Edition By Stanley B Lippman, Josée Lajoie, Barbara E Moo Publisher: Addison Wesley Professional Pub Date: February 14, 2005 Print ISBN: 0-201-72148-1 Pages: 912 Table of Contents | Index This popular tutorial introduction to standard C++ has been completely updated, reorganized, and rewritten to help programmers learn the language faster and use it in a more modern, effective way.Just as C++ has evolved since the last edition, so has the authors' approach to teaching it They now introduce the C++ standard library from the beginning, giving readers the means to write useful programs without first having to master every language detail Highlighting today's best practices, they show how to write programs that are safe, can be built quickly, and yet offer outstanding performance Examples that take advantage of the library, and explain the features of C++, also show how to make the best use of the language As in its previous editions, the book's authoritative discussion of fundamental C++ concepts and techniques makes it a valuable resource even for more experienced programmers.Program Faster and More Effectively with This Rewritten Classic Restructured for quicker learning, using the C++ standard library Updated to teach the most current programming styles and program design techniques Filled with new learning aids that emphasize important points, warn about common pitfalls, suggest good programming practices, and provide general usage tips Complete with exercises that reinforce skills learned Authoritative and comprehensive in its coverageThe source code for the book's extended examples is available on the Web at the address below C++ Primer, Fourth Edition By Stanley B Lippman, Josée Lajoie, Barbara E Moo Publisher: Addison Wesley Professional Pub Date: February 14, 2005 Print ISBN: 0-201-72148-1 Pages: 912 Table of Contents | Index Copyright Preface Changes to the Fourth Edition Structure of This Book Acknowledgments Chapter 1 Getting Started Section 1.1 Writing a Simple C++ Program Section 1.2 A First Look at Input/Output Section 1.3 A Word About Comments Section 1.4 Control Structures Section 1.5 Introducing Classes Section 1.6 The C++ Program Chapter Summary Keyterm Defined Terms Part I: The Basics Chapter 2 Variables and Basic Types Section 2.1 Primitive Built-in Types Section 2.2 Literal Constants Section 2.3 Variables Section 2.4 const Qualifier Section 2.5 References Section 2.6 Typedef Names Section 2.7 Enumerations Section 2.8 Class Types Section 2.9 Writing Our Own Header Files Chapter Summary Keyterm Defined Terms Chapter 3 Library Types Section 3.1 Namespace using Declarations Section 3.2 Library string Type Section 3.3 Library vector Type Section 3.4 Introducing Iterators Section 3.5 Library bitset Type Chapter Summary Keyterm Defined Terms Chapter 4 Arrays and Pointers Section 4.1 Arrays Section 4.2 Introducing Pointers Section 4.3 C-Style Character Strings Section 4.4 Multidimensioned Arrays Chapter Summary Keyterm Defined Terms Chapter 5 Expressions Section 5.1 Arithmetic Operators Section 5.2 Relational and Logical Operators Section 5.3 The Bitwise Operators Section 5.4 Assignment Operators Section 5.5 Increment and Decrement Operators Section 5.6 The Arrow Operator Section 5.7 The Conditional Operator Section 5.8 The sizeof Operator Section 5.9 Comma Operator Section 5.10 Evaluating Compound Expressions Section 5.11 The new and delete Expressions Section 5.12 Type Conversions Chapter Summary Keyterm Defined Terms Chapter 6 Statements Section 6.1 Simple Statements Section 6.2 Declaration Statements Section 6.3 Compound Statements (Blocks) Section 6.4 Statement Scope Section 6.5 The if Statement Section 6.6 The switch Statement Section 6.7 The while Statement Section 6.8 The for Loop Statement Section 6.9 The do while Statement Section 6.10 The break Statement Section 6.11 The continue Statement Section 6.12 The goto Statement Section 6.13 try Blocks and Exception Handling Section 6.14 Using the Preprocessor for Debugging Chapter Summary Keyterm Defined Terms Chapter 7 Functions Section 7.1 Defining a Function Section 7.2 Argument Passing Section 7.3 The return Statement Section 7.4 Function Declarations Section 7.5 Local Objects Section 7.6 Inline Functions Section 7.7 Class Member Functions Section 7.8 Overloaded Functions Section 7.9 Pointers to Functions Chapter Summary Keyterm Defined Terms Chapter 8 The IO Library Section 8.1 An Object-Oriented Library Section 8.2 Condition States Section 8.3 Managing the Output Buffer Section 8.4 File Input and Output Section 8.5 String Streams Chapter Summary Keyterm Defined Terms Part II: Containers and Algorithms Chapter 9 Sequential Containers Section 9.1 Defining a Sequential Container Section 9.2 Iterators and Iterator Ranges Section 9.3 Sequence Container Operations Section 9.4 How a vector Grows Section 9.5 Deciding Which Container to Use Section 9.6 strings Revisited Section 9.7 Container Adaptors Chapter Summary Keyterm Defined Terms Chapter 10 Associative Containers Section 10.1 Preliminaries: the pair Type Section 10.2 Associative Containers Section 10.3 The map Type Section 10.4 The set Type Section 10.5 The multimap and multiset Types Section 10.6 Using Containers: Text-Query Program Chapter Summary Keyterm Defined Terms Chapter 11 Generic Algorithms Section 11.1 Overview Section 11.2 A First Look at the Algorithms Section 11.3 Revisiting Iterators Section 11.4 Structure of Generic Algorithms Section 11.5 Container-Specific Algorithms Chapter Summary Keyterm Defined Terms Part III: Classes and Data Abstraction Chapter 12 Classes Section 12.1 Class Definitions and Declarations Section 12.2 The Implicit this Pointer Section 12.3 Class Scope Section 12.4 Constructors Section 12.5 Friends Section 12.6 static Class Members Chapter Summary Keyterm Defined Terms Chapter 13 Copy Control Section 13.1 The Copy Constructor Section 13.2 The Assignment Operator Section 13.3 The Destructor Section 13.4 A Message-Handling Example Section 13.5 Managing Pointer Members Chapter Summary Keyterm Defined Terms Chapter 14 Overloaded Operations and Conversions Section 14.1 Defining an Overloaded Operator Section 14.2 Input and Output Operators Section 14.3 Arithmetic and Relational Operators Section 14.4 Assignment Operators Section 14.5 Subscript Operator Section 14.6 Member Access Operators Section 14.7 Increment and Decrement Operators Section 14.8 Call Operator and Function Objects Section 14.9 Conversions and Class Types Chapter Summary Keyterm Defined Terms Part IV: Object-Oriented and Generic Programming Chapter 15 Object-Oriented Programming Section 15.1 OOP: An Overview Section 15.2 Defining Base and Derived Classes Section 15.3 Conversions and Inheritance Section 15.4 Constructors and Copy Control Section 15.5 Class Scope under Inheritance Section 15.6 Pure Virtual Functions Section 15.7 Containers and Inheritance Section 15.8 Handle Classes and Inheritance Section 15.9 Text Queries Revisited Chapter Summary Keyterm Defined Terms Chapter 16 Templates and Generic Programming Section 16.1 Template Definitions Section 16.2 Instantiation Section 16.3 Template Compilation Models Section 16.4 Class Template Members Section 16.5 A Generic Handle Class Section 16.6 Template Specializations Section 16.7 Overloading and Function Templates Chapter Summary Keyterm Defined Terms Part V: Advanced Topics Chapter 17 Tools for Large Programs Section 17.1 Exception Handling Section 17.2 Namespaces Section 17.3 Multiple and Virtual Inheritance Chapter Summary Keyterm Defined Terms Chapter 18 Specialized Tools and Techniques Section 18.1 Optimizing Memory Allocation Section 18.2 Run-Time Type Identification Section 18.3 Pointer to Class Member Section 18.4 Nested Classes Section 18.5 Union: A Space-Saving Class Section 18.6 Local Classes Section 18.7 Inherently Nonportable Features Chapter Summary Keyterm Defined Terms Appendix A The Library Section A.1 Library Names and Headers Section A.2 A Brief Tour of the Algorithms Section A.3 The IO Library Revisited Index Copyright Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests For more information, please contact: U S Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the U S., please contact: International Sales international@pearsoned.com Visit us on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Lippman, Stanley B C++ primer / Stanley B Lippman, Josée Lajoie, Barbara E Moo p cm Includes index ISBN 0-201-72148-1 (pbk : alk paper) C++ (Computer program language) I Lajoie, Josée II Moo, QA76.73.C153L57697 2005 005.13'3dc22 Copyright © 2005 Objectwrite Inc., Josée Lajoie and Barbara E Moo All rights reserved Printed in the United States of America This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise For information regarding permissions, write to: Pearson Education, Inc Rights and Contracts Department One Lake Street Upper Saddle River, NJ 07458 Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts First printing, February 2005 Dedication To Beth, who makes this, and all things, possible To Daniel and Anna, who contain virtually all possiblities SBL To Mark and Mom, for their unconditional love and support JL To Andy, who taught me to program and so much more BEM terminate 2nd 3rd 4th 5th TextQuery class definition main program using program design program interface revisited this pointer implicit 2nd implicit parameter 2nd in overloaded operator overloaded operator static member functions three, rule of 2nd tHRow 2nd 3rd 4th 5th example 2nd execution flow 2nd pointer to local object rethrow tolower top priority_queue stack toupper TRansform transformation program, word translation unit [See source file] trunc (file mode) TRy block 2nd 3rd 4th TRy keyword type abstract data 2nd arithmetic 2nd built-in 2nd 3rd class 2nd 3rd compound 2nd 3rd dynamic 2nd function return incomplete 2nd integral 2nd library nested [See nsted class] return 2nd 3rd 4th static 2nd determines name lookup name lookup and multiple inheritance type checking argument with class type conversion ellipsis parameter name lookup reference to array argument type identification, run-time 2nd type specifier 2nd type template parameter 2nd 3rd [See also template parameter] type_info header name member no copy or assign operations returned from typeid typedef 2nd typedef and pointer and pointer to member pointer to function typeid operator 2nd and virtual functions example returns type_info typename, keyword compared to class in template parameter inside template definition Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] U_Ptr unary function object unary minus (-) unary operator 2nd unary plus (+) uncaught exception undefined behavior 2nd dangling pointer synthesized copy control invalidated iterator uninitialized class data member uninitialized pointer uninitialized variable underflow_error unexpected 2nd uninitialized 2nd 3rd 4th uninitialized pointer uninitialized_copy 2nd uninitialized_fill union 2nd anonymous 2nd as nested type example limitations on union keyword unique 2nd list unique_copy 2nd unitbuf, manipulator flushes the buffer unnamed namespace 2nd local to file replace file static unsigned 2nd conversion to signed 2nd literal (numU or numu) unsigned char unwinding, stack 2nd upper_bound associative container book finding program example uppercase manipulator use count 2nd design overview generic class held in companion class pointer to self-assignment check user 2nd using declaration 2nd 3rd 4th access control class member access in header overloaded function overloaded inherited functions scope using directive 2nd overloaded function pitfalls scope example name collisions utility header Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] value initialization 2nd and dynamically allocated array and resize dequedeque listlist map subscript operator of dynamically allocated object sequential container vector vectorvector value semantics 2nd value_type map, multimap sequential container set, multiset varargs variable 2nd 3rd define before use defined after case label definition definitions and goto vector 2nd Vector vector argument as element type assign assignment (=) at back begin 2nd capacity Vector capacity vector clear const_iterator 2nd const_reference const_reverse_iterator constructor fromelement count, uses copy constructor constructor taking iterators constructors 2nd difference_type element type constraints 2nd empty 2nd end 2nd erase 2nd invalidates iterator front header initialization from pointer insert invalidates iterator iterator 2nd iterator supports arithmetic memory allocation strategy Vector memory allocation strategy vector memory management strategy parameter performance characteristics pop_back push_back 2nd Vector push_back vector push_back invalidates iterator random-access iterator rbegin 2nd Vector reallocate vector reference relational operators rend 2nd reserve resize reverse_iterator 2nd size 2nd Vector size vector size_type 2nd subscript ([ ]) subscript operator supports relational operators swap type types defined by Vector using explicit destructor call using operator new and delete using placement new vector value_type vector capacity program viable function 2nd with class type conversion virtual base class 2nd ambiguities conversion defining base as derived class constructor name lookup order of construction stream types virtual function 2nd 3rd assignment operator calls resolved at run time compared to run-time type identifi-cation default argument derived classes destructor multiple inheritance exception specifications in constructors in destructor introduction multiple inheritance no virtual constructor overloaded overloaded operator overriding run-time binding pure 2nd example return type run-time type identification scope static to copy unknown type type-sensitive equality virtual inheritance 2nd virtual keyword Visual Studio void 2nd return type void* 2nd const void* 2nd volatile 2nd pointer synthesized copy control vowel counting program Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] wcerr wchar_t literal wchar_t streams wcin wcout weak ordering, strict 2nd wfstream what [See exception] while statement 2nd 3rd 4th condition in whitespace wide character streams wifstream window, console Window_Mgr wiostream wistream wistringstream wofstream word 2nd word count program restricted word per line processing istringstream istringstreamistringstream word transformation program WordQuery definition wostream wostringstream wrap around wstringstream Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] ZooAnimal class hierarchy ZooAnimal, using virtual inheritance ... Finally, we thank the fine folks at Addison- Wesley who have shepherded this edition through the publishing process: Debbie Lafferty, our original editor, who initiated this edition and who had been with the Primer from its very first edition; Peter... show how to make the best use of C++ Although knowledge of C (the language on which C++ was originally based) is not assumed, we do assume the reader has programmed in a modern block-structured language Structure of This Book... Countless programmers have used previous editions of C++ Primer to learn C++ In that time C++ has matured greatly Over the years, the focus of the languageand of C++ programmershas grown beyond a concentration on run-time