C++ ALL IN ONE FOR DUMMIES [4 ed.] 9781119601753, 1119601754

9,752 1,777 11MB

English Pages [915] Year 2019

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

C++ ALL IN ONE FOR DUMMIES [4 ed.]
 9781119601753, 1119601754

Table of contents :
Title Page
Copyright Page
Table of Contents
Introduction
About This Book
Foolish Assumptions
Icons Used in This Book
Beyond the Book
Where to Go from Here
Book 1 Getting Started with C++
Chapter 1 Configuring Your Desktop System
Obtaining a Copy of C++ 20
Obtaining Code::Blocks
Installing Code::Blocks
Working with Windows
Working with Mac OS X
Using the standard Linux installation
Using the graphical Linux installation
Touring the Essential Code::Blocks Features
Starting Code::Blocks for the first time
Opening the sample projects
Viewing the essential windows
Using Other IDEs
Chapter 2 Configuring Your Mobile System
Obtaining CppDroid
Understanding why CppDroid is such a great choice
Getting your copy of CppDroid
Ensuring you get a good install
Considering Other Alternatives
Working with C4Droid
Getting multiple language support with AIDE
Using web-based IDEs
Touring the Essential CppDroid Features
Getting started with CppDroid
Accessing an example
Working with a simple online project
Accessing your source code
Considering differences with the desktop environment
Obtaining CppDroid Help
Working with the Help documentation
Getting community support
Using the free examples
Accessing the tutorials
Chapter 3 Creating Your First C++ Application
Code::Blocks Creating a Project
Understanding projects
Defining your first project
Building and executing your first application
Typing the Code
Starting with Main
Showing Information
Doing some math
Tabbing your output
Let Your Application Run Away
Chapter 4 Storing Data in C++
Putting Your Data Places: Variables
Creating an integer variable
Declaring multiple variables
Changing values
Setting one variable equal to another
Initializing a variable
Creating a great name for yourself
Manipulating Integer Variables
Adding integer variables
Subtracting integer variables
Multiplying integer variables
Dividing integer variables
Characters
Null character
Nonprintable and other cool characters
Strings
Getting a part of a string
Changing part of a string
Adding onto a string
Adding two strings
Making Decisions Using Conditional Operators
Telling the Truth with Boolean Variables
Reading from the Console
Chapter 5 Directing the Application Flow
Doing This or Doing That
Evaluating Conditions in C++
Finding the right C++ operators
Combining multiple evaluations
Including Evaluations in C++ Conditional Statements
Deciding what if and also what else
Going further with the else and if
Repeating Actions with Statements That Loop
Understanding how computers use loops
Looping situations
Looping for
Performing a simple for loop
Using multiple initialization variables
Working with ranges
Placing a condition within the declaration
Letting C++ determine the type
Looping while
Doing while
Breaking and continuing
Breaking
Continuing
Nesting loops
Chapter 6 Dividing Your Work with Functions
Dividing Your Work
Calling a Function
Passing a variable
Passing multiple variables
Writing Your Own Functions
Defining the AddOne() function
Seeing how AddOne() is called
Taking the AddOne() Function apart
Considering the AddOne() parameter
Understanding the AddOne() name and type
Improving On the Basic Function
Using multiple parameters or no parameters
Returning nothing
Keeping your variables local
Forward references and function prototypes
Writing two versions of the same function
Calling All String Functions
Inserting a string into a string
Removing parts of a string
Replacing parts of a string
Using the string functions together
Understanding main()
Chapter 7 Splitting Up Source Code Files
Creating Multiple Source Files
Adding a new source code file
Removing an existing source code file
Creating a project with multiple existing files
Getting multiple files to interact
Sharing with Header Files
Adding the header only once
Using angle brackets or quotes
Sharing Variables among Source Files
Using the Mysterious Header Wrappers
Chapter 8 Referring to Your Data Through Pointers
Understanding the Changes in Pointers for C++ 20
Avoiding broken code
Considering the issues
Writing cleaner and less bug-prone code
Heaping and Stacking the Variables
Getting a variable’s address
Changing a variable by using a pointer
Pointing at a string
Pointing to something else
Tips on pointer variables
Creating New Raw Pointers
Using new
Using an initializer
Freeing Raw Pointers
Working with Smart Pointers
Creating smart pointers using std::unique_ptr and std::shared_ptr
Defining nullable values using std::optional and std::nullopt
Passing Pointer Variables to Functions
Returning Pointer Variables from Functions
Book 2 Understanding Objects and Classes
Chapter 1 Working with Classes
Understanding Objects and Classes
Classifying classes and objects
Describing methods and data
Implementing a class
Separating method code
The parts of a class
Working with a Class
Accessing members
Using classes and raw pointers
Using classes and smart pointers
Passing objects to functions
Using const parameters in functions
Using the this pointer
Overloading methods
Starting and Ending with Constructors and Destructors
Starting with constructors
Ending with destructors
Sampling constructors and destructors
Adding parameters to constructors
Building Hierarchies of Classes
Creating a hierarchy in C++
Understanding types of inheritance
Creating and Using Object Aliases
Chapter 2 Using Advanced C++ Features
Filling Your Code with Comments
Converting Types
Understanding how int and string conversions work
Seeing int and string conversions in action
Considering other conversion issues
Reading from the Console
Understanding Preprocessor Directives
Understanding the basics of preprocessing
Creating constants and macros with #define
Performing conditional compilation
Exercising the basic preprocessor directives
Using Constants
Using Switch Statements
Supercharging enums with Classes
Working with Random Numbers
Storing Data in Arrays
Declaring and accessing an array
Arrays of pointers
Passing arrays to functions
Adding and subtracting pointers
Chapter 3 Planning and Building Objects
Recognizing Objects
Observing the Mailboxes class
Observing the Mailbox class
Finding other objects
Encapsulating Objects
Considering the Application Programming Interface
Understanding properties
Choosing between private and protected
Defining a process
Implementing properties
Building Hierarchies
Establishing a hierarchy
Protecting members when inheriting
Overriding methods
Specializing with polymorphism
Getting abstract about things
Chapter 4 Building with Design Patterns
Delving Into Pattern History
Introducing a Simple Pattern: the Singleton
Using an existing pattern
Creating a singleton pattern class
Watching an Instance with an Observer
Understanding the observer pattern
Defining an observer pattern class
Observers and the Standard C++ Library
Automatically adding an observer
Mediating with a Pattern
Defining the mediator pattern scenario
Outlining the car example
Creating the car example
Book 3 Understanding Functional Programming
Chapter 1 Considering Functional Programming
Understanding How Functional Programming Differs
Defining an Impure Language
Considering the requirements
Understanding the C++ functional limitations
Seeing Data as Immutable
Working with immutable variables
Working with immutability in classes and structures
Creating constant expressions
Considering the Effects of State
Eliminating Side Effects
Contrasting declarations and functions
Associating functions with side effects
Removing side effects
Creating a declarative C++ example
Understanding the Role of auto
Passing Functions to Functions
Seeing a simple example of function input
Using transforms
Using Lambda Expressions for Implementation
Chapter 2 Working with Lambda Expressions
Creating More Readable and Concise C++ Code
Defining the Essential Lambda Expression
Defining the parts of a lambda expression
Relying on computer detection of return type
Using the auto keyword with lambda expressions
Using lambda expressions as macros
Developing with Lambda Expressions
Using lambda expressions with classes and structures
Working with the capture clause
Sorting data using a lambda expression
Specifying that the lambda expression throws exceptions
Chapter 3 Advanced Lambda Expressions
Considering the C++ 20 Lambda Extensions
Defining an immediate function
Using = and this in captures
Finding other changes
Working in Unevaluated Contexts
Using Assignable Stateless Lambda Expressions
Dealing with Pack Expansions
Considering the template
Processing the variables using recursion
Processing the variables using a lambda expression
Book 4 Fixing Problems
Chapter 1 Dealing with Bugs
It’s Not a Bug. It’s a Feature!
Make Your Application Features Look Like Features
Anticipating (Almost) Everything
Considering menus
Dealing with textual input
Performing string processing
Avoiding Mistakes, Plain and Simple
Chapter 2 Debugging an Application
Programming with Debuggers
Running the debugger
Recognizing the parts of the Code::Blocks debugger
Debugging with Different Tools
Debugging a Code::Blocks Application with Command-Line Arguments
Chapter 3 Stopping and Inspecting Your Code
Setting and Disabling Breakpoints
Setting a breakpoint in Code::Blocks
Enabling and disabling breakpoints
Watching, Inspecting, and Changing Variables
Watching the variables
Changing values
Chapter 4 Traveling About the Stack
Stacking Your Data
Moving about the stack
Storing local variables
Debugging with Advanced Features
Viewing threads
Tracing through assembly code
Book 5 Advanced Programming
Chapter 1 Working with Arrays, Pointers, and References
Building Up Arrays
Declaring arrays
Arrays and pointers
Using multidimensional arrays
Arrays and command-line parameters
Allocating an array on the heap
Deleting an array from the heap
Storing arrays of pointers and arrays of arrays
Building constant arrays
Pointing with Pointers
Becoming horribly complex
Pointers to functions
Pointing a variable to a method
Pointing to static methods
Referring to References
Reference variables
Returning a reference from a function
Chapter 2 Creating Data Structures
Working with Data
The great variable roundup
Expressing variables from either side
Casting a spell on your data
Comparing casting and converting
Casting safely with C++
Structuring Your Data
Structures as component data types
Equating structures
Returning compound data types
Naming Your Space
Creating a namespace
Employing using namespace
Using variables
Using part of a namespace
Chapter 3 Constructors, Destructors, and Exceptions
Constructing and Destructing Objects
Overloading constructors
Initializing members
Adding a default constructor
Functional constructors
Calling one constructor from another
Copying instances with copy constructors
When constructors go bad
Destroying your instances
Virtually inheriting destructors
Programming the Exceptions to the Rule
Creating a basic try. . .catch block
Using multiple catch blocks
Throwing direct instances
Catching any exception
Rethrowing an exception
Using a standard category
Chapter 4 Advanced Class Usage
Inherently Inheriting Correctly
Morphing your inheritance
Avoiding polymorphism
Adjusting access
Avoiding variable naming conflicts
Using class-based access adjustment
Returning something different, virtually speaking
Multiple inheritance
Virtual inheritance
Friend classes and functions
Using Classes and Types within Classes
Nesting a class
Types within classes
Chapter 5 Creating Classes with Templates
Templatizing a Class
Considering types
Defining the need for templates
Creating and using a template
Understanding the template keyword
Going Beyond the Basics
Separating a template from the function code
Including static members in a template
Parameterizing a Template
Putting different types in the parameter
Including multiple parameters
Working with non-type parameters
Typedefing a Template
Deriving Templates
Deriving classes from a class template
Deriving a class template from a class
Deriving a class template from a class template
Templatizing a Function
Overloading and function templates
Templatizing a method
Chapter 6 Programming with the Standard Library
Architecting the Standard Library
Containing Your Classes
Storing in a vector
Working with std::array
Mapping your data
Containing instances, pointers, or references
Working with copies
Comparing instances
Iterating through a container
A map of pairs in your hand
The Great Container Showdown
Associating and storing with a set
Unionizing and intersecting sets
Listing with list
Stacking the deque
Waiting in line with stacks and queues
Copying Containers
Creating and Using Dynamic Arrays
Working with Unordered Data
Using std::unordered_set to create an unordered set
Manipulating unordered sets
Working with Ranges
Book 6 Reading and Writing Files
Chapter 1 Filing Information with the Streams Library
Seeing a Need for Streams
Programming with the Streams Library
Getting the right header file
Opening a file
Reading from a file
Reading and writing a file
Working with containers
Handling Errors When Opening a File
Flagging the ios Flags
Chapter 2 Writing with Output Streams
Inserting with the

Citation preview

C++ ALL-IN-ONE

4th Edition

by John Paul Mueller

C++ All-in-One For Dummies®, 4th Edition Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2021 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE.  NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES.  IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM.  THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2020949804 ISBN: 978-1-119-60174-6 ISBN 978-1-119-60175-3 (ebk); ISBN 978-1-119-60173-9 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1

Contents at a Glance Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Book 1: Getting Started with C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 1: CHAPTER 2: CHAPTER 3: CHAPTER 4: CHAPTER 5: CHAPTER 6: CHAPTER 7: CHAPTER 8:

Configuring Your Desktop System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Configuring Your Mobile System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Creating Your First C++ Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Storing Data in C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Directing the Application Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Dividing Your Work with Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Splitting Up Source Code Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Referring to Your Data Through Pointers . . . . . . . . . . . . . . . . . . . . . . . . 187

Book 2: Understanding Objects and Classes. . . . . . . . . . . . . . . . CHAPTER 1:

225

Working with Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 2: Using Advanced C++ Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 3: Planning and Building Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 4: Building with Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

227 269 309 335

Book 3: Understanding Functional Programming . . . . . . . . .

367

CHAPTER 1:

Considering Functional Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Working with Lambda Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 CHAPTER 3: Advanced Lambda Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 CHAPTER 2:

Book 4: Fixing Problems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 1:

427

Dealing with Bugs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 2: Debugging an Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 3: Stopping and Inspecting Your Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 4: Traveling About the Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

429 443 457 469

Book 5: Advanced Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

479

CHAPTER 1: CHAPTER 2: CHAPTER 3: CHAPTER 4: CHAPTER 5: CHAPTER 6:

Working with Arrays, Pointers, and References . . . . . . . . . . . . . . . . . . . Creating Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constructors, Destructors, and Exceptions. . . . . . . . . . . . . . . . . . . . . . . Advanced Class Usage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Classes with Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Programming with the Standard Library. . . . . . . . . . . . . . . . . . . . . . . . .

481 515 541 571 601 637

Book 6: Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 1: CHAPTER 2: CHAPTER 3: CHAPTER 4: CHAPTER 5:

Filing Information with the Streams Library . . . . . . . . . . . . . . . . . . . . . . Writing with Output Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reading with Input Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building Directories and Contents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Streaming Your Own Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Book 7: Advanced Standard Library Usage . . . . . . . . . . . . . . . . . CHAPTER 1:

681 683 697 711 727 737 751

Exploring the Standard Library Further. . . . . . . . . . . . . . . . . . . . . . . . . . Working with User-Defined Literals (UDLs). . . . . . . . . . . . . . . . . . . . . . . Building Original Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Investigating Boost. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Boosting up a Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

753 779 795 817 849

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

869

CHAPTER 2: CHAPTER 3: CHAPTER 4: CHAPTER 5:

Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Foolish Assumptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Icons Used in This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Beyond the Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1 4 4 5 6

BOOK 1: GETTING STARTED WITH C++ . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 1:

Configuring Your Desktop System. . . . . . . . . . . . . . . . . . . . . . 9 Obtaining a Copy of C++ 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Obtaining Code::Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing Code::Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Mac OS X. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the standard Linux installation. . . . . . . . . . . . . . . . . . . . . . . . . Using the graphical Linux installation . . . . . . . . . . . . . . . . . . . . . . . . Touring the Essential Code::Blocks Features. . . . . . . . . . . . . . . . . . . . . . Starting Code::Blocks for the first time . . . . . . . . . . . . . . . . . . . . . . . Opening the sample projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Viewing the essential windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Other IDEs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CHAPTER 2:

10 11 12 12 13 14 15 17 18 19 20 25

Configuring Your Mobile System. . . . . . . . . . . . . . . . . . . . . . 27 Obtaining CppDroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Understanding why CppDroid is such a great choice . . . . . . . . . . . 29 Getting your copy of CppDroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Ensuring you get a good install. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Considering Other Alternatives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Working with C4Droid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Getting multiple language support with AIDE. . . . . . . . . . . . . . . . . . 33 Using web-based IDEs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Touring the Essential CppDroid Features . . . . . . . . . . . . . . . . . . . . . . . . 35 Getting started with CppDroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Accessing an example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Working with a simple online project. . . . . . . . . . . . . . . . . . . . . . . . . 37 Accessing your source code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Considering differences with the desktop environment. . . . . . . . . 39

Table of Contents

v

Obtaining CppDroid Help. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with the Help documentation . . . . . . . . . . . . . . . . . . . . . . . Getting community support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the free examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing the tutorials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CHAPTER 3:

CHAPTER 4:

40 40 41 42 43

Creating Your First C++ Application. . . . . . . . . . . . . . . . . . . 45 Code::Blocks Creating a Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Defining your first project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building and executing your first application . . . . . . . . . . . . . . . . . . Typing the Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Starting with Main. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Showing Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Doing some math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tabbing your output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Let Your Application Run Away. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46 46 47 52 53 55 55 60 66 67

Storing Data in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

69

Putting Your Data Places: Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Creating an integer variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Declaring multiple variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Changing values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Setting one variable equal to another . . . . . . . . . . . . . . . . . . . . . . . . 74 Initializing a variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Creating a great name for yourself. . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Manipulating Integer Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Adding integer variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Subtracting integer variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Multiplying integer variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Dividing integer variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Null character. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Nonprintable and other cool characters . . . . . . . . . . . . . . . . . . . . . . 89 Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Getting a part of a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Changing part of a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Adding onto a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Adding two strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Making Decisions Using Conditional Operators. . . . . . . . . . . . . . . . . . . 98 Telling the Truth with Boolean Variables. . . . . . . . . . . . . . . . . . . . . . . . 100 Reading from the Console. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

vi

C++ All-in-One For Dummies

CHAPTER 5:

CHAPTER 6:

Directing the Application Flow. . . . . . . . . . . . . . . . . . . . . . .

105

Doing This or Doing That . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Evaluating Conditions in C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Finding the right C++ operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combining multiple evaluations. . . . . . . . . . . . . . . . . . . . . . . . . . . . Including Evaluations in C++ Conditional Statements. . . . . . . . . . . . . Deciding what if and also what else. . . . . . . . . . . . . . . . . . . . . . . . . Going further with the else and if. . . . . . . . . . . . . . . . . . . . . . . . . . . Repeating Actions with Statements That Loop. . . . . . . . . . . . . . . . . . . Understanding how computers use loops . . . . . . . . . . . . . . . . . . . Looping situations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Looping for. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Performing a simple for loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using multiple initialization variables. . . . . . . . . . . . . . . . . . . . . . . . Working with ranges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Placing a condition within the declaration. . . . . . . . . . . . . . . . . . . . Letting C++ determine the type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Looping while. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Doing while. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Breaking and continuing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Breaking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Continuing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nesting loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

106 107 108 110 111 112 113 115 116 116 117 118 123 126 128 129 130 132 133 134 135 136

Dividing Your Work with Functions . . . . . . . . . . . . . . . . .

139

Dividing Your Work. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Calling a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Passing a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Passing multiple variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Writing Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Defining the AddOne() function. . . . . . . . . . . . . . . . . . . . . . . . . . . . .149 Seeing how AddOne() is called . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Taking the AddOne() Function apart. . . . . . . . . . . . . . . . . . . . . . . . . 150 Considering the AddOne() parameter . . . . . . . . . . . . . . . . . . . . . . . 151 Understanding the AddOne() name and type. . . . . . . . . . . . . . . . . 152 Improving On the Basic Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Using multiple parameters or no parameters . . . . . . . . . . . . . . . . 153 Returning nothing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Keeping your variables local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Forward references and function prototypes. . . . . . . . . . . . . . . . . 159 Writing two versions of the same function . . . . . . . . . . . . . . . . . . . 161

Table of Contents

vii

CHAPTER 7:

CHAPTER 8:

Calling All String Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inserting a string into a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing parts of a string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replacing parts of a string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the string functions together . . . . . . . . . . . . . . . . . . . . . . . . . Understanding main(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

163 163 164 164 164 165

Splitting Up Source Code Files. . . . . . . . . . . . . . . . . . . . . . . .

169

Creating Multiple Source Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding a new source code file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing an existing source code file. . . . . . . . . . . . . . . . . . . . . . . Creating a project with multiple existing files. . . . . . . . . . . . . . . . . Getting multiple files to interact . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sharing with Header Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding the header only once. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using angle brackets or quotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sharing Variables among Source Files. . . . . . . . . . . . . . . . . . . . . . . . . . Using the Mysterious Header Wrappers . . . . . . . . . . . . . . . . . . . . . . . .

170 170 173 173 177 179 182 182 183 185

Referring to Your Data Through Pointers. . . . . . . . . .

187

Understanding the Changes in Pointers for C++ 20. . . . . . . . . . . . . . . 188 Avoiding broken code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Considering the issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Writing cleaner and less bug-prone code . . . . . . . . . . . . . . . . . . . . 191 Heaping and Stacking the Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Getting a variable’s address. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Changing a variable by using a pointer . . . . . . . . . . . . . . . . . . . . . . 198 Pointing at a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Pointing to something else. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Tips on pointer variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Creating New Raw Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Using new. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Using an initializer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Freeing Raw Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Working with Smart Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Creating smart pointers using std::unique_ptr and std::shared_ptr. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Defining nullable values using std::optional and std::nullopt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Passing Pointer Variables to Functions . . . . . . . . . . . . . . . . . . . . . . . . . 218 Returning Pointer Variables from Functions. . . . . . . . . . . . . . . . . . . . . 221

viii

C++ All-in-One For Dummies

PART 2: UNDERSTANDING OBJECTS AND CLASSES. . . . . . . CHAPTER 1:

CHAPTER 2:

225

Working with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

227

Understanding Objects and Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . Classifying classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Describing methods and data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Separating method code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The parts of a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using classes and raw pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using classes and smart pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . Passing objects to functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using const parameters in functions . . . . . . . . . . . . . . . . . . . . . . . . Using the this pointer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Starting and Ending with Constructors and Destructors. . . . . . . . . . . Starting with constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ending with destructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sampling constructors and destructors. . . . . . . . . . . . . . . . . . . . . . Adding parameters to constructors. . . . . . . . . . . . . . . . . . . . . . . . . Building Hierarchies of Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a hierarchy in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding types of inheritance. . . . . . . . . . . . . . . . . . . . . . . . . Creating and Using Object Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

227 230 231 232 237 240 241 241 244 248 249 251 252 256 259 259 260 260 263 264 265 266 267

Using Advanced C++ Features. . . . . . . . . . . . . . . . . . . . . . . .

269

Filling Your Code with Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Converting Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding how int and string conversions work . . . . . . . . . . Seeing int and string conversions in action. . . . . . . . . . . . . . . . . . . Considering other conversion issues. . . . . . . . . . . . . . . . . . . . . . . . Reading from the Console. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . Understanding the basics of preprocessing . . . . . . . . . . . . . . . . . . Creating constants and macros with #define. . . . . . . . . . . . . . . . . Performing conditional compilation. . . . . . . . . . . . . . . . . . . . . . . . . Exercising the basic preprocessor directives . . . . . . . . . . . . . . . . . Using Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

270 272 272 273 276 277 282 282 283 286 288 292 295

Table of Contents

ix

CHAPTER 3:

Supercharging enums with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Random Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Storing Data in Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Declaring and accessing an array. . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays of pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Passing arrays to functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding and subtracting pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . .

298 300 302 303 304 306 307

Planning and Building Objects . . . . . . . . . . . . . . . . . . . . . . .

309

Recognizing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Observing the Mailboxes class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Observing the Mailbox class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Finding other objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Encapsulating Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 Considering the Application Programming Interface. . . . . . . . . . . 316 Understanding properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Choosing between private and protected. . . . . . . . . . . . . . . . . . . . 318 Defining a process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Implementing properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Building Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Establishing a hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Protecting members when inheriting. . . . . . . . . . . . . . . . . . . . . . . . 324 Overriding methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Specializing with polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 Getting abstract about things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 CHAPTER 4:

Building with Design Patterns. . . . . . . . . . . . . . . . . . . . . . . .

335

Delving Into Pattern History. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Introducing a Simple Pattern: the Singleton. . . . . . . . . . . . . . . . . . . . . 337 Using an existing pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Creating a singleton pattern class. . . . . . . . . . . . . . . . . . . . . . . . . . . 338 Watching an Instance with an Observer . . . . . . . . . . . . . . . . . . . . . . . . 341 Understanding the observer pattern. . . . . . . . . . . . . . . . . . . . . . . . 341 Defining an observer pattern class. . . . . . . . . . . . . . . . . . . . . . . . . . 343 Observers and the Standard C++ Library. . . . . . . . . . . . . . . . . . . . .346 Automatically adding an observer . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Mediating with a Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Defining the mediator pattern scenario. . . . . . . . . . . . . . . . . . . . . .350 Outlining the car example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Creating the car example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

x

C++ All-in-One For Dummies

BOOK 3: UNDERSTANDING FUNCTIONAL PROGRAMMING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

367

Considering Functional Programming. . . . . . . . . . . . . .

369

CHAPTER 1:

Understanding How Functional Programming Differs . . . . . . . . . . . . 370 Defining an Impure Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Considering the requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Understanding the C++ functional limitations . . . . . . . . . . . . . . . . 374 Seeing Data as Immutable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Working with immutable variables. . . . . . . . . . . . . . . . . . . . . . . . . . 376 Working with immutability in classes and structures . . . . . . . . . . 377 Creating constant expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Considering the Effects of State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 Eliminating Side Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382 Contrasting declarations and functions. . . . . . . . . . . . . . . . . . . . . . 383 Associating functions with side effects. . . . . . . . . . . . . . . . . . . . . . . 384 Removing side effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Creating a declarative C++ example. . . . . . . . . . . . . . . . . . . . . . . . . 387 Understanding the Role of auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Passing Functions to Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 Seeing a simple example of function input. . . . . . . . . . . . . . . . . . . 391 Using transforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Using Lambda Expressions for Implementation . . . . . . . . . . . . . . . . . 394 CHAPTER 2:

CHAPTER 3:

Working with Lambda Expressions. . . . . . . . . . . . . . . . . .

397

Creating More Readable and Concise C++ Code . . . . . . . . . . . . . . . . . Defining the Essential Lambda Expression. . . . . . . . . . . . . . . . . . . . . . Defining the parts of a lambda expression. . . . . . . . . . . . . . . . . . . Relying on computer detection of return type . . . . . . . . . . . . . . . . Using the auto keyword with lambda expressions . . . . . . . . . . . . Using lambda expressions as macros . . . . . . . . . . . . . . . . . . . . . . . Developing with Lambda Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . Using lambda expressions with classes and structures . . . . . . . . Working with the capture clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . Sorting data using a lambda expression . . . . . . . . . . . . . . . . . . . . . Specifying that the lambda expression throws exceptions. . . . . .

398 399 399 401 404 405 406 407 408 411 413

Advanced Lambda Expressions. . . . . . . . . . . . . . . . . . . . . .

415

Considering the C++ 20 Lambda Extensions. . . . . . . . . . . . . . . . . . . . . Defining an immediate function. . . . . . . . . . . . . . . . . . . . . . . . . . . . Using = and this in captures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Finding other changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

416 416 417 418

Table of Contents

xi

Working in Unevaluated Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Assignable Stateless Lambda Expressions. . . . . . . . . . . . . . . . . Dealing with Pack Expansions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Considering the template. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Processing the variables using recursion. . . . . . . . . . . . . . . . . . . . . Processing the variables using a lambda expression . . . . . . . . . .

418 420 422 422 423 424

BOOK 4: FIXING PROBLEMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

427

CHAPTER 1:

CHAPTER 2:

CHAPTER 3:

CHAPTER 4:

Dealing with Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

429

It’s Not a Bug. It’s a Feature!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Make Your Application Features Look Like Features. . . . . . . . . . . . . . Anticipating (Almost) Everything. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Considering menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dealing with textual input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Performing string processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Avoiding Mistakes, Plain and Simple . . . . . . . . . . . . . . . . . . . . . . . . . . .

430 431 432 432 435 437 441

Debugging an Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

443

Programming with Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running the debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Recognizing the parts of the Code::Blocks debugger. . . . . . . . . . . Debugging with Different Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Debugging a Code::Blocks Application with Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

444 446 453 455

Stopping and Inspecting Your Code. . . . . . . . . . . . . . . . .

457

Setting and Disabling Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting a breakpoint in Code::Blocks . . . . . . . . . . . . . . . . . . . . . . . . Enabling and disabling breakpoints. . . . . . . . . . . . . . . . . . . . . . . . . Watching, Inspecting, and Changing Variables. . . . . . . . . . . . . . . . . . . Watching the variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

458 459 460 463 465 466

Traveling About the Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 Stacking Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Moving about the stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Storing local variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Debugging with Advanced Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . Viewing threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tracing through assembly code . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xii

456

C++ All-in-One For Dummies

470 471 473 475 475 475

BOOK 5: ADVANCED PROGRAMMING. . . . . . . . . . . . . . . . . . . . . . . CHAPTER 1:

CHAPTER 2:

479

Working with Arrays, Pointers, and References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Building Up Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Declaring arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays and pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using multidimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays and command-line parameters . . . . . . . . . . . . . . . . . . . . . . Allocating an array on the heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deleting an array from the heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . Storing arrays of pointers and arrays of arrays . . . . . . . . . . . . . . . Building constant arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointing with Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Becoming horribly complex. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointers to functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointing a variable to a method . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointing to static methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Referring to References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reference variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning a reference from a function. . . . . . . . . . . . . . . . . . . . . . .

482 482 484 488 492 494 494 495 498 498 499 505 506 509 510 510 511

Creating Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

515

Working with Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 The great variable roundup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516 Expressing variables from either side . . . . . . . . . . . . . . . . . . . . . . . 518 Casting a spell on your data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520 Comparing casting and converting. . . . . . . . . . . . . . . . . . . . . . . . . . 521 Casting safely with C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 Structuring Your Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 Structures as component data types. . . . . . . . . . . . . . . . . . . . . . . . 531 Equating structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531 Returning compound data types. . . . . . . . . . . . . . . . . . . . . . . . . . . .532 Naming Your Space. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534 Creating a namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534 Employing using namespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 Using variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Using part of a namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 CHAPTER 3:

Constructors, Destructors, and Exceptions. . . . . . . .

541

Constructing and Destructing Objects. . . . . . . . . . . . . . . . . . . . . . . . . . Overloading constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Initializing members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding a default constructor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functional constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

542 542 543 548 550

Table of Contents

xiii

CHAPTER 4:

CHAPTER 5:

xiv

Calling one constructor from another . . . . . . . . . . . . . . . . . . . . . . . Copying instances with copy constructors . . . . . . . . . . . . . . . . . . . When constructors go bad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Destroying your instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Virtually inheriting destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Programming the Exceptions to the Rule . . . . . . . . . . . . . . . . . . . . . . . Creating a basic try. . .catch block. . . . . . . . . . . . . . . . . . . . . . . . . . . Using multiple catch blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Throwing direct instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Catching any exception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Rethrowing an exception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a standard category. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

553 555 557 558 560 563 563 565 566 567 568 570

Advanced Class Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

571

Inherently Inheriting Correctly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Morphing your inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Avoiding polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adjusting access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Avoiding variable naming conflicts. . . . . . . . . . . . . . . . . . . . . . . . . . Using class-based access adjustment . . . . . . . . . . . . . . . . . . . . . . . Returning something different, virtually speaking. . . . . . . . . . . . . Multiple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Virtual inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Friend classes and functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Classes and Types within Classes. . . . . . . . . . . . . . . . . . . . . . . . . Nesting a class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Types within classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

572 572 573 574 575 576 577 581 584 588 591 591 597

Creating Classes with Templates . . . . . . . . . . . . . . . . . . . .

601

Templatizing a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Considering types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Defining the need for templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and using a template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding the template keyword. . . . . . . . . . . . . . . . . . . . . . . Going Beyond the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Separating a template from the function code. . . . . . . . . . . . . . . . Including static members in a template. . . . . . . . . . . . . . . . . . . . . . Parameterizing a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Putting different types in the parameter. . . . . . . . . . . . . . . . . . . . . Including multiple parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with non-type parameters . . . . . . . . . . . . . . . . . . . . . . . . .

602 602 602 605 607 609 609 611 612 613 616 619

C++ All-in-One For Dummies

CHAPTER 6:

Typedefing a Template. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deriving Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deriving classes from a class template . . . . . . . . . . . . . . . . . . . . . . Deriving a class template from a class. . . . . . . . . . . . . . . . . . . . . . . Deriving a class template from a class template . . . . . . . . . . . . . . Templatizing a Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overloading and function templates . . . . . . . . . . . . . . . . . . . . . . . . Templatizing a method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

622 623 623 626 627 630 632 635

Programming with the Standard Library . . . . . . . . . .

637

Architecting the Standard Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 Containing Your Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 Storing in a vector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639 Working with std::array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .642 Mapping your data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 Containing instances, pointers, or references . . . . . . . . . . . . . . . . 644 Working with copies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648 Comparing instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649 Iterating through a container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 A map of pairs in your hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658 The Great Container Showdown. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658 Associating and storing with a set. . . . . . . . . . . . . . . . . . . . . . . . . . . 658 Unionizing and intersecting sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 Listing with list. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664 Stacking the deque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 Waiting in line with stacks and queues . . . . . . . . . . . . . . . . . . . . . . 670 Copying Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673 Creating and Using Dynamic Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 Working with Unordered Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 Using std::unordered_set to create an unordered set. . . . . . . . . . 677 Manipulating unordered sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 Working with Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679

BOOK 6: READING AND WRITING FILES . . . . . . . . . . . . . . . . . . . .

681

Filing Information with the Streams Library . . . . . .

683

CHAPTER 1:

Seeing a Need for Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .684 Programming with the Streams Library. . . . . . . . . . . . . . . . . . . . . . . . . 686 Getting the right header file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686 Opening a file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687 Reading from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690 Reading and writing a file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691 Working with containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 Handling Errors When Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . 693 Flagging the ios Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

Table of Contents

xv

CHAPTER 2:

CHAPTER 3:

CHAPTER 4:

697

Inserting with the