
This is for Alexander and Henry who are both going to learn programming soon. If their amazing expertise with Minecraft is anything to go by, they will be brilliant at it.
—Ivor Horton
For my wonderful family. For all your love and support. For putting up with me never having the time to help out around the house or to play with the train set I got you for Christmas.
—Peter Van Weert
Welcome to Beginning C++17 . This is a revised and updated version of Ivor Horton’s 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, you’ll 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, it’s 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 you’ll 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. In essence, 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 in particular 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. 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 that’s where this book comes in!
In this latest revision of the book, we have gone through great lengths to bring it back in line with the new, modern era of C++ programming we’re living in. As before, we of course do so in the form of a gradual, informal tutorial. We’ll introduce to you all the shiny blades and pointy things C++ has to offer—both old and new—using many hands-on coding samples and exercises. But that’s not all: more than ever before we’ve 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. We’ve 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++17. Not everything of C++17 is covered, since many of the extensions compared to previous versions of the language are targeted toward more advanced use. All the examples in the book can be compiled and executed using C++17-conforming compilers that are available now.
To learn C++ with this book, you’ll need a compiler that conforms to the C++17 standard and a text editor suitable for working with program code. Several compilers are available currently that are C++17 compliant, some of which are free.
The GCC and Clang compilers have comprehensive support for C++17 and are both open source and free to download. Installing them 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 GCC along with a suitable editor is to download Code::Blocks or Qt Creator. Both are free integrated development environments (IDEs) for Linux, Apple macOS, and Microsoft Windows. They support a complete program development for several compilers, including GCC and Clang. This implies you get support for both C and C++.
Another possibility is to use Microsoft Visual C++ that runs under Microsoft Windows. It is nearly fully compliant with C++17 as well; all examples in this book should compile with the latest version just fine. The Community and Express editions are free for individual use or even small professional teams. With Visual Studio you get a comprehensive professional editor and support for other languages such as C# and Basic.
There are other compilers that support C++17 as well, which you can find with a quick online search. The online download section for this book also contains a list of further useful resources on how to get started.
We’ve organized the material in this book to be read sequentially, so you should start at the beginning and keep going until you reach the end. However, no one ever learned programming by just reading a book. You’ll only learn how to program in C++ by writing code, so make sure you key in all the examples—don’t just copy them from the download files—and compile and execute the code that you’ve keyed in. This might seem tedious at times, but it’s surprising how much just typing in C++ statements will help your understanding, especially when you may feel you’re struggling with some of the ideas. If an example doesn’t work, resist the temptation to go straight back to the book to see why. Try to figure out from your code what is wrong. This is good practice for what you’ll have to do when you are developing C++ applications for real.
Making mistakes is a fundamental part of the learning process, and the exercises should provide you with ample opportunity for that. It’s a good idea to dream up a few exercises of your own. If you are not sure about how to do something, just have a go before looking it up. The more mistakes you make, the greater the insight you’ll have into what can, and does, go wrong. Make sure you attempt all the exercises, and remember, don’t look at the solutions until you’re sure that you can’t work them out yourself. Most of these exercises just involve a direct application of what’s covered in a chapter—they’re just practice, in other words—but some also require a bit of thought or maybe even inspiration.
Ivor Horton
Peter Van Weert

graduated as a mathematician and was lured into information technology with promises of great rewards for very little work. In spite of the reality being a great deal of work for relatively modest rewards, he has continued to work with computers to the present day. He has been engaged at various times in programming, systems design, consultancy, and the management and implementation of projects of considerable complexity.
Ivor has many years of experience in designing and implementing systems for engineering design and manufacturing control. He has developed occasionally useful applications in a wide variety of programming languages and has taught primarily scientists and engineers to do likewise. His currently published works include tutorials on C, C++, and Java. At the present time, when he is not writing programming books or providing advice to others, he spends his time fishing, traveling, and enjoying life in general.

is a Belgian software engineer whose main interests and expertise are application software development, programming languages, algorithms, and data structures.
He received his master of science degree in computer science summa cum laude with congratulations of the Board of Examiners from the University of Leuven. In 2010, he completed his PhD thesis there on the design and efficient compilation of rule-based programming languages at the research group for declarative programming languages and artificial intelligence. During his doctoral studies, he was a teaching assistant for object-oriented programming (Java), software analysis and design, and declarative programming.
After graduating, Peter worked at Nikon Metrology for more than six years on large-scale, industrial application software in the area of 3D laser scanning and point cloud inspection. He learned to master C++ and refactoring and debugging of very large code bases, and he gained further proficiency in all aspects of the software development process, including the analysis of functional and technical requirements, and agile and scrum-based project and team management.
Today, Peter works for Danaher in its R&D unit for digital dentistry software, developing software for the dental practice of tomorrow.
In his spare time, he has co-authored two books on C++ and two award-winning Windows 8 apps and is a regular expert speaker at, and board member of, the Belgian C++ Users Group.
is a software engineer from Belgium. He graduated from the University of Leuven, Belgium, with a degree in “Burgerlijk ingenieur in de computer wetenschappen” (equivalent to a master of science degree in computer engineering). The year after, he received the cum laude degree of master in artificial intelligence at the same university. After his studies, Marc started working for a software consultancy company called Ordina Belgium. As a consultant, he worked for Siemens and Nokia Siemens Networks on critical 2G and 3G software running on Solaris for telecom operators. This required working on international teams stretching from South America and the United States to Europe, the Middle East, and Asia. Currently, Marc works for Nikon Metrology on industrial 3D laser scanning software.