| |
| |
| |
Introduction to Computers and C++ Programming | |
| |
| |
| |
Computer Systems | |
| |
| |
Hardware | |
| |
| |
Software | |
| |
| |
High-Level Languages | |
| |
| |
Compilers | |
| |
| |
History Note | |
| |
| |
| |
Programming and Problem-Solving | |
| |
| |
Algorithms | |
| |
| |
Program Design | |
| |
| |
Object-Oriented Programming | |
| |
| |
The Software Life Cycle | |
| |
| |
| |
Introduction to C++ | |
| |
| |
Origins of the C++ Language | |
| |
| |
A Sample C++ Program | |
| |
| |
Pitfall: Using the Wrong Slash in \n | |
| |
| |
Programming Tip: Input and Output Syntax | |
| |
| |
Layout of a Simple C++ Program | |
| |
| |
Pitfall: Putting a Space before the include File Name | |
| |
| |
Compiling and Running a C++ Program | |
| |
| |
Programming Tip: Getting Your Program to Run | |
| |
| |
| |
Testing and Debugging | |
| |
| |
Kinds of Program Errors | |
| |
| |
Pitfall: Assuming Your Program Is Correct | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
C++ Basics | |
| |
| |
| |
Variables and Assignments | |
| |
| |
Variables | |
| |
| |
Names: Identifiers | |
| |
| |
Variable Declarations | |
| |
| |
Assignment Statements | |
| |
| |
Pitfall: Uninitialized Variables | |
| |
| |
Programming Tip: Use Meaningful Names | |
| |
| |
| |
Input and Output | |
| |
| |
Output Using cout | |
| |
| |
Include Directives and Namespaces | |
| |
| |
Escape Sequences | |
| |
| |
Programming Tip: End Each Program with a \n or endl | |
| |
| |
Formatting for Numbers with a Decimal Point | |
| |
| |
Input Using cin | |
| |
| |
Designing Input and Output | |
| |
| |
Programming Tip: Line Breaks in I/O | |
| |
| |
| |
Data Types and Expressions | |
| |
| |
The Types int and double | |
| |
| |
Other Number Types | |
| |
| |
The Type char | |
| |
| |
The Type bool | |
| |
| |
Introduction to the Class string | |
| |
| |
Type Compatibilities | |
| |
| |
Arithmetic Operators and Expressions | |
| |
| |
Pitfall: Whole Numbers in Division | |
| |
| |
More Assignment Statements | |
| |
| |
| |
Simple Flow of Control | |
| |
| |
A Simple Branching Mechanism | |
| |
| |
Pitfall: Strings of Inequalities | |
| |
| |
Pitfall: Using = in place of == | |
| |
| |
Compound Statements | |
| |
| |
Simple Loop Mechanisms | |
| |
| |
Increment and Decrement Operators | |
| |
| |
Programming Example: Charge Card Balance | |
| |
| |
Pitfall: Infinite Loops | |
| |
| |
| |
Program Style | |
| |
| |
Indenting | |
| |
| |
Comments | |
| |
| |
Naming Constants | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
More Flow of Control | |
| |
| |
| |
Using Boolean Expressions | |
| |
| |
Evaluating Boolean Expressions | |
| |
| |
Pitfall: Boolean Expressions Convert to int Values | |
| |
| |
Enumeration Types (Optional) | |
| |
| |
| |
Multiway Branches | |
| |
| |
Nested Statements | |
| |
| |
Programming Tip: Use Braces in Nested Statements | |
| |
| |
Multiway if-else Statements | |
| |
| |
Programming Example: State Income Tax | |
| |
| |
The switch Statement | |
| |
| |
Pitfall: Forgetting a break in a switch Statement | |
| |
| |
Using switch Statements for Menus | |
| |
| |
Blocks | |
| |
| |
Pitfall: Inadvertent Local Variables | |
| |
| |
| |
More About C++ Loop Statements | |
| |
| |
The while Statements Reviewed | |
| |
| |
Increment and Decrement Operators Revisited | |
| |
| |
The for Statement | |
| |
| |
Pitfall: Extra Semicolon in a for Statement | |
| |
| |
What Kind of Loop to Use | |
| |
| |
Pitfall: Uninitialized Variables and Infinite Loops | |
| |
| |
The break Statement | |
| |
| |
Pitfall: The break Statement in Nested Loops | |
| |
| |
| |
Designing Loops | |
| |
| |
Loops for Sums and Products | |
| |
| |
Ending a Loop | |
| |
| |
Nested Loops | |
| |
| |
Debugging Loops | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Procedural Abstraction and Functions That Return a Value | |
| |
| |
| |
Top-Down Design | |
| |
| |
| |
Predefined Functions | |
| |
| |
Using Predefined Functions | |
| |
| |
Type Casting | |
| |
| |
Older Form of Type Casting | |
| |
| |
Pitfall: Integer Division Drops the Fractional Part | |
| |
| |
| |
Programmer-Defined Functions | |
| |
| |
Function Definitions | |
| |
| |
Functions That Return a Boolean Value | |
| |
| |
Alternate Form for Function Declarations | |
| |
| |
Pitfall: Arguments in the Wrong Order | |
| |
| |
Function Definition-Syntax Summary | |
| |
| |
More About Placement of Function Definitions | |
| |
| |
Programming Tip: Use Function Calls in Branching Statements | |
| |
| |
| |
Procedural Abstraction | |
| |
| |
The Black Box Analogy | |
| |
| |
Programming Tip: Choosing Formal Parameter Names | |
| |
| |
Programming Tip: Nested Loops | |
| |
| |
Case Study: Buying Pizza | |
| |
| |
Programming Tip: Use Pseudocode | |
| |
| |
| |
Local Variables | |
| |
| |
The Small Program Analogy | |
| |
| |
Programming Example: Experimental Pea Patch | |
| |
| |
Global Constants and Global Variables | |
| |
| |
Call-by-Value Formal Parameters Are Local Variables | |
| |
| |
Namespaces Revisited | |
| |
| |
Programming Example: The Factorial Function | |
| |
| |
| |
Overloading Function Names | |
| |
| |
Introduction to Overloading | |
| |
| |
Programming Example: Revised Pizza-Buying Program | |
| |
| |
Automatic Type Conversion | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Functions for All Subtasks | |
| |
| |
| |
Void Functions | |
| |
| |
Definitions of void Functions | |
| |
| |
Programming Example: Converting Temperatures | |
| |
| |
Return Statements in void Functions | |
| |
| |
| |
Call-by-Reference Parameters | |
| |
| |
A First View of Call-by-Reference | |
| |
| |
Call-by-Reference in Detail | |
| |
| |
Programming Example: The swap_values Function | |
| |
| |
Mixed Parameter Lists | |
| |
| |
Programming Tip: What Kind of Parameter to Use | |
| |
| |
Pitfall: Inadvertent Local Variables | |
| |
| |
| |
Using Procedural Abstraction | |
| |
| |
Functions Calling Functions | |
| |
| |
Preconditions and Postconditions | |
| |
| |
Case Study: Supermarket Pricing | |
| |
| |
| |
Testing and Debugging Functions | |
| |
| |
Stubs and Drivers | |
| |
| |
| |
General Debugging Techniques | |
| |
| |
Keep an Open Mind | |
| |
| |
Check Common Errors | |
| |
| |
Localize the Error | |
| |
| |
The assert macro | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
I/O Streams as an Introduction to Objects and Classes | |
| |
| |
| |
Streams and Basic File I/O | |
| |
| |
Why Use Files for I/O? | |
| |
| |
File I/O | |
| |
| |
Introduction to Classes and Objects | |
| |
| |
Programming Tip: Check Whether a File Was Opened Successfully | |
| |
| |
Techniques for File I/O | |
| |
| |
Appending to a File (Optional) | |
| |
| |
File Names as Input (Optional) | |
| |
| |
| |
Tools for Stream I/O | |
| |
| |
Formatting Output with Stream Functions | |
| |
| |
Manipulators | |
| |
| |
Streams as Arguments to Functions | |
| |
| |
Programming Tip: Checking for the End of a File | |
| |
| |
A Note on Namespaces | |
| |
| |
Programming Example: Cleaning Up a File Format | |
| |
| |
| |
Character I/O | |
| |
| |
The Member Functions get and put | |
| |
| |
The putback Member Function (Optional) | |
| |
| |
Programming Example: Checking Input | |
| |
| |
Pitfall: Unexpected '\n' in Input | |
| |
| |
The eof Member Function | |
| |
| |
Programming Example: Editing a Text File | |
| |
| |
Predefined Character Functions | |
| |
| |
Pitfall: toupper and tolower Return Values | |
| |
| |
| |
Inheritance | |
| |
| |
Inheritance Among Stream Classes | |
| |
| |
Programming Example: Another new_line Function | |
| |
| |
Default Arguments for Functions (Optional) | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Arrays | |
| |
| |
| |
Introduction to Arrays | |
| |
| |
Declaring and Referencing Arrays | |
| |
| |
Programming Tip: Use for Loops with Arrays | |
| |
| |
Pitfall: Array Indexes Always Start with Zero | |
| |
| |
Programming Tip: Use a Defined Constant for the Size of an Array | |
| |
| |
Arrays in Memory | |
| |
| |
Pitfall: Array Index Out of Range | |
| |
| |
Initializing Arrays | |
| |
| |
| |
Arrays in Functions | |
| |
| |
Indexed Variables as Function Arguments | |
| |
| |
Entire Arrays as Function Arguments | |
| |
| |
The const Parameter Modifier | |
| |
| |
Pitfall: Inconsistent Use of const Parameters | |
| |
| |
Functions That Return an Array | |
| |
| |
Case Study: Production Graph | |
| |
| |
| |
Programming with Arrays | |
| |
| |
Partially Filled Arrays | |
| |
| |
Programming Tip: Do Not Skimp on Formal Parameters | |
| |
| |
Programming Example: Searching an Array | |
| |
| |
Programming Example: Sorting an Array | |
| |
| |
| |
Multidimensional Arrays | |
| |
| |
Multidimensional Array Basics | |
| |
| |
Multidimensional Array Parameters | |
| |
| |
Programming Example: Two-Dimensional Grading Program | |
| |
| |
Pitfall: Using Commas Between Array Indexes | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Strings and Vectors | |
| |
| |
| |
An Array Type for Strings | |
| |
| |
C-String Values and C-String Variables | |
| |
| |
Pitfall: Using = and == with C Strings | |
| |
| |
Other Functions in <cstring> | |
| |
| |
C-String Input and Output | |
| |
| |
C-String-to-Number Conversions and Robust Input | |
| |
| |
| |
The Standard string Class | |
| |
| |
Introduction to the Standard Class string | |
| |
| |
I/O with the Class string | |
| |
| |
Programming Tip: More Versions of getline | |
| |
| |
Pitfall: Mixing cin [double greater-than sign] variable; and getline | |
| |
| |
String Processing with the Class string | |
| |
| |
Programming Example: Palindrome Testing | |
| |
| |
Converting between string Objects and C Strings | |
| |
| |
| |
Vectors | |
| |
| |
Vector Basics | |
| |
| |
Pitfall: Using Square Brackets Beyond the Vector Size | |
| |
| |
Programming Tip: Vector Assignment Is Well Behaved | |
| |
| |
Efficiency Issues | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Pointers and Dynamic Arrays | |
| |
| |
| |
Pointers | |
| |
| |
Pointer Variables | |
| |
| |
Basic Memory Management | |
| |
| |
Pitfall: Dangling Pointers | |
| |
| |
Static Variables and Automatic Variables | |
| |
| |
Programming Tip: Define Pointer Types | |
| |
| |
| |
Dynamic Arrays | |
| |
| |
Array Variables and Pointer Variables | |
| |
| |
Creating and Using Dynamic Arrays | |
| |
| |
Pointer Arithmetic (Optional) | |
| |
| |
Multidimensional Dynamic Arrays (Optional) | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Defining Classes | |
| |
| |
| |
Structures | |
| |
| |
Structures for Diverse Data | |
| |
| |
Pitfall: Forgetting a Semicolon in a Structure Definition | |
| |
| |
Structures as Function Arguments | |
| |
| |
Programming Tip: Use Hierarchical Structures | |
| |
| |
Initializing Structures | |
| |
| |
| |
Classes | |
| |
| |
Defining Classes and Member Functions | |
| |
| |
Public and Private Members | |
| |
| |
Programming Tip: Make All Member Variables Private | |
| |
| |
Programming Tip: Define Accessor and Mutator Functions | |
| |
| |
Programming Tip: Use the Assignment Operator with Objects | |
| |
| |
Programming Example: BankAccount Class-Version 1 | |
| |
| |
Summary of Some Properties of Classes | |
| |
| |
Constructors for Initialization | |
| |
| |
Programming Tip: Always Include a Default Constructor | |
| |
| |
Pitfall: Constructors with No Arguments | |
| |
| |
| |
Abstract Data Types | |
| |
| |
Classes to Produce Abstract Data Types | |
| |
| |
Programming Example: Alternative Implementation of a Class | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Friends, Overloaded Operators, and Arrays in Classes | |
| |
| |
| |
Friend Functions | |
| |
| |
Programming Example: An Equality Function | |
| |
| |
Friend Functions | |
| |
| |
Programming Tip: Define Both Accessor Functions and Friend Functions | |
| |
| |
Programming Tip: Use Both Member and Nonmember Functions | |
| |
| |
Programming Example: Money Class (Version 1) | |
| |
| |
Implementation of digit_to_int (Optional) | |
| |
| |
Pitfall: Leading Zeros in Number Constants | |
| |
| |
The const Parameter Modifier | |
| |
| |
Pitfall: Inconsistent Use of const | |
| |
| |
| |
Overloading Operators | |
| |
| |
Overloading Operators | |
| |
| |
Constructors for Automatic Type Conversion | |
| |
| |
Overloading Unary Operators | |
| |
| |
Overloading [double greater-than sign] and [double less-than sign] | |
| |
| |
| |
Arrays and Classes | |
| |
| |
Arrays of Classes | |
| |
| |
Arrays as Class Members | |
| |
| |
Programming Example: A Class for a Partially Filled Array | |
| |
| |
| |
Classes and Dynamic Arrays | |
| |
| |
Programming Example: A String Variable Class | |
| |
| |
Destructors | |
| |
| |
Pitfall: Pointers as Call-by-Value Parameters | |
| |
| |
Copy Constructors | |
| |
| |
Overloading the Assignment Operator | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Separate Compilation and Namespaces | |
| |
| |
| |
Separate Compilation | |
| |
| |
ADTs Reviewed | |
| |
| |
Case Study: DigitalTime-A Class Compiled Separately | |
| |
| |
Using #ifndef | |
| |
| |
Programming Tip: Defining Other Libraries | |
| |
| |
| |
Namespaces | |
| |
| |
Namespaces and using Directives | |
| |
| |
Creating a Namespace | |
| |
| |
Qualifying Names | |
| |
| |
A Subtle Point About Namespaces (Optional) | |
| |
| |
Unnamed Namespaces | |
| |
| |
Programming Tip: Choosing a Name for a Namespace | |
| |
| |
Pitfall: Confusing the Global Namespace and the Unnamed Namespace | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Pointers and Linked Lists | |
| |
| |
| |
Nodes and Linked Lists | |
| |
| |
Nodes | |
| |
| |
Linked Lists | |
| |
| |
Inserting a Node at the Head of a List | |
| |
| |
Pitfall: Losing Nodes | |
| |
| |
Searching a Linked List | |
| |
| |
Pointers as Iterators | |
| |
| |
Inserting and Removing Nodes Inside a List | |
| |
| |
Pitfall: Using the Assignment Operator with Dynamic Data Structures | |
| |
| |
Variations on Linked Lists | |
| |
| |
Linked Lists of Classes | |
| |
| |
| |
Stacks and Queues | |
| |
| |
Stacks | |
| |
| |
Programming Example: A Stack Class | |
| |
| |
Queues | |
| |
| |
Programming Example: A Queue Class | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Recursion | |
| |
| |
| |
Recursive Functions for Tasks | |
| |
| |
Case Study: Vertical Numbers | |
| |
| |
A Closer Look at Recursion | |
| |
| |
Pitfall: Infinite Recursion | |
| |
| |
Stacks for Recursion | |
| |
| |
Pitfall: Stack Overflow | |
| |
| |
Recursion Versus Iteration | |
| |
| |
| |
Recursive Functions for Values | |
| |
| |
General Form for a Recursive Function That Returns a Value | |
| |
| |
Programming Example: Another Powers Function | |
| |
| |
| |
Thinking Recursively | |
| |
| |
Recursive Design Techniques | |
| |
| |
Case Study: Binary Search-An Example of Recursive Thinking | |
| |
| |
Programming Example: A Recursive Member Function | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Inheritance | |
| |
| |
| |
Inheritance Basics | |
| |
| |
Derived Classes | |
| |
| |
Constructors in Derived Classes | |
| |
| |
Pitfall: Use of Private Member Variables from the Base Class | |
| |
| |
Pitfall: Private Member Functions Are Effectively Not Inherited | |
| |
| |
The protected Qualifier | |
| |
| |
Redefinition of Member Functions | |
| |
| |
Redefining Versus Overloading | |
| |
| |
Access to a Redefined Base Function | |
| |
| |
| |
Inheritance Details | |
| |
| |
Functions That Are Not Inherited | |
| |
| |
Assignment Operators and Copy Constructors in Derived Classes | |
| |
| |
Destructors in Derived Classes | |
| |
| |
| |
Polymorphism | |
| |
| |
Late Binding | |
| |
| |
Virtual Functions in C++ | |
| |
| |
Virtual Functions and Extended Type Compatibility | |
| |
| |
Pitfall: The Slicing Problem | |
| |
| |
Pitfall: Not Using Virtual Member Functions | |
| |
| |
Pitfall: Attempting to Compile Class Definitions Without Definitions for Every Virtual Member Function | |
| |
| |
Programming Tip: Make Destructors Virtual | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Exception Handling | |
| |
| |
| |
Exception-Handling Basics | |
| |
| |
A Toy Example of Exception Handling | |
| |
| |
Defining Your Own Exception Classes | |
| |
| |
Multiple Throws and Catches | |
| |
| |
Pitfall: Catch the More Specific Exception First | |
| |
| |
Programming Tip: Exception Classes Can Be Trivial | |
| |
| |
Throwing an Exception in a Function | |
| |
| |
Exception Specification | |
| |
| |
Pitfall: Exception Specification in Derived Classes | |
| |
| |
| |
Programming Techniques for Exception Handling | |
| |
| |
When to Throw an Exception | |
| |
| |
Pitfall: Uncaught Exceptions | |
| |
| |
Pitfall: Nested try-catch Blocks | |
| |
| |
Pitfall: Overuse of Exceptions | |
| |
| |
Exception Class Hierarchies | |
| |
| |
Testing for Available Memory | |
| |
| |
Rethrowing an Exception | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Templates | |
| |
| |
| |
Templates for Algorithm Abstraction | |
| |
| |
Templates for Functions | |
| |
| |
Pitfall: Compiler Complications | |
| |
| |
Programming Example: A Generic Sorting Function | |
| |
| |
Programming Tip: How to Define Templates | |
| |
| |
Pitfall: Using a Template with an Inappropriate Type | |
| |
| |
| |
Templates for Data Abstraction | |
| |
| |
Syntax for Class Templates | |
| |
| |
Programming Example: An Array Class | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
| |
Standard Template Library | |
| |
| |
| |
Iterators | |
| |
| |
Using Declarations | |
| |
| |
Iterator Basics | |
| |
| |
Kinds of Iterators | |
| |
| |
Constant and Mutable Iterators | |
| |
| |
Reverse Iterators | |
| |
| |
Pitfall: Compiler Problems | |
| |
| |
Other Kinds of Iterators | |
| |
| |
| |
Containers | |
| |
| |
Sequential Containers | |
| |
| |
Pitfall: Iterators and Removing Elements | |
| |
| |
Programming Tip: Type Definitions in Containers | |
| |
| |
Container Adapters stack and queue | |
| |
| |
Associative Containers set and map | |
| |
| |
Efficiency | |
| |
| |
| |
Generic Algorithms | |
| |
| |
Running Times and Big-O Notation | |
| |
| |
Container Access Running Times | |
| |
| |
Nonmodifying Sequence Algorithms | |
| |
| |
Container Modifying Algorithms | |
| |
| |
Set Algorithms | |
| |
| |
Sorting Algorithms | |
| |
| |
Chapter Summary | |
| |
| |
Answers to Self-Test Exercises | |
| |
| |
Programming Projects | |
| |
| |
Appendices | |
| |
| |
| |
C++ Keywords | |
| |
| |
| |
Precedence of Operators | |
| |
| |
| |
The ASCII Character Set | |
| |
| |
| |
Some Library Functions | |
| |
| |
| |
Inline Functions | |
| |
| |
| |
Overloading the Array Index Square Brackets | |
| |
| |
| |
The this Pointer | |
| |
| |
| |
Overloading Operators as Member Operators | |
| |
| |
Index | |