B. Stroustrup - The C++ Programming Language (794319), страница 3
Текст из файла (страница 3)
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. 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 designer.AcknowledgmentsIn addition to the people mentioned in the acknowledgement sections of the first and second editions, 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, MikeMowbray, Rob Murray, Lee Nackman, Joseph Newcomer, Alex Stepanov, David Vandevoorde,Peter Weinberger, and Chris Van Wyk for commenting on draft chapters of this third edition. Without their help and suggestions, this book would have been harder to understand, contained moreerrors, been slightly less complete, and probably been a little bit shorter.I 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.
It is slightly unfair to single out individuals, but it would be even more unfair not to mention anyone, so I’d like to especially mentionMike Ball, Dag Brück, Sean Corfield, Ted Goldstein, Kim Knuttila, Andrew Koenig, DmitryLenkov, Nathan Myers, Martin O’Riordan, Tom Plum, Jonathan Shopiro, John Spicer, JerrySchwarz, Alex Stepanov, and Mike Vilot, as people who each directly cooperated with me oversome part of C++ and its standard library.After the initial printing of this book, many dozens of people have mailed me corrections andsuggestions for improvements. I have been able to accommodate many of their suggestions withinthe framework of the book so that later printings benefitted significantly. Translators of this bookinto many languages have also provided many clarifications.
In response to requests from readers, Ihave added appendices D and E. Let me take this opportunity to thank a few of those who helped:Dave Abrahams, Matt Austern, Jan Bielawski, Janina Mincer Daszkiewicz, Andrew Koenig, Dietmar Kühl, Nicolai Josuttis, Nathan Myers, Paul E. Sevinç, Andy Tenne-Sens, Shoichi Uchida,Ping-Fai (Mike) Yang, and Dennis Yelle.Murray Hill, New JerseyBjarne StroustrupPreface 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 users.This evolution has been guided by the experience of users of widely varying backgrounds workingin a great range of application areas. 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 here.The 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, safe means that a class provides a specifictype-safe interface between the users of the library and its providers; efficient means that use of theclass does not impose significant overheads in run-time or space on the user compared with handwritten C code.This book presents the complete C++ language.
Chapters 1 through 10 give a tutorial introduction; Chapters 11 through 13 provide a discussion of design and software development issues; and,finally, the complete C++ reference manual is included. Naturally, the features added and resolutions made since the original edition are integral parts of the presentation. They include refinedoverloading resolution, memory management facilities, and access control mechanisms, type-safelinkage, const and static member functions, abstract classes, multiple inheritance, templates, andexception handling.C++ is a general-purpose programming language; its core application domain is systems programming in the broadest sense.
In addition, C++ is successfully used in many application areasthat are not covered by this label. Implementations of C++ exist from some of the most modestmicrocomputers to the largest supercomputers and for almost all operating systems. Consequently,this book describes the C++ language itself without trying to explain a particular implementation,programming environment, or library.This book presents many examples of classes that, though useful, should be classified as‘‘toys.’’ This style of exposition allows general principles and useful techniques to stand out moreclearly than they would in a fully elaborated program, where they would be buried in details. MostxiiPreface to the Second Editionof the useful classes presented here, such as linked lists, arrays, character strings, matrices, graphicsclasses, associative arrays, etc., are available in ‘‘bulletproof ’’ and/or ‘‘goldplated’’ versions from awide variety of commercial and non-commercial sources.
Many of these ‘‘industrial strength’’classes and libraries are actually direct and indirect descendants of the toy versions found here.This edition provides a greater emphasis on tutorial aspects than did the first edition of thisbook. However, the presentation is still aimed squarely at experienced programmers and endeavorsnot to insult their intelligence or experience. The discussion of design issues has been greatlyexpanded to reflect the demand for information beyond the description of language features andtheir immediate use. Technical detail and precision have also been increased. The reference manual, in particular, represents many years of work in this direction.
The intent has been to provide abook with a depth sufficient to make more than one reading rewarding to most programmers. Inother words, this book presents the C++ language, its fundamental principles, and the key techniques needed to apply it. Enjoy!AcknowledgmentsIn addition to the people mentioned in the acknowledgements section in the preface to the first edition, I would like to thank Al Aho, Steve Buroff, Jim Coplien, Ted Goldstein, Tony Hansen, Lorraine Juhl, Peter Juhl, Brian Kernighan, Andrew Koenig, Bill Leggett, Warren Montgomery, MikeMowbray, Rob Murray, Jonathan Shopiro, Mike Vilot, and Peter Weinberger for commenting ondraft chapters of this second edition.
Many people influenced the development of C++ from 1985to 1991. I can mention only a few: Andrew Koenig, Brian Kernighan, Doug McIlroy, and JonathanShopiro. Also thanks to the many participants of the ‘‘external reviews’’ of the reference manualdrafts and to the people who suffered through the first year of X3J16.Murray Hill, New JerseyBjarne StroustrupPreface to the First EditionLanguage shapes the way we think,and determines what we can think about.– B.L.WhorfC++ is a general purpose programming language designed to make programming more enjoyablefor the serious programmer. Except for minor details, C++ is a superset of the C programming language.
In addition to the facilities provided by C, C++ provides flexible and efficient facilities fordefining new types. A programmer can partition an application into manageable pieces by definingnew types that closely match the concepts of the application. This technique for program construction is often called data abstraction.
Objects of some user-defined types contain type information.Such objects can be used conveniently and safely in contexts in which their type cannot be determined at compile time. Programs using objects of such types are often called object based. Whenused well, these techniques result in shorter, easier to understand, and easier to maintain programs.The key concept in C++ is class. A class is a user-defined type. Classes provide data hiding,guaranteed initialization of data, implicit type conversion for user-defined types, dynamic typing,user-controlled memory management, and mechanisms for overloading operators. C++ providesmuch better facilities for type checking and for expressing modularity than C does.
It also containsimprovements that are not directly related to classes, including symbolic constants, inline substitution of functions, default function arguments, overloaded function names, free store managementoperators, and a reference type. C++ retains C’s ability to deal efficiently with the fundamentalobjects of the hardware (bits, bytes, words, addresses, etc.).
This allows the user-defined types tobe implemented with a pleasing degree of efficiency.C++ and its standard libraries are designed for portability. The current implementation will runon most systems that support C. C libraries can be used from a C++ program, and most tools thatsupport programming in C can be used with C++.This book is primarily intended to help serious programmers learn the language and use it fornontrivial projects. It provides a complete description of C++, many complete examples, and manymore program fragments.xivPreface to the First EditionAcknowledgmentsC++ could never have matured without the constant use, suggestions, and constructive criticism ofmany friends and colleagues. In particular, Tom Cargill, Jim Coplien, Stu Feldman, Sandy Fraser,Steve Johnson, Brian Kernighan, Bart Locanthi, Doug McIlroy, Dennis Ritchie, Larry Rosler, JerrySchwarz, and Jon Shopiro provided important ideas for development of the language.
Dave Presotto wrote the current implementation of the stream I/O library.In addition, hundreds of people contributed to the development of C++ and its compiler bysending me suggestions for improvements, descriptions of problems they had encountered, andcompiler errors. I can mention only a few: Gary Bishop, Andrew Hume, Tom Karzes, VictorMilenkovic, Rob Murray, Leonie Rose, Brian Schmult, and Gary Walker.Many people have also helped with the production of this book, in particular, Jon Bentley,Laura Eaves, Brian Kernighan, Ted Kowalski, Steve Mahaney, Jon Shopiro, and the participants inthe C++ course held at Bell Labs, Columbus, Ohio, June 26-27, 1985.Murray Hill, New JerseyBjarne StroustrupPart IIntroductionThis introduction gives an overview of the major concepts and features of the C++ programming language and its standard library.
It also provides an overview of this bookand explains the approach taken to the description of the language facilities and theiruse. In addition, the introductory chapters present some background information aboutC++, the design of C++, and the use of C++.Chapters12345Notes to the ReaderA Tour of C++: The BasicsA Tour of C++: Abstraction MechanismsA Tour of C++: Containers and AlgorithmsA Tour of C++: Concurrency and Utilities2IntroductionPart I‘‘... and you, Marcus, you have given me many things; now I shall give you this goodadvice. Be many people.