内容简介
1Overview of Programming and Problem Solving
1.1 Overview of Programming
What Is Programming?
How Do We Write a Program?
1.2 What Is a Programming Language?
1.3 What Is a Computer?
1.4 Ethics and Responsibilities in the Computing Profession
Software Piracy
Privacy of Data
Use of Computer Resources
Software Engineering
1.5 Problem-Solving Techniques
Ask Questions
Look for Things That Are Familiar
Solve by Analogy
Means-Ends Analysis
Divide and Conquer
The Building-Block Approach
Merging Solutions
Mental Blocks: The Fear of Starting
Algorithmic Problem Solving
Summary
2C++Syntax and Semantics, and the Program Development Process
2.1 The Elements of C++ Programs
C++ Program Structure
Syntax and Semantics
Syntax Templates
Naming Program Elements: Identifiers
Data and Data Types
Naming Elements: Declarations
Taking Action: Executable Statements
Beyond Minimalism: Adding Comments to a Program
2.2 Program Construction
Blocks (Compound Statements)
The C++Preprocessor
An Introduction to Namespaces
2.3 More About Output
Creating Blank Lines
Inserting Blanks Within a Line
Programming Example: Contest Letter
Testing and Debugging
Summary
Quick Check
Exam Preparation Exercises
Programming Wann-up Exercises
Programming Problems
3Numeric Types, Expressions, and Output
3.1 Overview of C++Data Types
3.2 Numeric Data Types
Integral Types
Floating-Point Types
3.3 Declarations for Numeric Types
Named Constant Declarations
Variable Declarations
3.4 Simple Arithmetic Expressions
Arithmetic Operators
Increment and Decrement Operators
3.5 Compound Arithmetic Expressions
Precedence Rules
Type Coercion and Type Casting
3.6 Function Calls and Library Functions
Value-Retuming Functions
Library Functions
Void Functions
3.7 Formatting the Output
Integers and Strings
Floating-Point Numbers
3.8 Additional string Operations
The length and size Functions
The find Function
The substr Function
Programming Example: Map Measurements
Testing and Debugging
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
4Program Input and the Software Design Process
4.1 Getting Data into Programs
Input Streams and the Extraction Operator (>>)
The Reading Marker and the Newline Character
Reading Character Data with the get Function
Skipping Characters with the ignore Function
Reading String Data
4.2 Interactive Input/Output
4.3 Noninteractive Input/Output
4.4 File Input and Output
Files
Using Files
An Example Program Using Files
Run-Time Input of File Names
4.5 Input Failure
4.6 Software Design Methodologies
4.7 What Are Objects?
4.8 Object-Oriented Design
4.9 Functional Decomposition
Modules
A Perspective on Design
Programming Example: Stretching a Canvas
Testing and Debugging
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
5Conditions, Logical Expressions, and Selection Control Structures
5.1 Flow of Control
Selection
5.2 Conditions and Logical Expressions
The bool Data Type
Logical Expressions
Precedence of Operators
Relational Operators with Floating-Point Types
5.3 The If Statement
The If-Then-Else Form
Blocks (Compound Statements)
The If-Then Form
A Common Mistake
5.4 Nested If Statements
The Dangling else
5.5 Testing the State of an I/O Stream
Programming Example: Warning Notices
Testing and Debugging
Testing in the Problem-Solving Phase: The Algorithm Walk-Through
Testing in the Implementation Phase
The Test Plan
Tests Performed Automatically During Compilation and Execution
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
6Looping
6.1 The While Statement
6.2 Phases of Loop Execution
6.3 Loops Using the While Statement
Count-Controlled Loops
Event-Controlled Loops
Looping Subtasks
6.4 How to Design Loops
Designing the Flow of Control
Designing the Process Within the Loop
The Loop Exit
6.5 Nested Logic
Designing Nested Loops
Programming Example: Average Income by Gender
Testing and Debugging
Loop-Testing Strategy
Test Plans Involving Loops
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
7Functions
7.1 Functional Decomposition with Void Functions
Writing Modules as Void Functions
7.2 An Overview of User-Defined Functions
Flow of Control in Function Calls
Function Parameters
7.3 Syntax and Semantics of Void Functions
Function Call (Invocation)
Function Declarations and Denitions
Local Variables
The Return Statement
Header Files
7.4 Parameters
Value Parameters
Reference Parameters
7.5 Designing Functions
Writing Assertions as Program Comments
Documenting the Direction of Data Flow
Programming Example: Comparison of Furniture-Store Sales
Testing and Debugging
The assert Library Function
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
8Scope, Lifetime, and More on Functions
8.1 Scope of Identifiers
Scope Rules
Variable Declarations and Definitions
Namespaces
8.2 Lifetime of a Variable
Initializations in Declarations
8.3 Interface Design
Side Effects
Global Constants
8.4 Value-Returning Functions
Boolean Functions
Interface Design for Value-Retuming Functions
When to Use Value-Retuming Functions
Programming Example: Starship Weight and Balance
Testing and Debugging
Stubs and Drivers
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
9Additional Control Structures
9.1 The Switch Statement
9.2 The Do-While Statement
9.3 The For Statement
9.4 The Break and Continue Statements
9.5 Guidelines for Choosing a Looping Statement
Programming Example: Monthly Rainfall Averages
Testing and Debugging
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
10Simple Data Types: Built-In and User-Defined
10.1 Built-In Simple Types
Integral Types
Floating-Point Types
10.2 Additional C+++ Operators
Assignment Operators and Assignment Expressions
Increment and Decrement Operators
Bitwise Operators
The Cast Operation
The sizeof Operator
The ? : Operator
Operator Precedence
10.3 Working with Character Data
Character Sets
C++ char Constants
Programming Techniques
10.4 More on Floating-Point Numbers
Representation of Floating-Point Numbers
Arithmetic with Floating-Point Numbers
10.5 User-Defined Simple Types
The Typedef Statement
Enumeration Types
Named and Anonymous Data Types
User-Written Header Files
10.6 More on Type Coercion
Type Coercion in Arithmetic and Relational Expressions
Type Coercion in Assignments, Argument Passing,and Return of a Function Value
Programming Example: Rock, Paper, Scissors
Testing and Debugging
Floating-Point Data
Coping with Input Errors
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
11Structured Types, Data Abstraction, and Classes
11.1 Simple Versus Structured Data Types
11.2 Records (C++ Structs)
Accessing Individual Components
Aggregate Operations on Structs
More About Struct Declarations
Hierarchical Records
11.3 Unions
11.4 Data Abstraction
11.5 Abstract Data Types
11.6 C++ Classes
Classes, Class Objects, and Class Members
Built-in Operations on Class Objects
Class Scope
Information Hiding
11.7 Specification and Implementation Files
The Specification File
The Implementation File
Compiling and Linking a Multifile Program
11.8 Guaranteed Initialization with Class Constructors
Invoking a Constructor
Revised Specification and Implementation Files for TimeType
Guidelines for Using Class Constructors
Programming Example: Manipulating Dates
Programming Example: Birthday Calls
Testing and Debugging
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
12Arrays
12.1 One-Dimensional Arrays
Declaring Arrays
Accessing Individual Components
Out-of-Bounds Array Indexes
Initializing Arrays in Declarations
(Lack of) Aggregate Array Operations
Examples of Declaring and Accessing Arrays
Passing Arrays as Arguments
Assertions About Arrays
Using Typedef with Arrays
12.2 Arrays of Records and Class Objects
Arrays of Records
Arrays of Class Objects
12.3 Special Kinds of Array Processing
Subarray Processing
Indexes with Semantic Content
12.4 Two-Dimensional Arrays
12.5 Processing Two-Dimensional Arrays
Sum the Rows
Sum the Columns
Initialize the Array
Print the Array
12.6 Passing Two-Dimensional Arrays as Arguments
12.7 Another Way of Defining Two-Dimensional Arrays
12.8 Multidimensional Arrays
Programming Example: Comparison of Two Lists
Programming Example: City Council Election
Testing and Debugging
One-Dimensional Arrays
Complex Structures
Multidimensional Arrays
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
13Array-Based Lists
13.1 The List as an Abstract Data Type
13.2 Unsorted Lists
Basic Operations
Insertion and Deletion
Sequential Search
Sorting
13.3 Sorted Lists
Basic Operations
Insertion
Sequential Search
Binary Search
Deletion
13.4 Understanding Character Strings
Initializing C Strings
C String Input and Output
C String Library Routines
String Class or C Strings?
Programming Example: Exam Attendance
Testing and Debugging
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
14Object-Oriented Software Development
14.1 Object-Oriented Programming
14.2 Objects
14.3 Inheritance
Deriving One Class from Another
Specification of the ExtTime Class
Implementation of the ExtTime Class
Avoiding Multiple Inclusion of Header Files
14.4 Composition
Design of a TimeCard Class
Implementation of the TimeCard Class
14.5 Dynamic Binding and Virtual Functions
The Slicing Problem
Virtual Functions
14.6 Object-Oriented Design
Step 1: ldentify the Objects and Operations
Step 2: Determine the Relationships Among Objects
Step 3: Design the Driver
14.7 Implementing the Design
Programming Example: Time Card Lookup
Testing and Debugging
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
15Recursion
15.1 What Is Recursion?
15.2 Towers of Hanoi
15.3 Recursive Algorithms with Structured Variables
15.4 Recursion or Iteration?
Testing and Debugging
Testing and Debugging Hints
Summary
Quick Check
Exam Preparation Exercises
Programming Warm-up Exercises
Programming Problems
Appendix A Reserved Words
Appendix B Operator Precedence
Appendix C A Selection of Standard Library Routines
Appendix D Using This Book with a Prestandard Version of C++
Appendix E Character Sets
Appendix F Program Style, Formatting, and Documentation
Glossary
Answers to Selected Exercises
Index