You are previewing Beginning Programming with C++ For Dummies®.
O'Reilly logo
Beginning Programming with C++ For Dummies®

Book Description

An ideal starting point to get a strong grasp of the fundamentals of C++

C++ is an object-oriented programming language commonly adopted by would-be programmers. This book explores the basic development concepts and techniques of C++ and explains the "how" and "why" of C++ programming from the ground up.

You'll discover what goes into creating a program, as well as how to put the various pieces together, deal with standard programming challenges, handle debugging, and make it all work.

  • Details the basics of C++ programming and explores the "how" and "why" of this object-oriented language

  • Addresses the various components that go into creating a program with C++

  • Walks you through common challenges of C++ programming

Assuming no prior experience, Beginning Programming with C++ For Dummies is a fun and friendly guide to learning the C++ 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. Author's Acknowledgments
  4. Publisher's Acknowledgments
  5. Introduction
    1. About Beginning Programming with C++ For Dummies
    2. Foolish Assumptions
    3. Conventions Used in This Book
    4. What You Don't Have to Read
    5. How This Book Is Organized
      1. Part I: Let's Get Started
      2. Part II: Writing a Program: Decisions, Decisions
      3. Part III: Becoming a Functional Programmer
      4. Part IV: Data Structures
      5. Part V: Object-Oriented Programming
      6. Part VI: Advanced Strokes
      7. Part VII: The Part of Tens
      8. The CD-ROM Appendix
    6. Icons Used in This Book
    7. Where to Go from Here
  6. I. Let's Get Started
    1. 1. What Is a Program?
      1. 1.1. How Does My Son Differ from a Computer?
      2. 1.2. Programming a "Human Computer"
        1. 1.2.1. The algorithm
        2. 1.2.2. The Tire Changing Language
        3. 1.2.3. The program
        4. 1.2.4. Computer processors
      3. 1.3. Computer Languages
        1. 1.3.1. High level languages
        2. 1.3.2. The C++ language
    2. 2. Installing Code::Blocks
      1. 2.1. Reviewing the Compilation Process
      2. 2.2. Installing Code::Blocks
      3. 2.3. Testing the Code::Blocks Installation
        1. 2.3.1. Creating the project
        2. 2.3.2. Testing your default project
    3. 3. Writing Your First Program
      1. 3.1. Creating a New Project
      2. 3.2. Entering Your Program
      3. 3.3. Building the Program
      4. 3.4. Finding What Could Go Wrong
        1. 3.4.1. Misspelled commands
        2. 3.4.2. Missing semicolon
      5. 3.5. Using the Enclosed CD-ROM
      6. 3.6. Running the Program
      7. 3.7. How the Program Works
        1. 3.7.1. The template
          1. 3.7.1.1. Comments
          2. 3.7.1.2. Include files
          3. 3.7.1.3. main
        2. 3.7.2. The Conversion program
  7. II. Writing a Program: Decisions, Decisions
    1. 4. Integer Expressions
      1. 4.1. Declaring Variables
        1. 4.1.1. Variable names
        2. 4.1.2. Assigning a value to a variable
        3. 4.1.3. Initializing a variable at declaration
      2. 4.2. Integer Constants
      3. 4.3. Expressions
        1. 4.3.1. Binary operators
        2. 4.3.2. Decomposing compound expressions
      4. 4.4. Unary Operators
      5. 4.5. The Special Assignment Operators
    2. 5. Character Expressions
      1. 5.1. Defining Character Variables
        1. 5.1.1. Encoding characters
        2. 5.1.2. Example of character encoding
      2. 5.2. Encoding Strings of Characters
      3. 5.3. Special Character Constants
    3. 6. if I Could Make My Own Decisions
      1. 6.1. The if Statement
        1. 6.1.1. Comparison operators
        2. 6.1.2. Say "No" to "No braces"
      2. 6.2. What else Is There?
      3. 6.3. Nesting if Statements
      4. 6.4. Compound Conditional Expressions
    4. 7. Switching Paths
      1. 7.1. Controlling Flow with the switch Statement
      2. 7.2. Control Fell Through: Did I break It?
      3. 7.3. Implementing an Example Calculator with the switch Statement
    5. 8. Debugging Your Programs, Part I
      1. 8.1. Identifying Types of Errors
      2. 8.2. Avoiding Introducing Errors
        1. 8.2.1. Coding with style
        2. 8.2.2. Establishing variable naming conventions
      3. 8.3. Finding the First Error with a Little Help
      4. 8.4. Finding the Run-Time Error
        1. 8.4.1. Formulating test data
        2. 8.4.2. Executing the test cases
        3. 8.4.3. Seeing what's going on in your program
  8. III. Becoming a Functional Programmer
    1. 9. while Running in Circles
      1. 9.1. Creating a while Loop
      2. 9.2. Breaking out of the Middle of a Loop
      3. 9.3. Nested Loops
    2. 10. Looping for the Fun of It
      1. 10.1. The for Parts of Every Loop
      2. 10.2. Looking at an Example
      3. 10.3. Getting More Done with the Comma Operator
    3. 11. Functions, I Declare!
      1. 11.1. Breaking Your Problem Down into Functions
      2. 11.2. Understanding How Functions Are Useful
      3. 11.3. Writing and Using a Function
        1. 11.3.1. Returning things
        2. 11.3.2. Reviewing an example
      4. 11.4. Passing Arguments to Functions
        1. 11.4.1. Function with arguments
        2. 11.4.2. Functions with multiple arguments
        3. 11.4.3. Exposing main()
      5. 11.5. Defining Function Prototype Declarations
    4. 12. Dividing Programs into Modules
      1. 12.1. Breaking Programs Apart
      2. 12.2. Breaking Up Isn't That Hard to Do
        1. 12.2.1. Creating Factorial.cpp
        2. 12.2.2. Creating an #include file
        3. 12.2.3. Including #include files
        4. 12.2.4. Creating main.cpp
        5. 12.2.5. Building the result
      3. 12.3. Using the Standard C++ Library
      4. 12.4. Variable Scope
    5. 13. Debugging Your Programs, Part 2
      1. 13.1. Debugging a Dys-Functional Program
        1. 13.1.1. Performing unit level testing
        2. 13.1.2. Outfitting a function for testing
        3. 13.1.3. Returning to unit test
  9. IV. Data Structures
    1. 14. Other Numerical Variable Types
      1. 14.1. The Limitations of Integers in C++
        1. 14.1.1. Integer round-off
        2. 14.1.2. Limited range
      2. 14.2. A Type That "doubles" as a Real Number
        1. 14.2.1. Solving the truncation problem
        2. 14.2.2. When an integer is not an integer
        3. 14.2.3. Discovering the limits of double
          1. 14.2.3.1. Counting
          2. 14.2.3.2. Calculation speed
          3. 14.2.3.3. Consume more memory
          4. 14.2.3.4. Loss of accuracy
          5. 14.2.3.5. Not so limited range
      3. 14.3. Variable Size — the "long" and "short" of It
        1. 14.3.1. How far do numbers range?
      4. 14.4. Types of Constants
      5. 14.5. Passing Different Types to Functions
        1. 14.5.1. Overloading function names
        2. 14.5.2. Mixed mode overloading
    2. 15. Arrays
      1. 15.1. What Is an Array?
      2. 15.2. Declaring an Array
      3. 15.3. Indexing into an Array
      4. 15.4. Looking at an Example
      5. 15.5. Initializing an Array
    3. 16. Arrays with Character
      1. 16.1. The ASCII-Zero Character Array
      2. 16.2. Declaring and Initializing an ASCIIZ Array
      3. 16.3. Looking at an Example
      4. 16.4. Looking at a More Detailed Example
        1. 16.4.1. Foiling hackers
        2. 16.4.2. Do I Really Have to Do All That Work?
    4. 17. Pointing the Way to C++ Pointers
      1. 17.1. What's a Pointer?
      2. 17.2. Declaring a Pointer
      3. 17.3. Passing Arguments to a Function
        1. 17.3.1. Passing arguments by value
        2. 17.3.2. Passing arguments by reference
        3. 17.3.3. Putting it together
      4. 17.4. Playing with Heaps of Memory
        1. 17.4.1. Do you really need a new keyword?
        2. 17.4.2. Don't forget to clean up after yourself
        3. 17.4.3. Looking at an example
    5. 18. Taking a Second Look at C++ Pointers
      1. 18.1. Pointers and Arrays
        1. 18.1.1. Operations on pointers
        2. 18.1.2. Pointer addition versus indexing into an array
        3. 18.1.3. Using the pointer increment operator
        4. 18.1.4. Why bother with array pointers?
      2. 18.2. Operations on Different Pointer Types
      3. 18.3. Constant Nags
      4. 18.4. Differences Between Pointers and Arrays
      5. 18.5. My main() Arguments
        1. 18.5.1. Arrays of pointers
        2. 18.5.2. Arrays of arguments
          1. 18.5.2.1. Passing arguments to your program through the command line
          2. 18.5.2.2. Passing arguments to your program from the Code::Blocks environment
          3. 18.5.2.3. Passing arguments to your program through Windows
    6. 19. Programming with Class
      1. 19.1. Grouping Data
      2. 19.2. The Class
      3. 19.3. The Object
      4. 19.4. Arrays of Objects
      5. 19.5. Looking at an Example
    7. 20. Debugging Your Programs, Part 3
      1. 20.1. A New Approach to Debugging
        1. 20.1.1. The solution
      2. 20.2. Entomology for Dummies
        1. 20.2.1. Starting the debugger
        2. 20.2.2. Navigating through a program with the debugger
        3. 20.2.3. Fixing the (first) bug
        4. 20.2.4. Finding and fixing the second bug
  10. V. Object-Oriented Programming
    1. 21. What Is Object-Oriented Programming?
      1. 21.1. Abstraction and Microwave Ovens
        1. 21.1.1. Functional nachos
        2. 21.1.2. Object-oriented nachos
      2. 21.2. Classification and Microwave Ovens
      3. 21.3. Why Build Objects This Way?
      4. 21.4. Self-Contained Classes
    2. 22. Structured Play: Making Classes Do Things
      1. 22.1. Activating Our Objects
      2. 22.2. Creating a Member Function
        1. 22.2.1. Defining a member function
        2. 22.2.2. Naming class members
        3. 22.2.3. Calling a member function
        4. 22.2.4. Accessing other members from within a member function
      3. 22.3. Keeping a Member Function after Class
      4. 22.4. Overloading Member Functions
    3. 23. Pointers to Objects
      1. 23.1. Pointers to Objects
        1. 23.1.1. Arrow syntax
        2. 23.1.2. Calling all member functions
      2. 23.2. Passing Objects to Functions
        1. 23.2.1. Calling a function with an object value
        2. 23.2.2. Calling a function with an object pointer
        3. 23.2.3. Looking at an example
      3. 23.3. Allocating Objects off the Heap
    4. 24. Do Not Disturb: Protected Members
      1. 24.1. Protecting Members
        1. 24.1.1. Why you need protected members
        2. 24.1.2. Making members protected
        3. 24.1.3. So what?
      2. 24.2. Who Needs Friends Anyway?
    5. 25. Getting Objects Off to a Good Start
      1. 25.1. The Constructor
        1. 25.1.1. Limitations on constructors
        2. 25.1.2. Can I see an example?
        3. 25.1.3. Constructing data members
      2. 25.2. Destructors
        1. 25.2.1. Looking at an example
        2. 25.2.2. Destructing data members
    6. 26. Making Constructive Arguments
      1. 26.1. Constructors with Arguments
        1. 26.1.1. Looking at an example
      2. 26.2. Overloading the Constructor
      3. 26.3. The Default default Constructor
      4. 26.4. Constructing Data Members
        1. 26.4.1. Initializing data members with the default constructor
        2. 26.4.2. Initializing data members with a different constructor
        3. 26.4.3. Looking at an example
        4. 26.4.4. New with C++ 2009
    7. 27. Coping with the Copy Constructor
      1. 27.1. Copying an Object
        1. 27.1.1. The default copy constructor
        2. 27.1.2. Looking at an example
      2. 27.2. Creating a Copy Constructor
      3. 27.3. Avoiding Copies
  11. VI. Advanced Strokes
    1. 28. Inheriting a Class
      1. 28.1. Advantages of Inheritance
        1. 28.1.1. Learning the lingo
      2. 28.2. Implementing Inheritance in C++
        1. 28.2.1. Looking at an example
      3. 28.3. Having a HAS_A Relationship
    2. 29. Are Virtual Functions for Real?
      1. 29.1. Overriding Member Functions
        1. 29.1.1. Early binding
        2. 29.1.2. Ambiguous case
        3. 29.1.3. Enter late binding
      2. 29.2. When Is Virtual Not?
      3. 29.3. Virtual Considerations
    3. 30. Overloading Assignment Operators
      1. 30.1. Overloading an Operator
      2. 30.2. Overloading the Assignment Operator Is Critical
      3. 30.3. Looking at an Example
      4. 30.4. Writing Your Own (or Not)
    4. 31. Performing Streaming I/O
      1. 31.1. How Stream I/O Works
      2. 31.2. Stream Input/Output
        1. 31.2.1. Creating an input object
        2. 31.2.2. Creating an output object
        3. 31.2.3. Open modes
        4. 31.2.4. What is binary mode?
        5. 31.2.5. Hey, file, what state are you in?
      3. 31.3. Other Member Functions of the fstream Classes
        1. 31.3.1. Reading and writing streams directly
        2. 31.3.2. Controlling format
        3. 31.3.3. What's up with endl?
      4. 31.4. Manipulating Manipulators
      5. 31.5. Using the stringstream Classes
    5. 32. I Take Exception!
      1. 32.1. The Exception Mechanism
        1. 32.1.1. Examining the exception mechanism in detail
        2. 32.1.2. Special considerations for throwing
      2. 32.2. Creating a Custom Exception Class
        1. 32.2.1. Restrictions on exception classes
  12. VII. The Part of Tens
    1. 33. Ten Ways to Avoid Bugs
      1. 33.1. Enable All Warnings and Error Messages
      2. 33.2. Adopt a Clear and Consistent Coding Style
      3. 33.3. Comment the Code While You Write It
      4. 33.4. Single-Step Every Path in the Debugger at Least Once
      5. 33.5. Limit the Visibility
      6. 33.6. Keep Track of Heap Memory
      7. 33.7. Zero Out Pointers after Deleting What They Point To
      8. 33.8. Use Exceptions to Handle Errors
      9. 33.9. Declare Destructors Virtual
      10. 33.10. Provide a Copy Constructor and Overloaded Assignment Operator
    2. 34. Ten Features Not Covered in This Book
      1. 34.1. The goto Command
      2. 34.2. The Ternary Operator
      3. 34.3. Binary Logic
      4. 34.4. Enumerated Types
      5. 34.5. Namespaces
      6. 34.6. Pure Virtual Functions
      7. 34.7. The string Class
      8. 34.8. Multiple Inheritance
      9. 34.9. Templates and the Standard Template Library
      10. 34.10. The 2009 C++ Standard
    3. 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