O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

C++: An Active Learning Approach

Book Description

C++: An Active Learning Approach provides a hands-on approach to the C++ language through active learning exercises and numerous programming projects. Ideal for the introductory programming course, this text includes the latest C++ upgrades without losing site of the C underpinnings still required for all computing fields. With over 30 years combined teaching experience the authors understand potential pitfalls students face and aim to keep the language simple, straightforward, and conversational. The topics are covered in-depth yet as succinctly as possible. The text provides challenging exercises designed to teach students how to effectively debug a computer program and Team Programming exercises urge students to read existing code, adhere to code specifications, and write from existing design documents. Examples are provided electronically allowing to students to easily run code found in the text.

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication Page
  5. Preface
  6. Contents
  7. Chapter 1 - Introduction to Computers and Programming
    1. 1.1 - Overview of Computers and Programming
    2. 1.2 - Historical Developments
    3. 1.3 - Computer Hardware
      1. 1.3.1 - Input and Output (I/O) Devices
      2. 1.3.2 - Random-Access Memory (RAM)
      3. 1.3.3 - Central Processing Unit (CPU)
      4. 1.3.4 - Storage Devices
      5. 1.3.5 - Motherboard
    4. 1.4 - Computer Software
      1. 1.4.1 - Application Software
      2. 1.4.2 - System Software
    5. 1.5 - Embedded Systems
    6. 1.6 - Programming Paradigms
    7. 1.7 - Programming Languages
      1. 1.7.1 - The C Programming Language
      2. 1.7.2 - The C++ Programming Language
    8. 1.8 - Data Hierarchy
    9. 1.9 - Numbering Systems
      1. 1.9.1 - Counting
      2. 1.9.2 - Converting from a Base to Base 10
      3. 1.9.3 - Converting from Base 10 to a Different Base
    10. 1.10 - Summary
    11. 1.11 - Answers to Selected Chapter Exercises
  8. Chapter 2 - Program Design and Development
    1. 2.1 - Procedural Programming
    2. 2.2 - Problem-Solving Overview
      1. 2.2.1 - Development Process
    3. 2.3 - Algorithm Representation
      1. 2.3.1 - Flowchart
      2. 2.3.2 - Pseudocode
    4. 2.4 - Algorithm Development
      1. 2.4.1 - Value of Algorithm Development
      2. 2.4.2 - Stepwise Refinement
    5. 2.5 - Compilation Process
      1. 2.5.1 - Editor
      2. 2.5.2 - Preprocessor
      3. 2.5.3 - Language Translator
      4. 2.5.4 - Linker
    6. 2.6 - Program Development in Microsoft Visual Studio
      1. 2.6.1 - Using an Integrated Development Environment (IDE)
    7. 2.7 - Running the Program
    8. 2.8 - Errors
      1. 2.8.1 - Syntax Error
      2. 2.8.2 - Linker Error
      3. 2.8.3 - Run-Time Error
      4. 2.8.4 - Logic Error
    9. 2.9 - Desk Checking
    10. 2.10 - Using Visual Studio (VS)
      1. 2.10.1 - Getting Started
    11. 2.11 - Debugging
    12. 2.12 - Problem Solving Applied
    13. 2.13 - Summary
    14. 2.14 - Debugging Exercise
    15. 2.15 - Programming Exercises
    16. 2.16 - Team Programming Exercise
    17. 2.17 - Answers to Chapter Exercises
  9. Chapter 3 - Getting Started with C++
    1. 3.1 - C++ Basics
    2. 3.2 - Whitespace
    3. 3.3 - Comments
    4. 3.4 - The main Function
    5. 3.5 - The #include Preprocessor Directive
      1. 3.5.1 - Namespaces
    6. 3.6 - Problem Solving Applied
    7. 3.7 - C—The Differences
    8. 3.8 - Summary
    9. 3.9 - Debugging Exercise
    10. 3.10 - Programming Exercises
    11. 3.11 - Team Programming Exercise
    12. 3.12 - Answers to Chapter Exercises
  10. Chapter 4 - Literals, Variables, and Constants
    1. 4.1 - Literals
    2. 4.2 - Character Escape Sequences
    3. 4.3 - Variable Declarations
      1. 4.3.1 - Variable’s Initial Value
      2. 4.3.2 - Initialization
      3. 4.3.3 - Data Types
      4. 4.3.4 - The sizeof Operator
      5. 4.3.5 - Numeric Literal Suffixes
      6. 4.3.6 - Naming Rules
    4. 4.4 - ASCII Characters
    5. 4.5 - Constants
    6. 4.6 - const Versus #define
    7. 4.7 - Bringing It All Together
    8. 4.8 - Variable Declarations in Pseudocode
    9. 4.9 - Problem Solving Applied
    10. 4.10 - C—The Differences
    11. 4.11 - Summary
    12. 4.12 - Debugging Exercise
    13. 4.13 - Programming Exercises
    14. 4.14 - Team Programming Exercise
    15. 4.15 - Answers to Chapter Exercises
  11. Chapter 5 - Input and Output Streams
    1. 5.1 - Input and Output Streams
      1. 5.1.1 - Namespaces and I/O Streams
      2. 5.1.2 - The cout Statement
      3. 5.1.3 - The cin Statement
    2. 5.2 - The end1 Manipulator
      1. 5.2.1 - The endl Manipulator Versus ‘\n’
    3. 5.3 - Input and Output Buffers
    4. 5.4 - Formatting the Output Stream
      1. 5.4.1 - The .width Member Function and setw Manipulator
      2. 5.4.2 - The .precision Member Function and setprecision Manipulator
      3. 5.4.3 - The . setf Member Function
      4. 5.4.4 - The .flush Member Function and flush Manipulator
    5. 5.5 - Debugging
      1. 5.5.1 - Step Into, Step Over, and Step Out
      2. 5.5.2 - Run to Cursor
      3. 5.5.3 - Breakpoints
      4. 5.5.4 - Watches
      5. 5.5.5 - Importance of Debugging
    6. 5.6 - Problem Solving Applied
    7. 5.7 - C—The Differences
      1. 5.7.1 - The printf Function
      2. 5.7.2 - Formatting the printf Statement
      3. 5.7.3 - Flushing the Output Buffer
      4. 5.7.4 - The scan f Function
      5. 5.7.5 - The getch Function
    8. 5.8 - Summary
    9. 5.9 - Debugging Exercise
    10. 5.10 - Programming Exercises
    11. 5.11 - Team Programming Exercises
    12. 5.12 - Answers to Chapter Exercises
  12. Chapter 6 - Mathematical Operations
    1. 6.1 - Mathematical Expressions
    2. 6.2 - Assignment Operator (=)
    3. 6.3 - Standard Arithmetic Operators (+, -, *, /, %)
    4. 6.4 - Increment and Decrement Operators (++, --)
    5. 6.5 - Compound Assignment Operators (+=,-=, *=, /=, %=)
    6. 6.6 - Accumulators Versus Counters
    7. 6.7 - Order of Precedence
    8. 6.8 - Mathematical Functions
      1. 6.8.1 - The pow Function
      2. 6.8.2 - The sqrt Function
      3. 6.8.3 - The abs Function
    9. 6.9 - Type Casting
    10. 6.10 - Problem Solving Applied
    11. 6.11 - C—The Differences
    12. 6.12 - Summary
    13. 6.13 - Debugging Exercise
    14. 6.14 - Programming Exercises
    15. 6.15 - Team Programming Exercise
    16. 6.16 - Answers to Chapter Exercises
  13. Chapter 7 - Conditional Statements
    1. 7.1 - Conditional Expressions
      1. 7.1.1 - Relational Operators
      2. 7.1.2 - Logical Operators
    2. 7.2 - The if Statement
      1. 7.2.1 - The else Statement
      2. 7.2.2 - The else if Statement
    3. 7.3 - Variable Scope
    4. 7.4 - The switch Statement
    5. 7.5 - Conditional Operator
    6. 7.6 - Problem Solving Applied
    7. 7.7 - C—The Differences
    8. 7.8 - Summary
    9. 7.9 - Debugging Exercise
    10. 7.10 - Programming Exercises
    11. 7.11 - Team Programming Exercise
    12. 7.12 - Answers to Chapter Exercises
  14. Chapter 8 - Repetition Statements
    1. 8.1 - General Repetition Concepts
    2. 8.2 - The while Loop
    3. 8.3 - The do-while Loop
    4. 8.4 - The for Loop
      1. 8.4.1 - Nested for Loops
    5. 8.5 - Breakpoints and Loops
    6. 8.6 - Problem Solving Applied
    7. 8.7 - C—The Differences
    8. 8.8 - Summary
    9. 8.9 - Debugging Exercise
    10. 8.10 - Programming Exercises
    11. 8.11 - Team Programming Exercise
    12. 8.12 - Answers to Chapter Exercises
  15. Chapter 9 - Functions
    1. 9.1 - What Are Functions?
      1. 9.1.1 - Advantages and Disadvantages of Functions
      2. 9.1.2 - What about main
    2. 9.2 - Function Components
      1. 9.2.1 - Function Declaration
      2. 9.2.2 - Function Definition
      3. 9.2.3 - Function Call
    3. 9.3 - Return
    4. 9.4 - Passing Parameters
      1. 9.4.1 - Formal and Actual Parameters
      2. 9.4.2 - Scope
      3. 9.4.3 - Passing by Value
      4. 9.4.4 - Passing by Reference
    5. 9.5 - Default Arguments
    6. 9.6 - Putting It All Together
    7. 9.7 - Debugging—Call Stack
    8. 9.8 - More Predefined Functions
      1. 9.8.1 - Character Case
      2. 9.8.2 - The “is” Functions
    9. 9.9 - Structure Charts
    10. 9.10 - Problem Solving Applied
    11. 9.11 - C—The Differences
    12. 9.12 - Summary
    13. 9.13 - Debugging Exercise
    14. 9.14 - Programming Exercises
    15. 9.15 - Team Programming Exercise
    16. 9.16 - Answers to Chapter Exercises
  16. Chapter 10 - Arrays and cStrings
    1. 10.1 - What Are Arrays?
      1. 10.1.1 - Advantages and Disadvantages of Arrays
    2. 10.2 - Declaring Arrays
    3. 10.3 - Using Arrays
    4. 10.4 - Initialization
    5. 10.5 - Array Manipulation
    6. 10.6 - Passing Arrays to Functions
    7. 10.7 - Special Case: cStrings
      1. 10.7.1 - cString Initialization
      2. 10.7.2 - I/O with cStrings
      3. 10.7.3 - cStrings and the “address of” Operator
    8. 10.8 - cString Functions
      1. 10.8.1 - Copying
      2. 10.8.2 - Appending
      3. 10.8.3 - Comparing
      4. 10.8.4 - Finding the Length
      5. 10.8.5 - Changing the Case
      6. 10.8.6 - Reversing
      7. 10.8.7 - Converting cStrings to Numbers
      8. 10.8.8 - Converting Numbers to cStrings
      9. 10.8.9 - Summary of cString Functions
    9. 10.9 - cString Function Standards
    10. 10.10 - Multidimensional Arrays
      1. 10.10.1 - Declaration
      2. 10.10.2 - Initialization
      3. 10.10.3 - Multidimensional Array I/O
      4. 10.10.4 - Passing Multidimensional Arrays
    11. 10.11 - Parallel Arrays
    12. 10.12 - Problem Solving Applied
    13. 10.13 - C—The Differences
    14. 10.14 - Summary
    15. 10.15 - Debugging Exercise
    16. 10.16 - Programming Exercises
    17. 10.17 - Team Programming Exercise
    18. 10.18 - Answers to Chapter Exercises
  17. Chapter 11 - File I/O and Data Manipulation
    1. 11.1 - Data Files
    2. 11.2 - File Streams
    3. 11.3 - Opening Files
    4. 11.4 - Checking for Successful Opening
    5. 11.5 - Closing Files
    6. 11.6 - Writing to Files
    7. 11.7 - Reading from Files
    8. 11.8 - Searching
    9. 11.9 - Sorting
    10. 11.10 - Problem Solving Applied
    11. 11.11 - C—The Differences
      1. 11.11.1 - File Pointer
      2. 11.11.2 - Opening Files
      3. 11.11.3 - Checking for a Successful Open
      4. 11.11.4 - Closing Files
      5. 11.11.5 - Writing to a File
      6. 11.11.6 - Reading from a File
    12. 11.12 - Summary
    13. 11.13 - Debugging Exercise
    14. 11.14 - Programming Exercises
    15. 11.15 - Team Programming Exercise
    16. 11.16 - Answers to Chapter Exercises
  18. Chapter 12 - Pointers and Dynamic Memory Allocation
    1. 12.1 - Definition
    2. 12.2 - Declaration
    3. 12.3 - Advanced Pointer Declarations
    4. 12.4 - Indirection Operator
    5. 12.5 - Passing by Pointer
    6. 12.6 - Pointer Arithmetic
    7. 12.7 - Pointers Applied
    8. 12.8 - More cString Functions
      1. 12.8.1 - The strtok Function
      2. 12.8.2 - The strstr Function
      3. 12.8.3 - The strchr Function
      4. 12.8.4 - The memset Function
    9. 12.9 - Dynamic Memory Allocation
      1. 12.9.1 - The new Operator
      2. 12.9.2 - The delete Operator
    10. 12.10 - Passing Pointers by Reference
    11. 12.11 - Pointers and Strings
    12. 12.12 - Ragged Arrays
    13. 12.13 - Dynamic Two-Dimensional Arrays
    14. 12.14 - Testing for Memory Leaks
    15. 12.15 - Function Pointers
      1. 12.15.1 - Declaration
      2. 12.15.2 - Retrieving a Function’s Address
      3. 12.15.3 - Indirectly Calling a Function
      4. 12.15.4 - Uses of Function Pointers
    16. 12.16 - Problem Solving Applied
    17. 12.17 - C—The Differences
      1. 12.17.1 - Dynamic Memory Allocation
      2. 12.17.2 - Dynamic Memory Deallocation
      3. 12.17.3 - Memory Leak Detection
    18. 12.18 - Summary
    19. 12.19 - Debugging Exercise
    20. 12.20 - Programming Exercises
    21. 12.21 - Team Programming Exercise
    22. 12.22 - Answers to Chapter Exercises
  19. Chapter 13 - User-Defined Types
    1. 13.1 - The typedef Statement
    2. 13.2 - Enumerated Data Types
    3. 13.3 - Structures
      1. 13.3.1 - Nested Structures
      2. 13.3.2 - Structure Variables
      3. 13.3.3 - Accessing the Data Members
      4. 13.3.4 - Structure Variable Manipulation
      5. 13.3.5 - Shallow Copy Versus Deep Copy
    4. 13.4 - Unions
    5. 13.5 - Problem Solving Applied
    6. 13.6 - C—The Differences
    7. 13.7 - Summary
    8. 13.8 - Debugging Exercise
    9. 13.9 - Programming Exercise
    10. 13.10 - Team Programming Exercise
    11. 13.11 - Answers to Chapter Exercises
  20. Chapter 14 - Introduction to Object-Oriented Programming
    1. 14.1 - History of Object-Oriented Programming
    2. 14.2 - Key Concepts of Object-Oriented Programming
      1. 14.2.1 - Encapsulation
      2. 14.2.2 - Interface Versus Implementation
      3. 14.2.3 - Inheritance
      4. 14.2.4 - Polymorphism
    3. 14.3 - Advantages and Disadvantages
      1. 14.3.1 - Models Real Life
      2. 14.3.2 - Self-Contained
      3. 14.3.3 - Code Reuse
      4. 14.3.4 - OOP Overhead
    4. 14.4 - String Class
      1. 14.4.1 - Instantiation and Initialization
      2. 14.4.2 - Reading and Writing Strings
      3. 14.4.3 - Other String Features
    5. 14.5 - UML Introduction
      1. 14.5.1 - UML Overview
      2. 14.5.2 - Class Diagrams
    6. 14.6 - Problem Solving Applied
    7. 14.7 - C—The Differences
    8. 14.8 - Summary
    9. 14.9 - Answers to Chapter Exercises
  21. Chapter 15 - Introduction to Classes
    1. 15.1 - Classes
      1. 15.1.1 - Definition Syntax
      2. 15.1.2 - Access Specifiers
      3. 15.1.3 - Data Members
      4. 15.1.4 - Member Functions
      5. 15.1.5 - Setters and Getters
    2. 15.2 - Instantiation
    3. 15.3 - Manager Functions
      1. 15.3.1 - Constructor
      2. 15.3.2 - Destructor
    4. 15.4 - Implementation Hiding
      1. 15.4.1 - #include “header.h”
    5. 15.5 - Conditional Compilation
      1. 15.5.1 - #ifndef
      2. 15.5.2 - #pragma once
    6. 15.6 - Base Member Initialization
    7. 15.7 - Function Overloading
      1. 15.7.1 - Name Mangling
    8. 15.8 - Passing By Const-ref
      1. 15.8.1 - Const Methods
    9. 15.9 - Problem Solving Applied
    10. 15.10 - C—The Differences
    11. 15.11 - Summary
    12. 15.12 - Debugging Exercise
    13. 15.13 - Programming Exercises
    14. 15.14 - Team Programming Exercise
    15. 15.15 - Answers to Chapter Exercises
  22. Chapter 16 - Miscellaneous Topics
    1. 16.1 - Storage Classes
      1. 16.1.1 - Static
      2. 16.1.2 - Extern
    2. 16.2 - Command-Line Arguments
    3. 16.3 - Conditional Compilation
    4. 16.4 - Macros
      1. 16.4.1 - Advantages and Disadvantages
    5. 16.5 - Bitwise Operations
      1. 16.5.1 - Bitwise Operators
      2. 16.5.2 - Bit Fields
      3. 16.5.3 - Masking
    6. 16.6 - Recursion
    7. 16.7 - Summary
    8. 16.8 - Programming Exercises
    9. 16.9 - Answers to Chapter Exercises
  23. Chapter 17 - Binary and Random Access Files
    1. 17.1 - Text Files Versus Binary Files
      1. 17.1.1 - Advantages and Disadvantages of Binary Files
    2. 17.2 - Opening and Closing Binary Files
    3. 17.3 - Binary File I/O
    4. 17.4 - Sequential Files Versus Random Access Files
      1. 17.4.1 - Determining Current FPM Location
      2. 17.4.2 - Moving the FPM
    5. 17.5 - C—The Differences
      1. 17.5.1 - File Modes
      2. 17.5.2 - Binary File I/O with C
      3. 17.5.3 - Random Access Functions
    6. 17.6 - Summary
    7. 17.7 - Programming Exercise
    8. 17.8 - Answers to Chapter Exercises
  24. Chapter 18 - Introduction to Linked Lists
    1. 18.1 - Data Structures
    2. 18.2 - Linked Lists
      1. 18.2.1 - List Implementation Options
      2. 18.2.2 - Node Structure
      3. 18.2.3 - Head Pointer
      4. 18.2.4 - Creating a New Node
    3. 18.3 - Prepending a Node
    4. 18.4 - Appending a Node
    5. 18.5 - Inserting a Node into an Ordered List
    6. 18.6 - Traversing the List
    7. 18.7 - Deleting Nodes
      1. 18.7.1 - Deleting All Nodes
      2. 18.7.2 - Deleting a Specific Node from an Ordered List
    8. 18.8 - Arrays Versus Linked Lists
    9. 18.9 - Problem Solving Applied
    10. 18.10 - C—The Differences
    11. 18.11 - Summary
    12. 18.12 - Debugging Exercise
    13. 18.13 - Programming Exercises
    14. 18.14 - Team Programming Exercise
    15. 18.15 - Answers to Chapter Exercises
  25. Appendix A: ASCII Chart
  26. Glossary
  27. Index