C++ Primer, Fifth Edition
Stanley B. Lippman
Jose Lajoie
Barbara E. Moo
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sidney Tokyo Singapore Mexico City
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: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Lippman, Stanley B.
C++ primer / Stanley B. Lippman, Jose Lajoie, Barbara E. Moo. 5th ed.
p. cm.
Includes index.
ISBN 0-321-71411-3 (pbk. : alk. paper) 1. C++ (Computer program language) I. Lajoie, Jose. II.
Moo, Barbara E. III. Title.
QA76.73.C153L57697 2013
005.13'3dc23 2012020184
Copyright 2013 Objectwrite Inc., Jose 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. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-71411-4
ISBN-10: 0-321-71411-3
Text printed in the United States on recycled paper at Courier in Westford, Massachusetts.
First printing, August 2012
To Beth, who makes this, and all things, possible.
To Daniel and Anna, who contain virtually all possibilities.
SBL
To Mark and Mom, for their unconditional love and support.
JL
To Andy, who taught me to program and so much more.
BEM
New Features in C++11
Preface
Countless programmers have learned C++ from previous editions of C++ Primer. During that time, C++ has matured greatly: Its focus, and that of its programming community, has widened from looking mostly at machine efficiency to devoting more attention to programmer efficiency.
In 2011, the C++ standards committee issued a major revision to the ISO C++ standard. This revised standard is latest step in C++s evolution and continues the emphasis on programmer efficiency. The primary goals of the new standard are to
Make the language more uniform and easier to teach and to learn
Make the standard libraries easier, safer, and more efficient to use
Make it easier to write efficient abstractions and libraries
In this edition, we have completely revised the C++ Primer to use the latest standard. You can get an idea of how extensively the new standard has affected C++ by reviewing the New Features Table of Contents, which lists the sections that cover new material and appears on page xxi.
Some additions in the new standard, such as auto
for type inference, are pervasive. These facilities make the code in this edition easier to read and to understand. Programs (and programmers!) can ignore type details, which makes it easier to concentrate on what the program is intended to do. Other new features, such as smart pointers and move-enabled containers, let us write more sophisticated classes without having to contend with the intricacies of resource management. As a result, we can start to teach how to write your own classes much earlier in the book than we did in the Fourth Edition. Weand youno longer have to worry about many of the details that stood in our way under the previous standard.
Weve marked those parts of the text that cover features defined by the new standard, with a marginal icon. We hope that readers who are already familiar with the core of C++ will find these alerts useful in deciding where to focus their attention. We also expect that these icons will help explain error messages from compilers that might not yet support every new feature. Although nearly all of the examples in this book have been compiled under the current release of the GNU compiler, we realize some readers will not yet have access to completely updated compilers. Even though numerous capabilities have been added by the latest standard, the core language remains unchanged and forms the bulk of the material that we cover. Readers can use these icons to note which capabilities may not yet be available in their compiler.
Why Read This Book?
Modern C++ can be thought of as comprising three parts:
The low-level language, much of which is inherited from C
More advanced language features that allow us to define our own types and to organize large-scale programs and systems
The standard library, which uses these advanced features to provide useful data structures and algorithms
Most texts present C++ in the order in which it evolved. They teach the C subset of C++ first, and present the more abstract features of C++ as advanced topics at the end of the book. There are two problems with this approach: Readers can get bogged down in the details inherent in low-level programming and give up in frustration. Those who do press on learn bad habits that they must unlearn later.
We take the opposite approach: Right from the start, we use the features that let programmers ignore the details inherent in low-level programming. For example, we introduce and use the library string
and vector
types along with the built-in arithmetic and array types. Programs that use these library types are easier to write, easier to understand, and much less error-prone.
Too often, the library is taught as an advanced topic. Instead of using the library, many books use low-level programming techniques based on pointers to character arrays and dynamic memory management. Getting programs that use these low-level techniques to work correctly is much harder than writing the corresponding C++ code using the library.
Throughout C++ Primer, we emphasize good style: We want to help you, the reader, develop good habits immediately and avoid needing to unlearn bad habits as you gain more sophisticated knowledge. We highlight particularly tricky matters and warn about common misconceptions and pitfalls.
We also explain the rationale behind the rulesexplaining the why not just the what. We believe that by understanding why things work as they do, readers can more quickly cement their grasp of the language.