About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Discovering Modern C++
An Intensive Course for Scientists, Engineers, and Programmers
Peter Gottschling
Boston Columbus Indianapolis New York San Francisco Amsterdam Cape Town
Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City
Sao Paulo Sidney Hong Kong Seoul Singapore Taipei Tokyo
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 author 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.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the U.S., please contact .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2015955339
Copyright 2016 Pearson Education, Inc.
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, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
ISBN-13: 978-0-13-438358-3
ISBN-10: 0-13-438358-3
Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan. First printing, December 2015
To my parents, Helga and Hans-Werner
Contents
Preface
The world is built on C++ (and its C subset).
Herb Sutter
The infrastructures of Google, Amazon, and Facebook are built to a large extent in C++. In addition, a considerable fraction of the underlying technology is implemented in C++. In telecommunications, almost all landline and cellular phone connections are driven by C++ software. Most importantly, all the major transmission nodes in Germany are handled with C++, which means that peace in the authors family unconditionally relies on C++ software.
Even software written in other programming languages depends on C++ since the most popular compilers are realized in C++: Visual Studio, clang
, and newer parts of Gnu and the Intel compiler. This is even more true for software running on Windows which is also implemented in C++ (as well as the Office package). The language is omnipresent; even your cell phone and your car certainly contain components driven by C++. Its inventor, Bjarne Stroustrup, set up a web page with applications where most examples here come from.
In science and engineering, many high-quality software packages are implemented in C++. The strength of the language is manifested in particular when projects exceed a certain size and data structures are rather complex. No wonder that manyif not mostsimulation software programs in science and engineering are realized today in C++: the leaders Abaqus, deal.II, FEniCS, OpenFOAM, to name only a few; likewise the leading CAD software CATIA. Even embedded systems are increasingly realized in C++ thanks to more powerful processors and improved compilers (in which not all modern language features and libraries can always be used). Finally, we do not know how many projects would be realized in C++ instead of C if they had been started later. For instance, the authors good friend Matt Knepley, who is coauthor of the very successful scientific library PETSc, admitted that he would program the library today in C++ if rewriting was affordable.
Reasons to Learn C++
Like no other language, C++ masters the full spectrum from programming sufficiently close to the hardware on one end to abstract high-level programming on the other. The lower-level programminglike user-definable memory managementempowers you as a programmer to understand what really happens during execution, which in turn helps you to understand the behavior of programs in other languages. In C++ you can write extremely efficient programs that can only be slightly out-performed by code written in machine language with ridiculous effort. However, you should wait a little with the hardcore performance tuning and focus first on clear and expressive software.
This is where the high-level features of C++ come into play. The language supports a wide variety of programming paradigms directly: object-oriented programming (), among others.
Several programming techniqueslike RAII ()were invented in and for C++. As the language is so expressive, it was often possible to establish these new techniques without changing the language. And who knows, maybe one day you will invent a new technique.
Reasons to Read This Book
The material of the book has been tested on real humans. The author taught his class C++ for Scientists over three years (i.e., three times two semesters). The students, mostly from the mathematics department, plus some from the physics and engineering departments, often did not know C++ before the class and were able to implement advanced techniques like expression templates (.
The Beauty and the Beast
C++ programs can be written in so many ways. In this book, we will lead you smoothly to the more sophisticated styles. This requires the use of advanced features that might be intimidating at first but will become less so once you get used to them. Actually high-level programming is not only applicable in a wider range but is usually equally or more efficient and readable.