内容简介
Chapter 1 C++ BASICS
1.1 INTRODUCTION TO C+
Origins of the C++ Language
C++ and Object-Oriented Programming
The Character Of C+++
C++ Terminology y
A Sample C++ Program y
1.2 VARIABLES, EXPRESSIONS, AND ASSIGNMENT STATEMENTS
Identifiers
Variables
Assignment Statements
Pitfall: Uninitialized Variables
Tip: Use Meaningful Names
More Assignment Statements
Assignment Compatibility
Literals
Escape Sequences
Naming Constants
Arithmetic Operators and Expressions
Integer and Floating-Point Division
Pitfall: Division and whole Number
Type Casting
Increment and Decrement Operators
Pitfall: Order of Evaluation
1.3 CONSOLE NPT/OUPU
output Using cout
New Lines in Output
Tip: End Each Program with \n or endl
Formaing for Numbers with a Decimal Point
Output with cerr
Input Using cin
Tip: Line Breaks in l/O
1.4 PROGRAM STYLE
Comments
1.5 LIBRARIES AND NAMESPACES
Libraries and include Directives
Namespaces
Pitfall: Problems with Libra Names
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter 2 FLOW Of CONTROL
2.1 BOOLEAN EXPRESSION
Building Boolean Expressions
Pitfall: Strings of Inequalities
Evaluating Boolean Expressions
Precedence Rule
Pitfall: Integer Values Can Be Used as Boolean Values
2.2 BRANCHING MECHANISMS
if-else Statements
Compound Statements
Pitfall: Using in Place of - -
Omiing the else
Nested Statement
Multiway if-else Statement
The switch Statement
Pitfall: Forgeing a break in a switch Statement
Tip: Use switch Statements for Menu
Enumeration Types
The Conditional Operation
2.3 LOOPS
The while and do-while Statements
Increment and Deement Operators Revisited
The Comma Operator
The for Statement
Tip: Repeat-N-Times Loops
Pitfall: Extra Semicolon in a for Statement
Pitfall: infinite Loops
The break and continue Statement
Nested Loops
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter 3 FUNCTION BASICS
3.1 PREDEFINED FUNCTIONS
Predefined Funions That Retu a Value
Predefined void Functions
A Random Number Generator
3.2 PROGRAMMER-DEFINED FUNCTIONS
Defining Functions That Return a Value
Alternate Form for Function Declarations
Pitfall: Arguments in the Wrong Order m
Pitfall: Use the Terms Parameterand Argument in
Functctions Calling Functions
Example: A Rounding Function
Functions That Return a Boolean Value
Defining void Functions
retuct Statements in void Funions
Preconditions and Postconditions
main is a Function
Recursive Functions
3.3 SCOPE RULES
Local Variables
Procedural Abstraion
Global Constants and Global Variables
Blocks
Nested Scopes
Tip: Use Funion Calls in Branching and Loop Statements
Variables Declared in a for Loop
Chapter Summary
Answers to Self-Test Exercises
Programming Projects
Chapter 4 PARAMETERS AND OVERLOADING
4.1 PARAMETERS
Call-by-Value Parameters
A Fit Look at Call-by-Reference Parameters
Call-by-Reference Mechanism in Detail
Constant Reference Parameters
Example: The swapValues Function
Tip: Think of Actions, Not Code
Mixed Parameter Lists
Tip: What Kind of Parameter to Use
Pitfall: Inadvertent Local Variables
Tip: Choosing Formal Parameter Names
Example: Buying Pizza
4.2 OVERLOADING AND DEFAULT ARGUMENTS
Introduction to overloading
Pitfall: Automatic Type Conversion and Overloading
Rules for Resolving overloading
Example: Revised Pizza-Buying Program
Default Arguments
4.3 TESTING AND DEBUGGING FUNCTIONS
The assert Mao
Stubs and Drivers
Chapter Summary
Answers to Self-Test Exercises
Programming Projects
Chapter5 ARRAYS
5.1 INTRODluCTION TO ARRAYS
Declaring and Referencing Arrays
Tip: Use for Loops with Aays
Pitfall: Aay Indexes Always Sta with Zero
Tip: Use a Defined Constant for the Size of an Aay
Aays in Memo
Pitfall: Aay Index Out of Range
Initializing Aays
5.2 ARRAYS IN FUNCTIONS
Indexed Variables as Funion Arguments
Entire Aays as Funion Arguments
TheconstParameter Modifier
Pitfall: Inconsistent Use of const Paramete
Functions That Retum an Array
Example: Production Graph
5.3 PROGRAONG WITH ARRAYS
Paially Filled Arrays
Tip: Do Not Skimp on Formal Paramete
Example: Searching an Array
Example: Soing an Array
5.4 MULTIDlAENSIONAL ARRAYS
Multidimensional Array Basics
Multidimensional Array Parameters
Example: Two-Dimensional Grading Program
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter 6 STRUCTURES AND CLASSES
6.1 STRUCTURES
Structure Types
Pitfall: Forgeing a Semicolon in a Structure Definition
Structures as Function Arguments
Tip: Use Hierarchical Structures
Initializing Structures
6.2 CLASSES
Defining Classes and Member Functions
Encapsulation
Public and Private Members
Accessor and Mutator Functions
Tip: Separate Interface and implementation
Tip: A Test for Encapsulation
Structures versus Classes
Tip: Thinking Objects
Chapter Summary
Answers to Self-Test Exercises
Programming Projects
Chapter 7 CONSTRUCTORS AND OTHER TOOLS
7.1 CONSTRUCTORS
ConstructoTDefinitions
Pitfall: Constructors with No Arguments
Explicit Constructor Calls
Tip: Always Include a Default Constructor
Example: BonkAccount Class
Class Type Member Variables
7.2 MORE TOOLS
The const Parameter Modifier
Pitfall: Inconsistent Use of const
Inline Functions
Static Members
Nested and Local Class Definitions
7.3 VECTORS—A PREVIEWS OF THE STANDARD TEMPLATE LIBRARY
Vector Basics
Pitfall: Using Square Brackets beyond the Vector Size
Tip: Vector Assignment Is Well Behaved
Efficiency Issues
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter 8 OPERATOR OVERLOADING, FRIENDS, AND REFERENCES
8.1 BASIC OPERATOR OVERLOADING
Overloading Basics
Tip: A Constructor Can Return an Object
Returning by const Value
Tip: Retuing Member Variables of a Class Type
Overloading Una Operators
Overloading as Members Functions
Tip: A Class Has Access to All its objects
Overloading Function Application ( )
Pitfall: Overloading , ll, and the Comma Operator
8.2 FRIEND FUNCTIONS AND AUTOMATk TYPE CONVERSION
Constructors for Automatic Type Conversion
Pitfall: Member Operators and Automatic Type Conversion
Friend Functions
Pitfall: Compilers without Friends
Friend Classes
8.3 REFERENCES AND MORE OVERLOADED OPERATORS
References
Pitfall: Returning a Reference to Ceain Member Variables
Overloading and
Tip: What Mode of Retued Value to Use
The Assignment Operator
Overloading the Increment and Deement Operato
Overloading the Array operator
Overloading Based on L-Value veus R-value
Chapter Summa
Answers to self-Test Exercises
Programming Projects
Chapter 9 STRINGS
9.1 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
9.2 CHARACTER MANIPULATION TOOLS
Character l/O
The Member Functions get and put
Example: Checking Input Using a Newfine Function
Pitfall: Unexpected \n in Input
The putback, peek, and ignore Member Functions
Character-Manipulating Functions
Pitfall: toupper and tolower Retu int Values
9.3 THE STANDARD CLASS string
Introduction to the Standard Class string
l/O with the Class string
Tip: More Versions of getline
Pitfall: Mixing cin variable; and getline
String Processing with the Class string
Example: Palindrome Testing
Converting between string Objes and C-Strings
Chapter Summa
Answers to Self-Test Exercises
Programming Projes
Chapter 10 POINTERS AND DYNAMIC ARRAYS
10.1 POINTERS
Pointer Variables
Basic Memo Management
Pitfall: Dangling Pointers
Dynamic Variables and Automatic Variables
Tip: Define Pointer Types
Pitfall: Pointers as Call-by-Value Parameters
Uses for Pointers
10.2 DYNAMIC ARRAYS
Array Variables and Pointer Variables
Creating and Using Dynamic Aays
Example: A Function That Retus an Aay
Pointer Arithmetic
Multidimensional Dynamic Aays
10.3 CLASSES POINTERS AND DYNAC ARRAYS
The -> Operator
The this Pointer
Overloading the Assignment Operator
Example: A Class for Paially Filled Aays
Destructors
Copy Constructors
Chapter Summary
Answers to Self-Test Exercises
Programming Projects
Chapter 11 SEPARATE COMPILATION AND NAMESPACES
11.1SEPARATE COMPILATION
Encapsulation Reviewed
Header Files and Implementation Files
Example: DigitalTime Class
Tip: Reusable Components
Using #ifndef
Tip: Defining Other Libraries
11.2 NAMESPACES
Namespaces and using Directives
Creating a Namespace
Using Declarations
Qualifying Names
Tip: Choosing a Name for a Namespace
Example: A Class Definition in a Namespace
Unnamed Namespaces
Pitfall: Confusing the Global Namespace and the Unnamed Namespace
Tip: Unnamed Namespaces Replace the static Qualifer
Tip: Hiding Helping Functions
Nested Namespaces
Tip: What Namespace Specification Should You Use?
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter 12 STREAMS AND FILE l/O
12.1 l/O STREAMS
File l/O
Pitfall: Restrictions on Stream Variables
Appending to a File
Tip: Another Syntax for Opening a File
Tip: Check That a File Was Opened Successfully
Character l/O
Checking for the End of a File
12.2 TOOLS FOR STREAM l/O
File Names as Input
Formaing output with Stream Functions
Manipulators
Saving Flag Seings
More Output Stream Member Functions
Example: Cleaning Up a File Format
Example: Editing a Text File
12.3 STREAM HIERARCHIES: A PREVIEW OF INHERITANCE
Inheritance among Stream Classes
Example: Another newline Function
12.4 RANDOM ACCESS TO FILES
Chapter Summa
Answe to Self-Test Exercises
Programming Projects
Chapter 13 RECURSION
13.1 RECURSIVE void FUNCTIONS
Example: Vertical Numbers
Tracing a Recursive Call
A Closer look at Recuion
Pitfall: Infinite Recursion
Stacks for Recuion
Pitfall: Stack overflow
Recursion versus iteration
13.2 RECURSIVE FUNCTIONS THAT RETURN A VALUE
General Form for a Recursive Function That Retus a Value
Example: Another Powers Function
13.3 THINKING RECURSIVELY
Recursive Design Techniques
Bina Search
Chapter Summa
Answe to Self-Test Exercises
Programming Projects
Chapter 14 INHERITANCE
14.1 INHERITANCE BASICSDerived Classes
Constructo 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 veus Overloading
Access to a Redefined Base Function
Functions That Are Not Inherited
14.2 PROGRAMANING WITH INHERITANCE
Assignment Operato and Copy Constructors in Derived Classes
Destructors in Derived Classes
Example: Paially Filled Array with Backup
Pitfall: Same Object on Both Sides of the Assignment Operator
Example: Alteate Implementation of PFArrayDBak
Tip: A Class Has Access to Private Membe of All Objects of the Class
Tip: “Is a” versus “Has a”
Protected and Private Inheritance
Multiple Inheritance
Chapter Summa
Answe to Self-Test Exercises
Programming Projects
Chapter15 POLYMORPHISM AND VIRTUAL FUNCTIONS
15.1 VIRTUAL FUNCTION BASICS
LAte Binding
Virtual Functions in C+++
Tip: The Virtual Property Is Inherited
Tip: When to Use a Virtual Function
Pitfall: Omiing the Definition of a Virtual Member Function
Abstract Classes and Pure Viual functions
Example: An Abstract Class
15.2 POINTERS AND VIRTU FUNCTIONS
Virtual Functions and Extended Type Compatibility
Pitfall: The Slicing Problem
Tip: Make Destructo Viual
Downcasting and Upcasting
How C++ Implements Viual Functions
Chapter Summa
Answe to Self-Test Exeises
Programming Projects
Chapter 16 TEMPLATES
16.1 FUNCTION TEMPLATES
Syntax for Function Templates
Pitfall: Compiler Complitions
Tip: How to Define Templates
Example: A Generic Soing Funion
Pitfall: Using a Template with an Inappropriate Type
16.2 CLASS TEMPLATES
Syntax for Class Templates
Example: An Aay Template Class
The vector and basic-string Templates
16.3 TEMPALTES AND INHERITANCE
Example: Template Class for a Partially Filled Array with Backup
Chapter Summary
Answers to Self-Test Exercises
Programming Projects
Chapter 17 LINKED DATA STRUCTURES
17.1 NODES AND LINKED LISTS
Nodes
Linked Lists
Inserting a Node at the Head of a List
Pitfall: Losing Nodes
Inseing and Removing Nodes Inside a List
Pitfall: Using the Assignment Operator with Dynamic Data Structures
Searching a Linked List
Example: A Generic Soing Template Version of Linked List Tools
17.2 LINKED LIST APPLICATIONS
Example: A Stack Template Class
Example: A Queue Template Class
Tip: A Comment on Namespaces
Friend Classes and Similar Alternatives
17.3 ITERATORS
Pointers as Iterators
Iterator Classes
Example: An Iterator Class
17.4 TREES
Tree Propeies
Example: A Tree Template Class
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter18 EXCEPTION HANDLING
18.1 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
Tip: Exception Classes Can Be Trivial
Throwing an Exception in a Function
Exception Specification
Pitfall: Exception Specification in Derived Classes
18.2 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 Memo
Rethrowing an Exception
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter19 STANDARD TEMPLATE LIBRARY
19.1 ITERATORS
Iterator Basi
Kinds of Iterators
Constant and Mutable Iterators
Revee Iterators
Pitfall: Compiler Problems
Other Kinds of Iterato
19.2 CONTAINERS
Sequential Containers
Pitfall: Iterato and Removing Elements
Tip: Type Definitions in Containers
The Container Adapters stack and queue
The Associative Containe set and nap
Efficiency
19.3 GENERc: ALGORITHMS
Running Times and Big-ONotation
Container Access Running Times
Nonmodifying Sequence Algorithms
Modifying Sequence Algorithms
Set Algorithms
Soing Algorithms
Chapter Summa
Answers to Self-Test Exercises
Programming Projects
Chapter 20 PATENS AND UML
20.1 PATTERNS
Adapter Patte
The Model-View-Controller Paern
Example: A Soing Pattern
Efficiency of the Soing Pattern
Tip: Pragmatics and Paes
Pattern Formalism
20.2 UML
Histo of UML
UML Class Diagrams
Class Interactions
Chapter Summa
Answe to Self-Test Exercises
Programming Projects
Appendix 1 C+++ Keywords
Appendix 2 Precedence of operators
Appendix 3 The ASCII Character Set
Appendix 4 Some Libra Functions
Arithmetic Funions
Input and Output Member functions
Charaer Functions
C-String Functions
string Class Functions
Random Number Generator
Trigonometric functions
Appendix5 Old and New Header Files
Further Reading
lndex