Cover: C++ All-in-One For Dummies, 4th Edition by John Paul Mueller

Logo: Wiley

C++ All-in-One For Dummies®

To view this book's Cheat Sheet, simply go to www.dummies.com and search for “C++ All-in-One For Dummies Cheat Sheet” in the Search box.

Introduction

There are many general-purpose programming languages today, but few can claim to be the language of the millennium. C++ can make that claim, and for good reason:

  • It’s powerful. You can write almost any program in it.
  • It’s fast, and it’s fully compiled. That’s a good thing.
  • It’s easy to use — if you have this book.
  • It’s object oriented. If you’re not sure what that is, don’t worry. You can find out about it by reading this very book you’re holding.
  • It supports functional programming techniques, which makes modeling math problems considerably easier and makes parallel processing easier. This book covers functional programming techniques, too.
  • It’s portable. Versions are available for nearly every computer.
  • It’s standardized. The American National Standards Institute (ANSI) and the International Standards Organization (ISO) both approve an official version.
  • It’s continually updated to meet the changing challenges of the computer community.
  • It’s popular. More people are using C++ because so many other people use it.

Sure, some people criticize C++. But most of these people don’t truly understand C++ or are just having a bad day. Or both.

About This Book

This book is a hands-on, roll-up-your-sleeves experience that gives you the opportunity to truly learn C++. This edition starts out by helping you get a great C++ installation in place. A lot of readers wrote to tell me that they simply couldn’t get C++ to work for them, and I listened by adding configuration instructions in Book 1, Chapter 1. You can find instructions for working with the Mac, Linux, and Windows throughout the book. The examples are also tested to work on all three platforms.

C++ All-in-One For Dummies, 4th Edition, is devoted to working with C++ wherever you want to use it. Book 1, Chapter 2 even includes techniques for writing C++ code on your mobile device, although writing a complex application on your smartphone would be understandably difficult because of the small device size.

At the very beginning, I start you out from square one. I don’t assume any programming experience whatsoever. Everybody has to start somewhere. You can start here. Not to brag, but you are in the hands of a highly successful C++ developer who has shown thousands of people how to program, many of whom also started out from square one.

You already know C++? This book is great for you, too, because although I start discussing C++ from the beginning, I cover the important aspects of the language in depth. Even if you’ve used C++ in the past, this book gets you up to speed with the latest in C++ 14 and above innovations, including C++ 20 additions. Plus, this edition of the book focuses on all the latest programming strategies while removing some of the less used functionality of the past.

If you’re interested in using the time-tested Object Oriented Programming (OOP) techniques that C++ developers have used for years, then Book 2 is where you want to look. You start with a view of classes, but eventually move into more advanced topics, including the use of programming patterns in Book 2 Chapter 4.

One of the most exciting additions to this edition is the use of functional programming techniques, which you can find in Book 3. Functional programming has become extremely popular because it makes modeling math problems significantly easier, and many people use functional programming techniques to solve modern data science problems. More important, functional programming can be a lot easier than earlier programming paradigms.

Every application out there has a bug or two. If you doubt this statement, just try to find one that is bug free—you won’t. Book 4 includes all sorts of techniques you can use to make your application as bug free as possible before it leaves your machine and then help you find the bugs that others graciously point out later.

Book 5 is all about moving you from generalized programming strategies into the advanced strategies used by modern developers. It starts with a look at standardized structures for working with classes in a safe manner. The minibook takes you through

  • Simple structures, such as arrays
  • More advanced data management
  • The use of constructors, destructors, and exceptions
  • Templatized programming
  • Use of the Standard Library (originally called the Standard Template Library or STL).

Everyone needs to work with files at some point. You use local, network, and Internet files today on a regular basis. Book 6 is all about working with files in various ways. This book includes topics on working with data streams as well.

The Standard Library is immense and there are entire books written about its use. C++ All-in-One For Dummies, 4th Edition, focuses on providing you with a really good overview that you can use to drill down into more detailed topics later. Besides looking at the Standard Library in more detail, you discover how to work with User Defined Literals (UDLs) and how to create your own templates. This book also delves into the Boost library, which is the library that has added more to Standard Library than just about any other source. Check out Book 7, Chapters 4 and 5 to learn about Boost. If you use C++ and don’t use Boost, you’re really missing out!

C++ is standardized, and you can use the information in this book on many different platforms. I wrote the samples using Mac OS X, SUSE Linux (some of the beta readers used other flavors of Linux), and Windows systems (with some testing on my ASUS tablet as well). In order to make this happen, I used a compiler called Code::Blocks that runs on almost every computer (Windows, Linux, and Macintosh) and CppDroid for my tablet. It doesn’t matter which device you’re using!

To make absorbing the concepts easy, this book uses the following conventions:

  • Text that you’re meant to type just as it appears in the book is in bold. The exception is when you’re working through a step list: Because each step is bold, the text to type is not bold.
  • Web addresses and programming code appear in monofont. If you’re reading a digital version of this book on a device connected to the Internet, you can click or tap the web address to visit that website, like this: https://www.dummies.com.
  • When you need to type command sequences, you see them separated by a special arrow, like this: File⇒  New File. In this example, you go to the File menu first and then select the New File entry on that menu.
  • When you see words in italics as part of a typing sequence, you need to replace that value with something that works for you. For example, if you see “Type Your Name and press Enter,” you need to replace Your Name with your actual name.

Foolish Assumptions

This book is designed for novice and professional alike. You can either read this book from cover to cover, or you can look up topics and treat the book as a reference guide — whichever works best for you. Keep it on your shelf, and have it ready to grab when you need to look something up. However, I’ve made some assumptions about your level of knowledge when I put the book together. The most important of these assumptions is that you already know how to use your device and work with the operating system that supports it. You also need to know how to perform tasks like downloading files and installing applications. A familiarity with the Internet is also required, and you need to know how to interact with it moderately well to locate the resources you need to work with the book. Finally, you must know how to work with archives, such as the ZIP file format.

Icons Used in This Book

As you read this book, you see icons in the margins that indicate material of interest (or not, as the case may be). This section briefly describes each icon in this book.

Tip Tips are nice because they help you save time or perform some task without a lot of extra work. The tips in this book are time-saving techniques or pointers to resources that you should try so that you can get the maximum benefit from C++. Most important, many of these tips will help you make sense of the overwhelming quantity of libraries and tools that C++ developers have created over the years.

Warning I don’t want to sound like an angry parent or some kind of maniac, but you should avoid doing anything that’s marked with a Warning icon. Otherwise, you might find that your application fails to work as expected, you get incorrect answers from seemingly bulletproof code, or (in the worst-case scenario) you lose data. Given where C++ appears, you might also send the next rocket off to Mars prematurely, make someone’s thermostat misbehave, or cause nationwide power outages. Really, warnings are for everyone!

Technical stuff Whenever you see this icon, think advanced tip or technique. You might find these tidbits of useful information just too boring for words, or they could contain the solution you need to get a program running. Skip these bits of information whenever you like.

Remember If you don’t get anything else out of a particular chapter or section, remember the material marked by this icon. This text usually contains an essential process or a bit of information that you must know to work with C++, or to perform development tasks successfully.

Beyond the Book

If you want to email me, please do! Make sure you send your book-specific requests to:

John@JohnMuellerBooks.com

I get a lot of email from readers, so sometimes it takes me a while to answer. I try very hard to answer every book-specific question I receive, though, so I highly recommend contacting me with your questions. I want to ensure that your book experience is the best one possible. The blog category at http://blog.johnmuellerbooks.com/categories/263/c-all-in-one-for-dummies.aspx contains a wealth of additional information about this book. You can check out the website at http://www.johnmuellerbooks.com/.

This book isn’t the end of your C++ programming experience — it’s really just the beginning. I provide online content to make this book more flexible and better able to meet your needs. That way, as I receive email from you, I can address questions and tell you how updates to either Code::Blocks or the C++ language affect book content. You can also access other cool materials:

  • Cheat Sheet: You remember using crib notes in school to make a better mark on a test, don’t you? You do? Well, a cheat sheet is sort of like that. It provides you with some special notes on things you can do with C++ that not every other developer knows. You can find the cheat sheet for this book at www.dummies.com and typing C++ All-in-One For Dummies, 4th Edition in the search field. It contains really neat information like the top ten mistakes developers make when working with C++, a list of header files that you use in most applications, and some of the C++ syntax that gives most developers problems.
  • Updates: Sometimes changes happen. For example, I might not have seen an upcoming change when I looked into my crystal ball during the writing of this book. In the past, such a situation simply meant that the book would become outdated and less useful, but you can now find updates to the book at www.dummies.com. In addition to these updates, check out the blog posts with answers to reader questions and demonstrations of useful book-related techniques at http://blog.johnmuellerbooks.com/.
  • Companion files: Hey! Who really wants to type all the code in the book? Most readers would prefer to spend their time actually working through coding examples rather than typing. Fortunately for you, the source code is available for download, so all you need to do is read the book to learn C++ coding techniques. Each of the book examples even tells you precisely which example project to use. You can find these files by visiting www.dummies.com/go/caiofd4e.

    Just in case you’re worried about Code::Blocks, you can find complete download and installation instructions for it in Book 1, Chapter 1. Don’t worry about which platform you use. This chapter includes instructions for Mac OS X, Linux, and Windows.

Where to Go from Here

If you’re just starting your C++ adventure, I highly recommend starting at either Book 1, Chapter 1 (for desktop developers) or Book 1, Chapter 2 (for mobile developers). You really do need to create a solid foundation before you can tackle the code in this book. If you’re in a hurry and already have a C++ installation, you can always try starting with Book 1, Chapter 3.

Readers with a little more experience, who already know some C++ basics, can skip some of these introductory chapters, but you definitely don’t want to skip Book 1, Chapter 8 because it contains a lot of pointer-related changes in current versions of C++. If you skip this chapter, you may find later that you have a hard time following the example code in the book because the newer examples use these pointer features.

An advanced reader with some idea of the current changes in C++ 20 could possibly skip Book 1, but scanning Book 2 is a good idea because there are some OOP changes you definitely want to know about. However, even for advanced readers, skipping Book 3 is a bad idea because modern development really is moving toward functional programming techniques.

Book 1

Getting Started with C++

Contents at a Glance

  1. Chapter 1: Configuring Your Desktop System
    1. Obtaining a Copy of C++ 20
    2. Obtaining Code::Blocks
    3. Installing Code::Blocks
    4. Touring the Essential Code::Blocks Features
    5. Using Other IDEs
  2. Chapter 2: Configuring Your Mobile System
    1. Obtaining CppDroid
    2. Considering Other Alternatives
    3. Touring the Essential CppDroid Features
    4. Obtaining CppDroid Help
  3. Chapter 3: Creating Your First C++ Application
    1. Code::Blocks Creating a Project
    2. Typing the Code
    3. Starting with Main
    4. Showing Information
    5. Let Your Application Run Away
  4. Chapter 4: Storing Data in C++
    1. Putting Your Data Places: Variables
    2. Manipulating Integer Variables
    3. Characters
    4. Strings
    5. Making Decisions Using Conditional Operators
    6. Telling the Truth with Boolean Variables
    7. Reading from the Console
  5. Chapter 5: Directing the Application Flow
    1. Doing This or Doing That
    2. Evaluating Conditions in C++
    3. Including Evaluations in C++ Conditional Statements
    4. Repeating Actions with Statements That Loop
    5. Looping for
    6. Looping while
    7. Doing while
    8. Breaking and continuing
    9. Nesting loops
  6. Chapter 6: Dividing Your Work with Functions
    1. Dividing Your Work
    2. Calling a Function
    3. Writing Your Own Functions
    4. Improving On the Basic Function
    5. Calling All String Functions
    6. Understanding main()
  7. Chapter 7: Splitting Up Source Code Files
    1. Creating Multiple Source Files
    2. Sharing with Header Files
    3. Sharing Variables among Source Files
    4. Using the Mysterious Header Wrappers
  8. Chapter 8: Referring to Your Data Through Pointers
    1. Understanding the Changes in Pointers for C++ 20
    2. Heaping and Stacking the Variables
    3. Creating New Raw Pointers
    4. Freeing Raw Pointers
    5. Working with Smart Pointers
    6. Passing Pointer Variables to Functions
    7. Returning Pointer Variables from Functions