You are previewing C++ All-In-One For Dummies®, 2nd Edition.
O'Reilly logo
C++ All-In-One For Dummies®, 2nd Edition

Book Description

Here’s the easy way to learn how to use C++

C++, developed by Bjarne Stroustrup at Bell Labs, is one of the most widely used programming languages, with close to four million C++ programmers and growing. C++’s popularity has earned it a spot as part of the Standard Library.

Fully updated for the new C++ 2009 standard, C++ All-in-One for Dummies, 2nd Edition compiles seven books into one. This guidebook covers key topics like an introduction to C++, understanding objects and classes, fixing problems, advanced programming, reading and writing files, advanced C++, and building applications with Microsoft MFC. If you're a C++ newbie, start with Book I. But if you’re experienced with C++, simply jump in anywhere to learn more! This all-in-one reference helps you learn to:

  • Use C++ for Windows, Mac, and Linux by using the CodeBlocks compiler

  • Understand object-oriented programming

  • Use various diagrams to design your programs

  • Recognize how local variables are stored

  • Use packages, notes, and tags effectively

  • Make a class persistent

  • Handle constructors and destructors

With over 25,000 sold of the previous bestselling edition, this second edition with a bonus CD makes C++ easier to understand. It’s a perfect introduction for new programmers and guide for advanced programmers.

Note: CD-ROM/DVD and other supplementary materials are not included as part of eBook file.

Table of Contents

  1. Copyright
  2. About the Authors
  3. Authors' Acknowledgments
  4. Publisher's Acknowledgments
  5. Introduction
    1. No Experience Necessary
    2. Great for Advanced Folks, Too!
    3. For All Computers
    4. Conventions
    5. Organization
    6. Icons Galore
    7. What's Next?
  6. I. Introducing C++
    1. 1. Creating a First C++ Program
      1. 1.1. Creating a Project
        1. 1.1.1. Understanding projects
        2. 1.1.2. Defining your first project
        3. 1.1.3. Building and executing your first application
      2. 1.2. Typing the Code
      3. 1.3. Starting with Main
      4. 1.4. Showing Information
        1. 1.4.1. Doing some math
          1. 1.4.1.1. Ordering the operations
          2. 1.4.1.2. Going overboard
          3. 1.4.1.3. Pairing the parentheses
        2. 1.4.2. Tabbing your output
      5. 1.5. Let Your Program Run Away
    2. 2. Storing Data in C++
      1. 2.1. Putting Your Data Places: Variables
        1. 2.1.1. Creating an integer variable
        2. 2.1.2. Declaring multiple variables
        3. 2.1.3. Changing values
        4. 2.1.4. Setting one variable equal to another
        5. 2.1.5. Initializing a variable
        6. 2.1.6. Creating a great name for yourself
      2. 2.2. Manipulating Integer Variables
        1. 2.2.1. Adding integer variables
        2. 2.2.2. Subtracting integer variables
        3. 2.2.3. Multiplying integer variables
        4. 2.2.4. Dividing integer variables
      3. 2.3. Characters
        1. 2.3.1. Null character
        2. 2.3.2. Nonprintable and other cool characters
      4. 2.4. Strings
        1. 2.4.1. Getting a part of a string
        2. 2.4.2. Changing part of a string
        3. 2.4.3. Adding onto a string
        4. 2.4.4. Adding two strings
      5. 2.5. Deciding between Conditional Operators
      6. 2.6. Telling the Truth with Boolean Variables
      7. 2.7. Reading from the Console
    3. 3. Directing Your C++ Program Flow
      1. 3.1. Doing This or Doing That
      2. 3.2. Evaluating Conditions in C++
        1. 3.2.1. Finding the right C++ operators
        2. 3.2.2. Combining multiple evaluations
      3. 3.3. Including Evaluations in C++ Conditional Statements
        1. 3.3.1. Deciding what if and also what else
        2. 3.3.2. Going further with the else and if
      4. 3.4. Repeating Actions with Statements That Loop
        1. 3.4.1. Looping situations
        2. 3.4.2. Looping for
          1. 3.4.2.1. Meddling with the middle condition
          2. 3.4.2.2. Going backwards
          3. 3.4.2.3. Incrementing one step at a time
          4. 3.4.2.4. Getting sneaky (and too complicated)
        3. 3.4.3. Looping while
        4. 3.4.4. Doing while
        5. 3.4.5. Breaking and continuing
          1. 3.4.5.1. Breaking
          2. 3.4.5.2. Continuing
        6. 3.4.6. Nesting loops
    4. 4. Dividing Your Work with Functions
      1. 4.1. Dividing Your Work
      2. 4.2. Calling a Function
        1. 4.2.1. Passing a variable
        2. 4.2.2. Passing multiple variables
      3. 4.3. Writing Your Own Functions
        1. 4.3.1. Multiple parameters or no parameters
        2. 4.3.2. Returning nothing
        3. 4.3.3. Keeping your variables local
        4. 4.3.4. Forward references and function prototypes
        5. 4.3.5. Writing two versions of the same function
      4. 4.4. Calling All String Functions
      5. 4.5. Understanding main
    5. 5. Dividing Between Source-Code Files
      1. 5.1. Creating Multiple Source Files
        1. 5.1.1. Multiple source files in CodeBlocks
        2. 5.1.2. Multiple source files in other compilers
        3. 5.1.3. Creating multiple files
      2. 5.2. Sharing with Header Files
        1. 5.2.1. Adding the header only once
        2. 5.2.2. Using brackets or quotes
      3. 5.3. Sharing Variables Among Source Files
      4. 5.4. Using the Mysterious Header Wrappers
    6. 6. Referring to Your Data Through Pointers
      1. 6.1. Heaping and Stacking the Variables
        1. 6.1.1. Getting a variable's address
        2. 6.1.2. Changing a variable by using a pointer
        3. 6.1.3. Pointing at a string
        4. 6.1.4. Pointing to something else
        5. 6.1.5. Tips on pointer variables
      2. 6.2. Dynamically Allocating with new
        1. 6.2.1. Using new
        2. 6.2.2. Using an initializer
        3. 6.2.3. Making new strings
      3. 6.3. Freeing Pointers
      4. 6.4. Passing Pointer Variables to Functions
        1. 6.4.1. Changing variable values with pointers
        2. 6.4.2. Modifying string parameters
      5. 6.5. Returning Pointer Variables from Functions
      6. 6.6. Returning a Pointer as a Nonpointer
      7. 6.7. Passing by Reference
      8. 6.8. Remembering the Rules
    7. 7. Working with Classes
      1. 7.1. Understanding Objects and Classes
        1. 7.1.1. Classifying classes and objects
        2. 7.1.2. Describing member functions and data
        3. 7.1.3. Implementing a class
        4. 7.1.4. Separating member function code
        5. 7.1.5. The parts of a class
      2. 7.2. Working with a Class
        1. 7.2.1. Accessing members
        2. 7.2.2. Using classes and pointers
        3. 7.2.3. Passing objects to functions
        4. 7.2.4. Using const parameters in functions
        5. 7.2.5. Using the this pointer
        6. 7.2.6. Overloading member functions
      3. 7.3. Starting and Ending with Constructors and Destructors
        1. 7.3.1. Starting with constructors
        2. 7.3.2. Ending with destructors
        3. 7.3.3. Sampling constructors and destructors
        4. 7.3.4. Adding parameters to constructors
      4. 7.4. Building Hierarchies of Classes
        1. 7.4.1. Creating a hierarchy in C++
        2. 7.4.2. Understanding types of inheritance
    8. 8. Using Advanced C++ Features
      1. 8.1. Filling Your Code with Comments
      2. 8.2. Converting Types
      3. 8.3. Reading from the Console
      4. 8.4. Understanding Preprocessor Directives
      5. 8.5. Using Constants
      6. 8.6. Using Switch Statements
      7. 8.7. Supercharging enums with Classes
      8. 8.8. Working with Random Numbers
      9. 8.9. Storing Data in Arrays
        1. 8.9.1. Declaring and accessing an array
        2. 8.9.2. Arrays of pointers
        3. 8.9.3. Passing arrays to functions
        4. 8.9.4. Adding and subtracting pointers
  7. II. Understanding Objects and Classes
    1. 1. Planning and Building Objects
      1. 1.1. Recognizing Objects
        1. 1.1.1. Observing the mailboxes class
        2. 1.1.2. Observing the mailbox class
        3. 1.1.3. Finding other objects
      2. 1.2. Encapsulating Objects
      3. 1.3. Building Hierarchies
        1. 1.3.1. Establishing a hierarchy
        2. 1.3.2. Protecting members when inheriting
        3. 1.3.3. Overriding member functions
        4. 1.3.4. Specializing with polymorphism
        5. 1.3.5. Getting abstract about things
      4. 1.4. Discovering Classes
        1. 1.4.1. Engineering your software
        2. 1.4.2. Finding those pesky classes
          1. 1.4.2.1. Searching for nouns and verbs
          2. 1.4.2.2. Using CRC cards
          3. 1.4.2.3. Look for hidden or missing classes
          4. 1.4.2.4. Completing the analysis with the design workflow
    2. 2. Describing Your Program with UML
      1. 2.1. Moving Up to UML
        1. 2.1.1. Modeling with UML
        2. 2.1.2. Diagramming and designing with UML
      2. 2.2. Building with UML and the Unified Process
        1. 2.2.1. Speaking iteratively
        2. 2.2.2. Phasing in and out
        3. 2.2.3. The inception phase
        4. 2.2.4. The elaboration phase
        5. 2.2.5. The construction phase
        6. 2.2.6. The transition phase
      3. 2.3. Moving Forward with UML
    3. 3. Structuring Your Classes with UML
      1. 3.1. Drawing Classes
        1. 3.1.1. Mapping classes with UML
        2. 3.1.2. Inheriting in UML
        3. 3.1.3. Aggregating and composing classes
      2. 3.2. Building Components
      3. 3.3. Deploying the Software
    4. 4. Demonstrating Behavior with UML
      1. 4.1. Drawing Objects
      2. 4.2. Casing Out the Use Cases
        1. 4.2.1. Expanding use cases
        2. 4.2.2. Matching use cases and requirements
      3. 4.3. Sequence Diagrams
        1. 4.3.1. Notating sequence diagrams
        2. 4.3.2. Looping and comparing in sequence diagrams
      4. 4.4. Collaboration Diagrams
      5. 4.5. Activity Diagrams
      6. 4.6. State Diagrams
    5. 5. Modeling Your Programs with UML
      1. 5.1. Using UML Goodies
        1. 5.1.1. Packaging your symbols
        2. 5.1.2. Notating your diagrams
        3. 5.1.3. Tagging your symbols
      2. 5.2. Free to Be UML
      3. 5.3. C++ and UML
        1. 5.3.1. Drawing enumerations
        2. 5.3.2. Including static members
        3. 5.3.3. Parameterizing classes with templates
    6. 6. Building with Design Patterns
      1. 6.1. Introducing a Simple Pattern: the Singleton
      2. 6.2. Watching an Instance with an Observer
        1. 6.2.1. Observers and the Standard C++ Library
        2. 6.2.2. Automatically adding an observer
      3. 6.3. Mediating with a Pattern
  8. III. Fixing Problems
    1. 1. Dealing with Bugs
      1. 1.1. It's Not a Bug. It's a Feature!
      2. 1.2. Make Your Programming Features Look Like Features
      3. 1.3. Anticipating (Almost) Everything
      4. 1.4. Avoiding Mistakes, Plain and Simple
    2. 2. Debugging a Program
      1. 2.1. Programming with Debuggers
        1. 2.1.1. Running the debugger
        2. 2.1.2. Recognizing the parts of the CodeBlocks debugger
      2. 2.2. Debugging with Different Tools
        1. 2.2.1. Standard debuggers
    3. 3. Stopping and Inspecting Your Code
      1. 3.1. Setting and Disabling Breakpoints
        1. 3.1.1. Setting a breakpoint in CodeBlocks
        2. 3.1.2. Enabling and disabling breakpoints
      2. 3.2. Watching, Inspecting, and Changing Variables
        1. 3.2.1. Watching the local variables
        2. 3.2.2. Watching other variables
    4. 4. Traveling About the Stack
      1. 4.1. Stacking Your Data
        1. 4.1.1. Moving about the stack
        2. 4.1.2. Storing local variables
      2. 4.2. Debugging with Advanced Features
        1. 4.2.1. Viewing threads
        2. 4.2.2. Tracing through assembly code
  9. IV. Advanced Programming
    1. 1. Working with Arrays, Pointers, and References
      1. 1.1. Building Up Arrays
        1. 1.1.1. Declaring arrays
        2. 1.1.2. Arrays and pointers
        3. 1.1.3. Using multidimensional arrays
          1. 1.1.3.1. Initializing multidimensional arrays
          2. 1.1.3.2. Passing multidimensional arrays
        4. 1.1.4. Arrays and command-line parameters
        5. 1.1.5. Allocating an array on the heap
        6. 1.1.6. Storing arrays of pointers and arrays of arrays
        7. 1.1.7. Building constant arrays
      2. 1.2. Pointing with Pointers
        1. 1.2.1. Becoming horribly complex
        2. 1.2.2. Pointers to functions
        3. 1.2.3. Pointing a variable to a member function
        4. 1.2.4. Pointing to static member functions
      3. 1.3. Referring to References
        1. 1.3.1. Reference variables
        2. 1.3.2. Returning a reference from a function
    2. 2. Creating Data Structures
      1. 2.1. Working with Data
        1. 2.1.1. The great variable roundup
        2. 2.1.2. Expressing variables from either side
        3. 2.1.3. Casting a spell on your data
        4. 2.1.4. Casting safely with C++
        5. 2.1.5. Dynamically casting with dynamic_cast
        6. 2.1.6. Statically casting with static_cast
      2. 2.2. Structuring Your Data
        1. 2.2.1. Structures as component data types
        2. 2.2.2. Equating structures
        3. 2.2.3. Returning compound data types
      3. 2.3. Naming Your Space
        1. 2.3.1. Using variables and part of a namespace
    3. 3. Constructors, Destructors, and Exceptions
      1. 3.1. Constructing and Destructing Objects
        1. 3.1.1. Overloading constructors
        2. 3.1.2. Initializing members
        3. 3.1.3. Adding a default constructor
        4. 3.1.4. Functional constructors
        5. 3.1.5. Calling one constructor from another
        6. 3.1.6. Copying instances with copy constructors
        7. 3.1.7. When constructors go bad: failable constructors?
        8. 3.1.8. Destroying your instances
        9. 3.1.9. Virtually inheriting destructors
      2. 3.2. Programming the Exceptions to the Rule
        1. 3.2.1. Throwing direct instances
        2. 3.2.2. Catching any exception
        3. 3.2.3. Rethrowing an exception
    4. 4. Advanced Class Usage
      1. 4.1. Inherently Inheriting Correctly
        1. 4.1.1. Morphing your inheritance
        2. 4.1.2. Adjusting access
        3. 4.1.3. Returning something different, virtually speaking
        4. 4.1.4. Multiple inheritance
        5. 4.1.5. Virtual inheritance
        6. 4.1.6. Friend classes and functions
      2. 4.2. Using Classes and Types within Classes
        1. 4.2.1. Nesting a class
        2. 4.2.2. Types within classes
    5. 5. Creating Classes with Templates
      1. 5.1. Templatizing a Class
        1. 5.1.1. Separating a template from the function code
        2. 5.1.2. Including static members in a template
      2. 5.2. Parameterizing a Template
        1. 5.2.1. Putting different types in the parameter
        2. 5.2.2. Including multiple parameters
      3. 5.3. Typedefing a Template
      4. 5.4. Deriving Templates
        1. 5.4.1. Deriving classes from a class template
        2. 5.4.2. Deriving a class template from a class
        3. 5.4.3. Deriving a class template from a class template
      5. 5.5. Templatizing a Function
        1. 5.5.1. Overloading and function templates
        2. 5.5.2. Templatizing a member function
    6. 6. Programming with the Standard Library
      1. 6.1. Architecting the Standard Library
      2. 6.2. Containing Your Classes
        1. 6.2.1. Storing in a vector
        2. 6.2.2. Mapping your data
        3. 6.2.3. Containing instances, pointers, or references
        4. 6.2.4. Comparing instances
        5. 6.2.5. Iterating through a container
        6. 6.2.6. A map of pairs in your hand
      3. 6.3. The Great Container Showdown
        1. 6.3.1. Associating and storing with a set
        2. 6.3.2. Unionizing and intersecting sets
        3. 6.3.3. Listing with list
        4. 6.3.4. Stacking the deque
        5. 6.3.5. Waiting in line with stacks and queues
      4. 6.4. Copying Containers
  10. V. Reading and Writing Files
    1. 1. Filing Information with the Streams Library
      1. 1.1. Seeing a Need for Streams
      2. 1.2. Programming with the Streams Library
        1. 1.2.1. Getting the right header file
        2. 1.2.2. Opening a file
      3. 1.3. Handling Errors When Opening a File
      4. 1.4. Flagging the ios Flags
    2. 2. Writing with Output Streams
      1. 2.1. Inserting with the << Operator
      2. 2.2. Formatting Your Output
        1. 2.2.1. Formatting with flags
        2. 2.2.2. Specifying a precision
        3. 2.2.3. Setting the width and creating fields
    3. 3. Reading with Input Streams
      1. 3.1. Extracting with Operators
      2. 3.2. Encountering the End of File
      3. 3.3. Reading Various Types
      4. 3.4. Reading Formatted Input
    4. 4. Building Directories and Contents
      1. 4.1. Manipulating Directories
        1. 4.1.1. Creating a directory
        2. 4.1.2. Deleting a directory
      2. 4.2. Getting the Contents of a Directory
      3. 4.3. Copying Files
      4. 4.4. Moving and Renaming Files and Directories
    5. 5. Streaming Your Own Classes
      1. 5.1. Streaming a Class for Text Formatting
      2. 5.2. Manipulating a Stream
        1. 5.2.1. What's a manipulator?
        2. 5.2.2. Writing your own manipulator
  11. VI. Advanced C++
    1. 1. Exploring the Standard Library Further
      1. 1.1. Considering the Standard Library Categories
        1. 1.1.1. Containers
        2. 1.1.2. Iterators
        3. 1.1.3. Algorithms
        4. 1.1.4. Functors
        5. 1.1.5. Utilities
        6. 1.1.6. Adaptors
        7. 1.1.7. Allocators
      2. 1.2. Parsing Strings Using a Hash
      3. 1.3. Obtaining Information Using a Random Access Iterator
      4. 1.4. Locating Values Using the Find Algorithm
      5. 1.5. Using the Random Number Generator
      6. 1.6. Performing Comparisons Using min and max
      7. 1.7. Working with Temporary Buffers
    2. 2. Building Original Templates
      1. 2.1. Deciding When to Create a Template
      2. 2.2. Defining the Elements of a Good Template
      3. 2.3. Creating a Basic Math Template
      4. 2.4. Building a Structure Template
      5. 2.5. Developing a Class Template
      6. 2.6. Considering Template Specialization
      7. 2.7. Creating a Template Library
        1. 2.7.1. Defining the library project
        2. 2.7.2. Configuring the library project
        3. 2.7.3. Coding the library
      8. 2.8. Using Your Template Library
    3. 3. Investigating Boost
      1. 3.1. Understanding Boost
        1. 3.1.1. Boost features
        2. 3.1.2. Licensing
        3. 3.1.3. Paid support
      2. 3.2. Obtaining and Installing Boost for CodeBlocks
        1. 3.2.1. Unpacking Boost
        2. 3.2.2. Building the libraries
        3. 3.2.3. Building the Boost tools
      3. 3.3. Using Boost Jam
      4. 3.4. Using Boost Build
      5. 3.5. Using Regression
      6. 3.6. Using Inspect
      7. 3.7. Understanding BoostBook
      8. 3.8. Using QuickBook
      9. 3.9. Using bcp
      10. 3.10. Using Wave
      11. 3.11. Building Your First Boost Application Using Date Time
    4. 4. Boosting up a Step
      1. 4.1. Parsing Strings Using RegEx
        1. 4.1.1. Adding the RegEx library
        2. 4.1.2. Creating the RegEx code
      2. 4.2. Breaking Strings into Tokens Using Tokenizer
      3. 4.3. Performing Numeric Conversion
      4. 4.4. Creating Improved Loops Using Foreach
      5. 4.5. Accessing the Operating System Using Filesystem
  12. VII. Building Applications with Mircosoft MFC
    1. 1. Working with the Visual C++ 2008 IDE and Projects
      1. 1.1. Understanding the Project Types
      2. 1.2. Creating a New Win32 Console Application
        1. 1.2.1. Defining the project
        2. 1.2.2. Adding code
        3. 1.2.3. Running the application
      3. 1.3. Writing Code Faster
        1. 1.3.1. Obtaining coding help
        2. 1.3.2. Working with IntelliSense
      4. 1.4. Viewing Your Project in Solution Explorer
      5. 1.5. Using the Standard Toolbars
      6. 1.6. Changing Application Properties
      7. 1.7. Modifying the IDE Appearance
        1. 1.7.1. Changing toolbars and menus
        2. 1.7.2. Modifying windows
    2. 2. Creating an MFC Dialog Box Project
      1. 2.1. Understanding the MFC Dialog Box Project
      2. 2.2. Creating the MFC Dialog Box Project
      3. 2.3. Adding Components and Controls
        1. 2.3.1. Adding the component or control
        2. 2.3.2. Creating variables to use in your code
        3. 2.3.3. Defining methods to react to control events
      4. 2.4. Defining the Dialog Box Code
      5. 2.5. Understanding the Essential Windows
        1. 2.5.1. Working with Class View
        2. 2.5.2. Modifying the Toolbox
    3. 3. Creating an MFC SDI Project
      1. 3.1. Understanding the MFC SDI Project
      2. 3.2. Creating the MFC SDI Project
      3. 3.3. Understanding the Document/View Architecture
      4. 3.4. Adding Code to Your SDI Project
        1. 3.4.1. An overview of the essential application files
        2. 3.4.2. Locating Microsoft specified suggested changes
        3. 3.4.3. Making resource changes
        4. 3.4.4. Considering the help file
        5. 3.4.5. Registering and unregistering the application
      5. 3.5. Seeing the Text Editor Project in Action
    4. 4. Using the Visual C++ 2008 Debugger
      1. 4.1. A Quick Look at the Error Application
      2. 4.2. Starting Your Application in Debugging Mode
        1. 4.2.1. Creating the proper build
        2. 4.2.2. Setting breakpoints
        3. 4.2.3. Viewing your breakpoints
        4. 4.2.4. Starting the debugger
      3. 4.3. Working with the Debugging Windows
        1. 4.3.1. Viewing the focus variables using the Autos window
        2. 4.3.2. Viewing all of the variables in a method using the Locals window
        3. 4.3.3. Screening specific variables using the Watch 1 window
        4. 4.3.4. Working with the Call Stack window
    5. 5. Analyzing Your Visual C++ 2008 Code
      1. 5.1. Using Performance Wizard
      2. 5.2. Profiling the Code
  13. A. Automating Your Programs with Makefiles
    1. A.1. Compiling and Linking
    2. A.2. Automating Your Work
    3. A.3. Implying with Inference Rules
      1. A.3.1. Using rules that depend on other rules
      2. A.3.2. Making specific items
      3. A.3.3. Depending on multiple files
      4. A.3.4. Compiling and linking with make
      5. A.3.5. Cleaning up and making it all
      6. A.3.6. Using macros
      7. A.3.7. Getting the most out of Makefiles
  14. B. About the CD
    1. B.1. System Requirements
    2. B.2. Using the CD
    3. B.3. What You'll Find on the CD
      1. B.3.1. CodeBlocks Compiler
      2. B.3.2. Author-created material
    4. B.4. Troubleshooting
    5. B.5. Customer Care