You are previewing C++ For Dummies®, 6th Edition.
O'Reilly logo
C++ For Dummies®, 6th Edition

Book Description

Enter the world of computer programming with this step-by-step guide to the C++ language! C++ is a great introduction to object-oriented programming, and this friendly guide covers everything you need to know and nothing you don’t. You’ll write your first program by the end of Chapter 1.

C++ For Dummies, 6th Edition, helps you understand C++ programming from the ground up. It’s full of examples to show you how things work, and it even explains “why”, so you understand how the pieces fit together. And the bonus CD includes a special code editor, an update GNU compiler, and all source code from the book to save you time.

  • Learn programming lingo and what terms like object-oriented, compiler, and executable mean, so you can write a program right away

  • See how to bundle sections of your code into modules that can be reused in different programs

  • Work with features of object-oriented programming such as classes, constructors, and destructors

  • Discover how the concept of inheritance is the key to effective C++ programming

  • Work with assignment operators, stream I/O, and other more advanced concepts, once you’ve grasped the basics

You’ll discover ten ways to avoid adding bugs to your programs, what pointers are and how to use them, how to work with strings, and some advanced features new to C++. C++ For Dummies, 6th Edition gets you up and running with this popular object-oriented language.

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

Table of Contents

  1. Copyright
  2. About the Author
  3. Acknowledgments
  4. Publisher's Acknowledgments
  5. Introduction
    1. What's in This Book
    2. What's on the CD
    3. What Is C++?
    4. Conventions Used in This Book
    5. How This Book Is Organized
    6. And There's More
    7. Part I: Introduction to C++ Programming
    8. Part II: Becoming a Functional C++ Programmer
    9. Part III: Introduction to Classes
    10. Part IV: Inheritance
    11. Part V: Optional Features
    12. Part VI: The Part of Tens
    13. Icons Used in This Book
    14. Where to Go from Here
  6. I. Introduction to C++ Programming
    1. 1. Writing Your First C++ Program
      1. 1.1. Grasping C++ Concepts
      2. 1.2. Installing Code::Blocks
      3. 1.3. Creating Your First C++ Program
        1. 1.3.1. Creating a project
        2. 1.3.2. Entering the C++ code
        3. 1.3.3. Cheating
        4. 1.3.4. Building your program
      4. 1.4. Executing Your Program
      5. 1.5. Reviewing the Annotated Program
        1. 1.5.1. Examining the framework for all C++ programs
        2. 1.5.2. Clarifying source code with comments
        3. 1.5.3. Basing programs on C++ statements
        4. 1.5.4. Writing declarations
        5. 1.5.5. Generating output
      6. 1.6. Calculating Expressions
        1. 1.6.1. Storing the results of an expression
        2. 1.6.2. Examining the remainder of Conversion
    2. 2. Declaring Variables Constantly
      1. 2.1. Declaring Variables
      2. 2.2. Declaring Different Types of Variables
        1. 2.2.1. Reviewing the limitations of integers in C++
          1. 2.2.1.1. Integer round-off
          2. 2.2.1.2. Limited range
        2. 2.2.2. Solving the truncation problem
        3. 2.2.3. Looking at the limits of floating-point numbers
          1. 2.2.3.1. Counting
          2. 2.2.3.2. Calculation speed
          3. 2.2.3.3. Loss of accuracy
          4. 2.2.3.4. Not-so-limited range
      3. 2.3. Declaring Variable Types
        1. 2.3.1. Types of constants
        2. 2.3.2. Range of Numeric Types
        3. 2.3.3. Special characters
      4. 2.4. Wide Loads on Char Highway
      5. 2.5. Are These Calculations Really Logical?
      6. 2.6. Mixed Mode Expressions
      7. 2.7. Automatic Declarations
    3. 3. Performing Mathematical Operations
      1. 3.1. Performing Simple Binary Arithmetic
      2. 3.2. Decomposing Expressions
      3. 3.3. Determining the Order of Operations
      4. 3.4. Performing Unary Operations
      5. 3.5. Using Assignment Operators
    4. 4. Performing Logical Operations
      1. 4.1. Why Mess with Logical Operations?
      2. 4.2. Using the Simple Logical Operators
        1. 4.2.1. Storing logical values
        2. 4.2.2. Using logical int variables
        3. 4.2.3. Be careful performing logical operations on floating-point variables
          1. 4.2.3.1. Short circuits and C++
      3. 4.3. Expressing Binary Numbers
        1. 4.3.1. The decimal number system
        2. 4.3.2. Other number systems
        3. 4.3.3. The binary number system
      4. 4.4. Performing Bitwise Logical Operations
        1. 4.4.1. The single-bit operators
        2. 4.4.2. Using the bitwise operators
        3. 4.4.3. A simple test
        4. 4.4.4. Do something logical with logical calculations
    5. 5. Controlling Program Flow
      1. 5.1. Controlling Program Flow with the Branch Commands
      2. 5.2. Executing Loops in a Program
        1. 5.2.1. Looping while a condition is true
        2. 5.2.2. Using the autoincrement/autodecrement feature
        3. 5.2.3. Using the for loop
        4. 5.2.4. Avoiding the dreaded infinite loop
        5. 5.2.5. Applying special loop controls
      3. 5.3. Nesting Control Commands
      4. 5.4. Switching to a Different Subject?
  7. II. Becoming a Functional C++ Programmer
    1. 6. Creating Functions
      1. 6.1. Writing and Using a Function
        1. 6.1.1. Defining our first function
        2. 6.1.2. Defining the sumSequence() function
        3. 6.1.3. Calling the function sumSequence()
        4. 6.1.4. Divide and conquer
      2. 6.2. Understanding the Details of Functions
        1. 6.2.1. Understanding simple functions
        2. 6.2.2. Understanding functions with arguments
          1. 6.2.2.1. Functions with arguments
          2. 6.2.2.2. Functions with multiple arguments
          3. 6.2.2.3. main() exposed
      3. 6.3. Overloading Function Names
      4. 6.4. Defining Function Prototypes
      5. 6.5. Variable Storage Types
    2. 7. Storing Sequences in Arrays
      1. 7.1. Arraying the Arguments for Arrays
        1. 7.1.1. Using an array
        2. 7.1.2. Initializing an array
        3. 7.1.3. Accessing too far into an array
        4. 7.1.4. Using arrays
        5. 7.1.5. Defining and using arrays of arrays
      2. 7.2. Using Arrays of Characters
        1. 7.2.1. Creating an array of characters
        2. 7.2.2. Creating a string of characters
      3. 7.3. Manipulating Strings with Character
      4. 7.4. Adding Some Library Functions
      5. 7.5. Making Room for Wide Strings
    3. 8. Taking a First Look at C++ Pointers
      1. 8.1. Variable Size
      2. 8.2. What's in an Address?
      3. 8.3. Address Operators
      4. 8.4. Using Pointer Variables
        1. 8.4.1. Using different types of pointers
      5. 8.5. Passing Pointers to Functions
        1. 8.5.1. Passing by value
        2. 8.5.2. Passing pointer values
        3. 8.5.3. Passing by reference
      6. 8.6. Constant const Irritation
      7. 8.7. Making Use of a Block of Memory Called the Heap
        1. 8.7.1. Limited scope
        2. 8.7.2. Examining the scope problem
        3. 8.7.3. Providing a solution using the heap
    4. 9. Taking a Second Look at C++ Pointers
      1. 9.1. Defining Operations on Pointer Variables
        1. 9.1.1. Reexamining arrays in light of pointer variables
        2. 9.1.2. Applying operators to the address of an array
        3. 9.1.3. Expanding pointer operations to a string
        4. 9.1.4. Justifying pointer-based string manipulation
        5. 9.1.5. Applying operators to pointer types other than char
        6. 9.1.6. Contrasting a pointer with an array
      2. 9.2. When Is a Pointer Not?
      3. 9.3. Declaring and Using Arrays of Pointers
        1. 9.3.1. Utilizing arrays of character strings
        2. 9.3.2. Accessing the arguments to main()
          1. 9.3.2.1. Accessing program arguments DOS style
          2. 9.3.2.2. Accessing program arguments Code::Blocks style
          3. 9.3.2.3. Accessing program arguments Windows-style
    5. 10. The C++ Preprocessor
      1. 10.1. What Is a Preprocessor?
      2. 10.2. Including Files
      3. 10.3. #Defining Things
        1. 10.3.1. Okay, how about not #defining things?
        2. 10.3.2. Enumerating other options
      4. 10.4. Including Things #if I Say So
      5. 10.5. Intrinsically Defined Objects
      6. 10.6. Typedef
  8. III. Introduction to Classes
    1. 11. Examining Object-Oriented Programming
      1. 11.1. Abstracting Microwave Ovens
        1. 11.1.1. Preparing functional nachos
        2. 11.1.2. Preparing object-oriented nachos
      2. 11.2. Classifying Microwave Ovens
      3. 11.3. Why Classify?
    2. 12. Adding Class to C++
      1. 12.1. Introducing the Class
      2. 12.2. The Format of a Class
      3. 12.3. Accessing the Members of a Class
      4. 12.4. Activating Our Objects
        1. 12.4.1. Simulating real-world objects
        2. 12.4.2. Why bother with member functions?
      5. 12.5. Adding a Member Function
      6. 12.6. Calling a Member Function
        1. 12.6.1. Accessing other members from a member function
      7. 12.7. Scope Resolution (And I Don't Mean How Well Your Microscope Works)
      8. 12.8. Defining a Member Function in the Class
      9. 12.9. Keeping a Member Function After Class
      10. 12.10. Overloading Member Functions
    3. 13. Point and Stare at Objects
      1. 13.1. Declaring Arrays of Objects
      2. 13.2. Declaring Pointers to Objects
        1. 13.2.1. Dereferencing an object pointer
        2. 13.2.2. Pointing toward arrow pointers
      3. 13.3. Passing Objects to Functions
        1. 13.3.1. Calling a function with an object value
        2. 13.3.2. Calling a function with an object pointer
        3. 13.3.3. Calling a function by using the reference operator
      4. 13.4. Why Bother with Pointers or References?
      5. 13.5. Returning to the Heap
        1. 13.5.1. Allocating heaps of objects
      6. 13.6. Comparing Pointers to References
      7. 13.7. Linking Up with Linked Lists
        1. 13.7.1. Performing other operations on a linked list
        2. 13.7.2. Hooking up with a LinkedListData program
      8. 13.8. A Ray of Hope: A List of Containers Linked to the C++ Library
    4. 14. Protecting Members: Do Not Disturb
      1. 14.1. Protecting Members
        1. 14.1.1. Why you need protected members
        2. 14.1.2. Discovering how protected members work
      2. 14.2. Making an Argument for Using Protected Members
        1. 14.2.1. Protecting the internal state of the class
        2. 14.2.2. Using a class with a limited interface
      3. 14.3. Giving Nonmember Functions Access to Protected Members
    5. 15. Why Do You Build Me Up, Just to Tear Me Down Baby?
      1. 15.1. Creating Objects
      2. 15.2. Using Constructors
        1. 15.2.1. Constructing a single object
        2. 15.2.2. Constructing multiple objects
        3. 15.2.3. Constructing a duplex
      3. 15.3. Dissecting a Destructor
        1. 15.3.1. Why you need the destructor
        2. 15.3.2. Working with destructors
    6. 16. Making Constructive Arguments
      1. 16.1. Outfitting Constructors with Arguments
        1. 16.1.1. Using a constructor
      2. 16.2. Placing Too Many Demands on the Carpenter: Overloading the Constructor
      3. 16.3. Defaulting Default Constructors
      4. 16.4. Constructing Class Members
        1. 16.4.1. Constructing a complex data member
        2. 16.4.2. Constructing a constant data member
      5. 16.5. Reconstructing the Order of Construction
        1. 16.5.1. Local objects construct in order
        2. 16.5.2. Static objects construct only once
        3. 16.5.3. All global objects construct before main()
        4. 16.5.4. Global objects construct in no particular order
        5. 16.5.5. Members construct in the order in which they are declared
        6. 16.5.6. Destructors destruct in the reverse order of the constructors
      6. 16.6. Constructors as a Form of Conversion
    7. 17. Copying the Copy Copy Copy Constructor
      1. 17.1. Copying an Object
        1. 17.1.1. Why you need the copy constructor
        2. 17.1.2. Using the copy constructor
      2. 17.2. The Automatic Copy Constructor
      3. 17.3. Creating Shallow Copies versus Deep Copies
      4. 17.4. It's a Long Way to Temporaries
        1. 17.4.1. Avoiding temporaries, permanently
    8. 18. Static Members: Can Fabric Softener Help?
      1. 18.1. Defining a Static Member
        1. 18.1.1. Why you need static members
        2. 18.1.2. Using static members
        3. 18.1.3. Referencing static data members
        4. 18.1.4. Uses for static data members
      2. 18.2. Declaring Static Member Functions
      3. 18.3. What Is This About Anyway?
  9. IV. Inheritance
    1. 19. Inheriting a Class
      1. 19.1. Do I Need My Inheritance?
      2. 19.2. How Does a Class Inherit?
        1. 19.2.1. Using a subclass
        2. 19.2.2. Constructing a subclass
        3. 19.2.3. Destructing a subclass
      3. 19.3. Having a HAS_A Relationship
    2. 20. Examining Virtual Member Functions: Are They for Real?
      1. 20.1. Why You Need Polymorphism
      2. 20.2. How Polymorphism Works
      3. 20.3. When Is a Virtual Function Not?
      4. 20.4. Considering Virtual Considerations
    3. 21. Factoring Classes
      1. 21.1. Factoring
      2. 21.2. Implementing Abstract Classes
        1. 21.2.1. Describing the abstract class concept
        2. 21.2.2. Making an honest class out of an abstract class
        3. 21.2.3. Passing abstract classes
  10. V. Optional Features
    1. 22. A New Assignment Operator, Should You Decide to Accept It
      1. 22.1. Comparing Operators with Functions
      2. 22.2. Inserting a New Operator
      3. 22.3. Creating Shallow Copies Is a Deep Problem
      4. 22.4. Overloading the Assignment Operator
      5. 22.5. Overloading the Subscript Operator
    2. 23. Using Stream I/O
      1. 23.1. How Stream I/O Works
        1. 23.1.1. Default stream objects
      2. 23.2. Stream Input/Output
        1. 23.2.1. Open modes
        2. 23.2.2. Hey, file, what state are you in?
        3. 23.2.3. Can you show me an example?
      3. 23.3. Other Methods of the Stream Classes
        1. 23.3.1. Reading and writing streams directly
        2. 23.3.2. Controlling format
        3. 23.3.3. What's up with endl?
        4. 23.3.4. Positioning the pointer within a file
      4. 23.4. Using the stringstream Subclasses
      5. 23.5. Manipulating Manipulators
    3. 24. Handling Errors — Exceptions
      1. 24.1. Justifying a New Error Mechanism?
      2. 24.2. Examining the Exception Mechanism
      3. 24.3. What Kinds of Things Can I Throw?
      4. 24.4. Just Passing Through
    4. 25. Inheriting Multiple Inheritance
      1. 25.1. Describing the Multiple Inheritance Mechanism
      2. 25.2. Straightening Out Inheritance Ambiguities
      3. 25.3. Adding Virtual Inheritance
      4. 25.4. Constructing the Objects of Multiple Inheritance
      5. 25.5. Voicing a Contrary Opinion
    5. 26. Tempting C++ Templates
      1. 26.1. Generalizing a Function into a Template
      2. 26.2. Template Classes
      3. 26.3. Tips for Using Templates
    6. 27. Standardizing on the Standard Template Library
      1. 27.1. The string Container
      2. 27.2. Iterating through Lists
        1. 27.2.1. Making your way through a list
        2. 27.2.2. Operations on an entire list
        3. 27.2.3. Can you show me an example?
  11. VI. The Part of Tens
    1. 28. Ten Ways to Avoid Adding Bugs to Your Program
      1. 28.1. Enable All Warnings and Error Messages
      2. 28.2. Adopt a Clear and Consistent Coding Style
      3. 28.3. Limit the Visibility
      4. 28.4. Comment Your Code While You Write It
      5. 28.5. Single-Step Every Path at Least Once
      6. 28.6. Avoid Overloading Operators
      7. 28.7. Manage the Heap Systematically
      8. 28.8. Use Exceptions to Handle Errors
      9. 28.9. Declare Destructors Virtual
      10. 28.10. Avoid Multiple Inheritance
    2. 29. Ten Major Recent Additions to C++
      1. 29.1. Use Smart Pointers
      2. 29.2. Initialize Variables with a Variable-Length List
      3. 29.3. Initialize Data Members Inline
      4. 29.4. Instantiate an Extern Template
      5. 29.5. Implement Thread Local Storage
      6. 29.6. Use Rvalue References
      7. 29.7. Implement Concepts
      8. 29.8. Define Lamda Expressions
      9. 29.9. Define Variadic Templates
      10. 29.10. Use typeid()
  12. A. About the CD
    1. A.1. System Requirements
    2. A.2. Using the CD
    3. A.3. What You'll Find on the CD
      1. A.3.1. CPP programs
      2. A.3.2. Code::Blocks development environment
    4. A.4. Troubleshooting
    5. A.5. Customer Care