Transcription of The C++ Programming Language (Special 3rd Edition)
1 TheC+ +ProgrammingLanguageThird EditionBjarne StroustrupAT&T LabsMurray Hill, New JerseyAddison-WesleyAn Imprint of Addison Wesley Longman, , Massachusetts Harlow, England Menlo Park, CaliforniaBerkeley, California Don Mills, Ontario SydneyBonn Amsterdam Tokyo Mexico CityiiMany of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Wherethose designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have beenprinted in initial capital letters or all capital lettersThe author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of anykind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages inconnection with or arising out of the use of the information contained publisher offers discounts on this book when ordered in quantity for special sales.
2 For more information please contact:Corporate & Professional Publishing GroupAddison-Wesley Publishing CompanyOne Jacob WayReading, Massachusetts 01867 Library of Congress Cataloging-in-Publication DataStroustrup, BjarneThe C++ Programming Language / Bjarne Stroustrup. 3rd. 0-201-88954-41. C++(Computer Programming Language ) I. 1997 3 dc21 CIPC opyright 1997 by AT&TAll rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form orby any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of thepublisher. Printed in the United States of book was typeset in Times and Courier by the 0-201-88954-4 Printed on recycled paper1 2 3 4 5 6 7 8 9 CRW 0100999897 First printing, June 1997 ContentsContents iiiPreface vPreface to Second Edition viiPreface to First Edition ixIntroductory Material 11 Notes to the Reader.
3 32 A Tour of C++.. 213 A Tour of the Standard Library .. 45 Part I: Basic Facilities 674 Types and Declarations .. 695 Pointers, Arrays, and Structures .. 876 Expressions and Statements .. 1077 Functions .. 1438 Namespaces and Exceptions .. 1659 Source Files and Programs .. 197 The C++ Programming Language , Third Editionby Bjarne Stroustrup. Copyright 1997 by AT& by Addison Wesley Longman, Inc. ISBN 0-201-88954-4. All rights ContentsPart II: Abstraction Mechanisms 22110 Classes .. 22311 Operator Overloading .. 26112 Derived Classes .. 30113 Templates .. 32714 Exception Handling .. 35515 Class Hierarchies .. 389 Part III: The Standard Library 42716 Library Organization and Containers .. 42917 Standard Containers .. 46118 Algorithms and Function Objects .. 50719 Iterators and Allocators.
4 54920 Strings .. 57921 Streams .. 60522 Numerics .. 657 Part IV: Design Using C++68923 Development and Design .. 69124 Design and Programming .. 72325 Roles of Classes .. 765 Appendices 791A The C++Grammar .. 793B Compatibility .. 815C Technicalities .. 827 Index 869 The C++ Programming Language , Third Editionby Bjarne Stroustrup. Copyright 1997 by AT& by Addison Wesley Longman, Inc. ISBN 0-201-88954-4. All rights is understanding. Kristen NygaardI find using C++more enjoyable than ever. C++ s support for design and Programming hasimproved dramatically over the years, and lots of new helpful techniques have been developed forits use. However, C++is notjustfun. Ordinary practical programmers have achieved significantimprovements in productivity, maintainability, flexibility, and quality in projects of just about anykind and scale. By now, C++has fulfilled most of the hopes I originally had for it, and also suc-ceeded at tasks I hadn t even dreamt book introduces standard C++ and the key Programming and design techniques supportedby C++.
5 Standard C++is a far more powerful and polished Language than the version of C++intro-duced by the first edition of this book. New Language features such as namespaces, exceptions,templates, and run-time type identification allow many techniques to be applied more directly thanwas possible before, and the standard library allows the programmer to start from a much higherlevel than the bare a third of the information in the second edition of this book came from the first. Thisthird edition is the result of a rewrite of even larger magnitude. It offers something to even themost experienced C++programmer; at the same time, this book is easier for the novice to approachthan its predecessors were. The explosion of C++use and the massive amount of experience accu-mulated as a result makes this definition of an extensive standard library makes a difference to the way C++concepts canbe presented.
6 As before, this book presents C++independently of any particular implementation,and as before, the tutorial chapters present Language constructs and concepts in a bottom up order so that a construct is used only after it has been defined. However, it is much easier to use awell-designed library than it is to understand the details of its implementation. Therefore, the stan-dard library can be used to provide realistic and interesting examples well before a reader can beassumed to understand its inner workings. The standard library itself is also a fertile source of pro-gramming examples and design ISO/IEC 14882, Standard for the C++ Programming C++ Programming Language , Third Editionby Bjarne Stroustrup. Copyright 1997 by AT& by Addison Wesley Longman, Inc. ISBN 0-201-88954-4. All rights PrefaceThis book presents every major C++ Language feature and the standard library. It is organizedaround Language and library facilities.
7 However, features are presented in the context of their is, the focus is on the Language as the tool for design and Programming rather than on the lan-guage in itself. This book demonstrates key techniques that make C++effective and teaches thefundamental concepts necessary for mastery. Except where illustrating technicalities, examples aretaken from the domain of systems software. A companion,The Annotated C++ Language Stan-dard, presents the complete Language definition together with annotations to make it more primary aim of this book is to help the reader understand how the facilities offered by C++support key Programming techniques. The aim is to take the reader far beyond the point where heor she gets code running primarily by copying examples and emulating Programming styles fromother languages. Only a good understanding of the ideas behind the Language facilities leads tomastery.
8 Supplemented by implementation documentation, the information provided is sufficientfor completing significant real-world projects. The hope is that this book will help the reader gainnew insights and become a better programmer and addition to the people mentioned in the acknowledgement sections of the first and second edi-tions, I would like to thank Matt Austern, Hans Boehm, Don Caldwell, Lawrence Crowl, AlanFeuer, Andrew Forrest, David Gay, Tim Griffin, Peter Juhl, Brian Kernighan, Andrew Koenig,Mike Mowbray, Rob Murray, Lee Nackman, Joseph Newcomer, Alex Stepanov, David Vandevo-orde, Peter Weinberger, and Chris Van Wyk for commenting on draft chapters of this third their help and suggestions, this book would have been harder to understand, containedmore errors, been slightly less complete, and probably been a little bit would also like to thank the volunteers on the C++standards committees who did an immenseamount of constructive work to make C++what it is today.
9 It is slightly unfair to single out indi-viduals, but it would be even more unfair not to mention anyone, so I d like to especially mentionMike Ball, Dag Br..uck, Sean Corfield, Ted Goldstein, Kim Knuttila, Andrew Koenig, Jos e Lajoie,Dmitry Lenkov, Nathan Myers, Martin O Riordan, Tom Plum, Jonathan Shopiro, John Spicer,Jerry Schwarz, Alex Stepanov, and Mike Vilot, as people who each directly cooperated with meover some part of C++and its standard Hill, New Jersey Bjarne StroustrupThe C++ Programming Language , Third Editionby Bjarne Stroustrup. Copyright 1997 by AT& by Addison Wesley Longman, Inc. ISBN 0-201-88954-4. All rights to the Second EditionThe road goes ever on and on. Bilbo BagginsAs promised in the first edition of this book, C++has been evolving to meet the needs of its evolution has been guided by the experience of users of widely varying backgrounds workingin a great range of application areas.
10 The C++user-community has grown a hundredfold during thesix years since the first edition of this book; many lessons have been learned, and many techniqueshave been discovered and/or validated by experience. Some of these experiences are reflected primary aim of the Language extensions made in the last six years has been to enhance C++as a Language for data abstraction and object-oriented Programming in general and to enhance it asa tool for writing high-quality libraries of user-defined types in particular. A high-qualitylibrary, is a library that provides a concept to a user in the form of one or more classes that areconvenient, safe, and efficient to use. In this context,safemeans that a class provides a specifictype-safe interface between the users of the library and its providers;efficientmeans that use of theclass does not impose significant overheads in run-time or space on the user compared with hand-written C book presents the complete C++ Language .