Ivor Horton
Stratford-upon-Avon, Warwickshire, UK
Peter Van Weert
Kessel-Lo, Belgium
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484258835 . For more detailed information, please visit www.apress.com/source-code .
ISBN 978-1-4842-5883-5 e-ISBN 978-1-4842-5884-2
https://doi.org/10.1007/978-1-4842-5884-2
Ivor Horton and Peter Van Weert 2020
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction
Welcome to Beginning C++20. This is a revised and updated version of Ivor Hortons original book called Beginning ANSI C++. The C++ language has been extended and improved considerably since then, so much so that it was no longer possible to squeeze detailed explanations of all of C++ into a single book. This tutorial will teach the essentials of the C++ language and Standard Library features, which will be more than enough for you to write your own C++ applications. With the knowledge from this book, you should have no difficulty in extending the depth and scope of your C++ expertise.
We have assumed no prior programming knowledge. If you are keen to learn and have an aptitude for thinking logically, getting a grip on C++ will be easier than you might imagine. By developing C++ skills, youll be learning a language that is already used by millions and that provides the capability for application development in just about any context.
C++ is very powerful. Arguably, its more powerful than most programming languages. So, yes, like with any powerful tool you can wield some considerable damage if you use it without proper training. We often compare C++ to a Swiss Army knife: age-old, trusted, incredibly versatile, yet potentially mind-boggling and full of pointy things that could really hurt you. Once someone clearly explains to you what all the different tools are meant for, however, and teaches you some elementary knife safety rules, then youll never have to look for another pocketknife again.
C++ does not need to be dangerous or difficult at all either. C++ today is much more accessible than many people assume. The language has come a long way since its conception nearly 40 years ago. We have learned how to wield all its mighty blades and tools in the safest and most effective way possible. And, more importantly perhaps, the C++ language and its Standard Library have evolved accordingly to facilitate this. The past decade has seen the rise of what is now known as modern C++. Modern C++ emphasizes the use of newer, more expressive, safer language features, combined with tried and tested best practices and coding guidelines. Once you know and apply a handful of simple rules and techniques, C++ loses much of its complexity. The key is that someone properly and gradually explains not simply what you can do with C++ but rather what you should do with C++. And thats where this book comes in!
In this latest revision of the book, we have gone to great lengths to bring it back in line with the new, modern era of C++ programming were living in. As before, we of course do so in the form of a gradual, informal tutorial. Well introduce to you all the shiny blades and pointy things C++ has to offerboth old and newusing many hands-on coding samples and exercises. But thats not all: more than ever before weve made sure to always explain which tool is best to use for which purpose, why that is the case, and how to avoid getting cut. Weve made sure that you will begin C++, from day one, using the safe, productive, modern programming style that employers will expect from you tomorrow.
The C++ language in this book corresponds to the latest International Organization for Standardization (ISO) standard, commonly referred to as C++20. Not everything in C++20 is covered, since some of the extensions compared to previous versions of the language are targeted toward more advanced use.
Using the Book
To learn C++ with this book, youll need a compiler that conforms to the C++20 standard and a text editor suitable for working with program code. Several compilers are available currently that support, to some extent, C++20 features, many of which are free.
Note
At the time of writing, no compiler fully supports C++20. If the past is any guide, we are confident they will catch up soon, though. https://en.cppreference.com/w/cpp/compiler_support provides an excellent overview of what C++20 features are supported by all major compilers. If your compiler does not support a certain feature yet, you may have to skip some examples or rework them in terms of alternatives.
GCC and Clang are free, open source compilers, with increasing support for C++20. Installing these compilers and putting them together with a suitable editor can be a little tricky if you are new to this kind of thing. An easy way to install a compiler along with a suitable editor is to download a free integrated development environment (IDEs) such as Code::Blocks or Qt Creator. Such IDEs support a complete program development for several compilers, including GCC and Clang.
Another possibility is to use the commercial Microsoft Visual C++ IDE that runs under Microsoft Windows. The Community edition is free for individual use or even small professional teams, and its support for C++20 is on par with GCC and Clang. With Visual Studio you get a comprehensive, easy-to-use professional editor and debugger, as well as support for other languages such as C# and Javascript.