You are previewing Programming and Problem Solving with C++, 5th Edition.
O'Reilly logo
Programming and Problem Solving with C++, 5th Edition

Book Description

Completely revised and updated with the latest version of C++, the new Fifth Edition of Programming and Problem Solving with C++ provides the clearest introduction to C++, object-oriented programming, and software development available. Renowned author team Nell Dale and Chip Weems are careful to include all topics and guidelines put forth by the ACM/IEEE. A new chapter on Data Structures makes this text ideal for the one- or two-term course. New Software Maintenance Case Studies teach students how to read code in order to debug, alter, or enhance existing class or code segments.

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Contents
  5. Preface
  6. 1 - Overview of Programming and Problem Solving
    1. 1.1 - Overview of Programming
      1. What is Programming?
      2. How do we Write a Program?
      3. What is an Algorithm?
      4. What is a Programming Language?
    2. 1.2 - How does a Computer Run a Program?
      1. What Kinds of Instructions Can be Written in a Programming Language?
      2. What is Software Maintenance?
    3. Software Maintenance Case Study: An Introduction to Software Maintenance
    4. 1.3 - What's Inside the Computer?
    5. 1.4 - Ethics and Responsibilities in the Computing Profession
      1. Software Piracy
      2. Privacy of Data
      3. Use of Computer Resources
      4. Software Engineering
    6. 1.5 - Problem-Solving Techniques
      1. Ask Questions
      2. Look for Things that are Familiar
      3. Solve by Analogy
      4. Means-Ends Analysis
      5. Divide and Conquer
      6. The Building-Block Approach
      7. Merging Solutions
      8. Mental Blocks: The Fear of Starting
      9. Algorithmic Problem Solving
    7. Problem-Solving Case Study: Leap Year Algorithm
    8. Summary
    9. Quick Check
    10. Answers
    11. Exam Preparation Exercises
    12. Programming Warm-Up Exercises
    13. Case Study Follow-Up
    14. Line Number
  7. 2 -C++ Syntax and Semantics, and the Program Development Process
    1. 2.1 - The Elements of C++ Programs
      1. C++ Program Structure
      2. Syntax and Semantics
      3. Syntax Templates
      4. Naming Program Elements: Identifiers
      5. Data and Data Types
      6. Naming Elements: Declarations
      7. Taking Action: Executable Statements
      8. Beyond Minimalism: Adding Comments to a Program
    2. 2.2 - Program Construction
      1. Blocks (Compound Statements)
      2. The C++ Preprocessor
    3. Software Maintenance Case Study: Adding Titles to Names
    4. 2.3 - More About Output
      1. Creating Blank Lines
      2. Inserting Blanks within a Line
      3. Special Characters
    5. 2.4 - Program Entry, Correction, and Execution
      1. Entering a Program
      2. Compiling and Running a Program
    6. Problem-Solving Case Study: Printing a Chessboard
    7. Testing and Debugging
    8. Summary
    9. Quick Check
    10. Answers
    11. Exam Preparation Exercises
    12. Programming Warm-Up Exercises
    13. Programming Problems
    14. Case Study Follow-Up
  8. 3 -Numeric Types, Expressions, and Output
    1. 3.1 - Overview of C++ Data Types
    2. 3.2 - Numeric Data Types
      1. Integral Types
      2. Floating-Point Types
    3. 3.3 - Declarations for Numeric Types
      1. Named Constant Declarations
      2. Variable Declarations
    4. 3.4 - Simple Arithmetic Expressions
      1. Arithmetic Operators
      2. Increment and Decrement Operators
    5. 3.5 - Compound Arithmetic Expressions
      1. Precedence Rules
      2. Type Coercion and Type Casting
    6. Software Maintenance Case Study: Precedence Error
    7. 3.6 - Function Calls and Library Functions
      1. Value-Returning Functions
      2. Library Functions
      3. Void Functions
    8. 3.7 - Formatting Output
      1. Integers and Strings
      2. Floating-Point Numbers
    9. 3.8 - Additional string Operations
      1. The length and size Functions
      2. The find Function
      3. The substr Function
      4. Accessing Characters within a String: The at Function
      5. Converting to Lowercase and Uppercase
    10. Problem-Solving Case Study: Mortgage Payment Calculator
    11. Testing and Debugging
    12. Summary
    13. Quick Check
    14. Answers
    15. Exam Preparation Exercises
    16. Programming Warm-Up Exercises
    17. Programming Problems
    18. Case Study Follow-Up
  9. 4 -Program Input and the Software Design Process
    1. 4.1 - Getting Data into Programs
      1. Input Streams and the Extraction Operator (>>)
      2. The Reading Marker and the Newline Character
      3. Reading Character Data with the get Function
      4. Skipping Characters with the ignore Function
      5. Reading String Data
    2. 4.2 - Interactive Input/Output
    3. 4.3 - Noninteractive Input/Output
    4. 4.4 - File Input and Output
      1. Files
      2. Using Files
    5. Software Maintenance Case Study: Adding File Input/Output to a Program
      1. Run-Time Input of File Names
    6. 4.5 - Input Failure
    7. 4.6 - Software Design Methodologies
    8. 4.7 - Functional Decomposition
      1. Modules
      2. Implementing the Design
      3. A Perspective on Design
    9. Problem-Solving Case Study: Displaying a Name in Multiple Formats
    10. Testing and Debugging
      1. Testing and Debugging Hints
    11. Summary
    12. Quick Check
    13. Answers
    14. Exam Preparation Exercises
    15. Programming Warm-Up Exercises
    16. Programming Problems
    17. Case Study Follow-Up
  10. 5 -Conditions, Logical Expressions, and Selection Control Structures
    1. 5.1 - Flow of Control
      1. Selection
    2. 5.2 - Conditions and Logical Expressions
      1. The bool Data Type
      2. Logical Expressions
    3. 5.3 - The If Statement
      1. The If-Then-Else Form
      2. Blocks (Compound Statements)
      3. The If-Then Form
      4. A Common Mistake
    4. Software Maintenance Case Study: Incorrect Output
    5. 5.4 - Nested if Statements
      1. The Dangling else
    6. 5.5 - Logical Operators
      1. Precedence of Operators
      2. Relational Operators with Floating-Point Types
    7. 5.6 - Testing the State of an I/O Stream
    8. Problem-Solving Case Study: BMI Calculator
    9. Testing and Debugging
      1. Testing in the Problem-Solving Phase: The Algorithm Walk-Through
      2. Testing in the Implementation Phase
      3. The Test Plan
      4. Tests Performed Automatically During Compilation and Execution
      5. Testing and Debugging Hints
    10. Summary
    11. Quick Check
    12. Answers
    13. Exam Preparation Exercises
    14. Programming Warm-Up Exercises
    15. Programming Problems
    16. Case Study Follow-Up
  11. 6 -Looping
    1. 6.1 - The While Statement
    2. 6.2 - Phases of Loop Execution
    3. 6.3 - Loops Using the While Statement
      1. Count-Controlled Loops
      2. Event-Controlled Loops
      3. Looping Subtasks
    4. Software Maintenance Case Study: Make a Program General
    5. 6.4 - How to Design Loops
      1. Designing the Flow of Control
      2. Designing the Process within the Loop
      3. The Loop Exit
    6. 6.5 - Nested Logic
      1. Designing Nested Loops
    7. Problem-Solving Case Study: Recording Studio Design
    8. Testing and Debugging
      1. Loop-Testing Strategy
      2. Test Plans Involving Loops
      3. Testing and Debugging Hints
    9. Summary
    10. Quick Check
    11. Answers
    12. Exam Preparation Exercises
    13. Programming Warm-Up Exercises
    14. Programming Problems
    15. Case Study Follow-Up
  12. 7 -Additional Control Structures
    1. 7.1 - The Switch Statement
    2. 7.2 - The Do-While Statement
    3. 7.3 - The For Statement
      1. Software Maintenance Case Study: Changing a Loop Implementation
    4. 7.4 - The Break and Continue Statements
    5. 7.5 - Guidelines for Choosing a Looping Statement
    6. 7.6 - Additional C++ Operators
      1. Assignment Operators and Assignment Expressions
      2. Increment and Decrement Operators
      3. Bitwise Operators
      4. The Cast Operation
      5. The sizeof Operator
      6. The ?: Operator
      7. Operator Precedence
      8. Type Coercion in Arithmetic and Relational Expressions
    7. Problem-Solving Case Study: The Rich Uncle
    8. Testing and Debugging
      1. Testing and Debugging Hints
    9. Summary
    10. Quick Check
    11. Answers
    12. Exam Preparation Exercises
    13. Programming Warm-Up Exercises
    14. Programming Problems
    15. Case Study Follow-Up
  13. 8 -Functions
    1. 8.1 - Functional Decomposition with Void Functions
      1. When to Use Functions
      2. Why Do Modules Need an Interface Design?
      3. Designing Interfaces
      4. Writing Modules as Void Functions
    2. 8.2 - An Overview of User-Defined Functions
      1. Flow of Control in Function Calls
      2. Function Parameters
    3. 8.3 - Syntax and Semantics of Void Functions
      1. Function Call (Invocation)
      2. Function Declarations and Definitions
      3. Local Variables
      4. The Return Statement
    4. 8.4 - Parameters
      1. Value Parameters
      2. Reference Parameters
    5. Software Maintenance Case Study: Refactoring a Program
      1. Using Expressions with Parameters
      2. A Last Word of Caution About Argument and Parameter Lists
      3. Writing Assertions as Function Documentation
    6. Problem-Solving Case Study: Lawn Care Company Billing
    7. Testing and Debugging
      1. The assert Library Function
      2. Testing and Debugging Hints
    8. Summary
    9. Quick Check
    10. Answers
    11. Exam Preparation Exercises
    12. Programming Warm-Up Exercises
    13. Programming Problems
    14. Case Study Follow-Up
  14. 9 -Scope, Lifetime, and More on Functions
    1. 9.1 - Scope of Identifiers
      1. Scope Rules
      2. Variable Declarations and Definitions
      3. Namespaces
    2. 9.2 - Lifetime of a Variable
      1. Initializations in Declarations
    3. Software Maintenance Case Study: Debug a Simple Program
    4. 9.3 - Interface Design
      1. Side Effects
      2. Global Constants
    5. 9.4 - Value-Returning Functions
      1. Complete Example
      2. Boolean Functions
      3. Interface Design and Side Effects
      4. When to Use Value-Returning Functions
    6. 9.5 - Type Coercion in Assignments, Argument Passing, and Return of a Function Value
    7. Problem-Solving Case Study: Health Profile
    8. Testing and Debugging
      1. Stubs and Drivers
      2. Testing and Debugging Hints
    9. Summary
    10. Quick Check
    11. Answers
    12. Exam Preparation Exercises
    13. Programming Warm-Up Exercises
    14. Programming Problems
    15. Case Study Follow-Up
  15. 10 -User-Defined Data Types
    1. 10.1 - Built-In Simple Types
      1. Numeric Types
      2. Characters
    2. 10.2 - User-Defined Simple Types
      1. The Typedef Statement
      2. Enumeration Types
      3. Named and Anonymous Data Types
    3. 10.3 - Simple Versus Structured Data Types
    4. 10.4 - Records (Structs)
      1. Accessing Individual Components
      2. Aggregate Operations on Structs
      3. More About Struct Declarations
      4. Binding Like Items
    5. Software Maintenance Case Study: Changing a Loop Implementation
    6. 10.5 - Hierarchical Records
    7. 10.6 - Unions
      1. Problem-Solving Case Study: Stylistical Analysis of Text
      2. Testing and Debugging
        1. Coping with Input Errors
        2. Testing and Debugging Hints
    8. Summary
    9. Quick Check
    10. Answers
    11. Exam Preparation Exercises
    12. Programming Warm-Up Exercises
    13. Programming Problems
    14. Case Study Follow-Up
  16. 11 -Arrays
    1. 11.1 - One-Dimensional Arrays
      1. Declaring Arrays
      2. Accessing Individual Components of an Array
      3. Out-of-Bounds Array Indexes
      4. Initializing Arrays in Declarations
      5. (Lack of) Aggregate Array Operations
      6. Examples of Declaring and Accessing Arrays
      7. Passing Arrays as Arguments
      8. Commenting Arrays
    2. Software Maintenance Case Study: Modularizing a Program
      1. Using Typedef with Arrays
    3. 11.2 - Arrays of Records
      1. Arrays of Records
    4. 11.3 - Special Kinds of Array Processing
      1. Subarray Processing
      2. Indexes with Semantic Content
    5. 11.4 - Two-Dimensional Arrays
    6. 11.5 - Passing Two-Dimensional Arrays as Arguments
    7. 11.6 - Processing Two-Dimensional Arrays
      1. Sum the Rows
      2. Sum the Columns Revised
      3. Sum the Columns
      4. Initialize the Array
      5. Print the Array
    8. 11.7 - Another way of Defining Two-Dimensional Arrays
    9. 11.8 - Multidimensional Arrays
      1. Problem-Solving Case Study: Calculating Exam Statistics
      2. Problem-Solving Case Study: Favorite Rock Group
      3. Testing and Debugging
        1. One-Dimensional Arrays
        2. Complex Structures
        3. Multidimensional Arrays
        4. Testing and Debugging Hints
    10. Summary
    11. Quick Check
    12. Answers
    13. Exam Preparation Exercises
    14. Programming Warm-Up Exercises
    15. Programming Problems
    16. Case Study Follow-Up
  17. 12 -Classes and Abstraction
    1. 12.1 - Abstract Data Types
    2. 12.2 - C++ Classes
      1. Implementing the Member Functions
      2. Classes, Objects, and Members
      3. Built-in Operations on Objects
      4. Class Scope
    3. 12.3 - Information Hiding
      1. User-Written Header Files
      2. Specification and Implementation Files
      3. Compiling and Linking a Multifile Program
    4. 12.4 - What is an Object?
    5. 12.5 - Class Design Principles
      1. Encapsulation
      2. Abstraction
      3. Designing for Modifiability and Reuse
      4. Mutability
    6. Software Maintenance Case Study: Comparing Two TimeOfDay Objects
    7. 12.6 - The Name ADT
      1. Specification of the ADT
      2. Implementation File
    8. 12.7 - Composition
      1. Design of an Entry Class
    9. 12.8 - UML Diagrams
      1. Diagramming a Class
      2. Diagramming Composition of Classes
    10. Problem-Solving Case Study: Create an Array of Name Objects
    11. Testing and Debugging
      1. Testing and Debugging Hints
    12. Summary
    13. Quick Check
    14. Answers
    15. Exam Preparation Exercises
    16. Programming Warm-Up Exercises
    17. Programming Problems
    18. Case Study Follow-Up
  18. 13 -Array-Based Lists
    1. 13.1 - What is a List?
    2. 13.2 - The List as an Abstract Data Type
      1. Refining Responsibilities
      2. Data Representation
      3. Example Program
    3. 13.3 - Implementation of List ADT
      1. Basic Operations
      2. Insertion and Deletion
      3. Sequential Search
      4. Iterators
    4. Software Maintenance Case Study: Enhancing Class List with a Sort
    5. 13.4 - Sorted Lists
      1. Basic Operations
      2. Insertion
      3. Sequential Search
      4. Binary Search
      5. Deletion
    6. 13.5 - Sorted List of Classes
      1. IsThere
      2. Insert and Delete
    7. 13.6 - More on UML Diagrams
      1. Problem-Solving Case Study: Calculating Exam Statistics Revisited
      2. Testing and Debugging
        1. Testing and Debugging Hints
    8. Summary
    9. Quick Check
    10. Answers
    11. Exam Preparation Exercises
    12. Programming Warm-Up Exercises
    13. Programming Problems
    14. Case Study Follow-Up
  19. 14 -Pointers and Linked Lists
    1. 14.1 - Pointers
      1. Pointer Variables
      2. Pointer Expressions
    2. 14.2 - Reference Types
    3. 14.3 - Dynamic Data
      1. Allocating Dynamic Data
      2. Deleting Dynamic Data
      3. Constants and Dynamic Data
    4. 14.4 - Sequential Versus Linked Structures
    5. 14.5 - Creating a Dynamic Linked List: A Walk-Through Example
    6. 14.6 - Dynamic Implementation of ADT List
      1. Creating an Empty Linked List
      2. Inserting into a Linked List
      3. Traversals of a Linked List
      4. Deleting from a Linked List
      5. Resetting the List
      6. Getting the Next Item
      7. Testing for the Full Linked List
      8. Searching the List
    7. 14.7 - Destructors and Copy Constructors
      1. Destructor
      2. Shallow Versus Deep Copying
      3. Copy-Constructor
    8. 14.8 - Sorted Linked List
      1. Insert(20)
      2. Insert(60) (pick up with loop)
      3. Insert(100)
      4. Deleting from a Linked List
      5. Delete(30)
      6. Delete(50)
    9. Problem-Solving Case Study: Creating a Sorted List of Entry Objects
    10. Testing and Debugging
      1. Testing and Debugging Hints
    11. Summary
    12. Quick Check
    13. Answers
    14. Exam Preparation Exercises
    15. Programming Warm-Up Exercises
    16. Programming Problems
    17. Case Study Follow-Up
  20. 15 -Inheritance, Polymorphism, and Object-Oriented Design
    1. 15.1 - Object-Oriented Programming
    2. 15.2 - Inheritance
      1. An Analogy
      2. Inheritance and the Object-Oriented Design Process
      3. Deriving One Class from Another Class
      4. Specification of the ExpandedEntry Class
      5. Implementation of the ExpandedEntry Class
      6. Constructor Execution Order
    3. Software Maintenance Case Study: Extending TimeOfDay with Support for a Time Zone
    4. 15.3 - Dynamic Binding and Virtual Functions
      1. The Slicing Problem
      2. Virtual Functions
    5. 15.4 - Object-Oriented Design
      1. Brainstorming
      2. Filtering
      3. Scenario Exploration
      4. Responsibility Algorithms
      5. A Final Word
    6. 15.5 - Implementing a Design
      1. Problem-Solving Case Study: Creating an Appointment Calendar
      2. Testing and Debugging
        1. Testing and Debugging Hints
      3. Summary
      4. Quick Check
      5. Answers
      6. Exam Preparation Exercises
      7. Programming Warm-Up Exercises
      8. Programming Problems
      9. Case Study Follow-Up
  21. 16 -Templates, Operator Overloading, and Exceptions
    1. 16.1 - Template Classes
      1. Defining a Class Template
      2. Instantiating a Class Template
      3. Another Way of Implementing Incoming Parameters: const References
      4. Organization of Program Code
      5. A Word of Caution
    2. 16.2 - Generic Functions
      1. Function Overloading
      2. Defining a Function Template Outside a Class
      3. Instantiating a Function Template
    3. 16.3 - Operator Overloading
      1. Using *this
    4. 16.4 - Exceptions
      1. The throw Statement
      2. The try-catch Statement
      3. Nonlocal Exception Handlers
      4. Rethrowing an Exception
      5. Standard Exceptions
    5. Software Maintenance Case Study: Adding Exceptions to the Date Class
    6. Problem-Solving Case Study: Starship Weight and Balance
    7. Testing and Debugging
    8. Testing and Debugging Hints
    9. Summary
    10. Quick Check
    11. Answers
    12. Exam Preparation Exercises
    13. Programming Warm-Up Exercises
    14. Programming Problems
    15. Case Study Follow-Up
  22. 17 -Introduction to Data Structures Using the Standard Template Library
    1. 17.1 - Abstract Data Structures versus Implementations
    2. 17.2 - Additional Linear Structures
      1. Stacks
      2. Queues
      3. Priority Queues
    3. 17.3 - Bidirectional Linear Structures
      1. Bidirectional Lists
      2. Deques
    4. 17.4 - An Introduction to the STL
      1. Iterators
      2. The vector Template
      3. The list Template
      4. The stack Template
      5. The queue Template
      6. The priority_queue Template
      7. The deque Template
    5. Software Maintenance Case Study: Appointment Calendar Using STL List
    6. 17.5 - Nonlinear Structures
      1. Binary Trees
      2. Hash Tables
    7. 17.6 - Associative Containers
      1. The set Template
      2. The map Template
      3. Problem-Solving Case Study: Creating a Deck of Cards
      4. Testing and Debugging
      5. Testing and Debugging Hints
      6. Summary
      7. Quick Check
      8. Answers
      9. Exam Preparation Exercises
      10. Programming Warm-Up Exercises
      11. Programming Problems
      12. Case Study Follow-Up
  23. 18 -Recursion
    1. 18.1 - What is Recursion?
    2. 18.2 - Recursive Algorithms with Simple Variables
    3. 18.3 - Towers of Hanoi
    4. 18.4 - Recursive Algorithms with Structured Variables
      1. Software Maintenance Case Study: Substituting Binary Search for Linear Search
    5. 18.5 - Recursion Using Pointer Variables
      1. Printing a Dynamic Linked List in Reverse Order
      2. Copying a Dynamic Linked List
    6. 18.6 - Recursion or Iteration?
      1. Problem-Solving Case Study: Quicksort
      2. Testing and Debugging
      3. Testing and Debugging Hints
      4. Summary
      5. Quick Check
      6. Answers
      7. Exam Preparation Exercises
      8. Programming Warm-Up Exercises
      9. Programming Problems
      10. Case Study Follow-Up
  24. Appendices
  25. Index