Table of Contents
Introduction
Chapter 1. Getting Started
Learning C++: What Lies Before You
The Origins of C++: A Little History
The C Language
C Programming Philosophy
The C++ Shift: Object-Oriented Programming
C++ and Generic Programming
The Genesis of C++
Portability and Standards
The Mechanics of Creating a Program
Creating the Source Code File
Compilation and Linking
Summary
Chapter 2. Setting Out to C++
C++ Initiation
The main()
Function
C++ Comments
The C++ Preprocessor and the iostream
File
Header Filenames
Namespaces
C++ Output with cout
C++ Source Code Formatting
C++ Statements
Declaration Statements and Variables
Assignment Statements
A New Trick for cout
More C++ Statements
Using cin
Concatenating with cout
cin
and cout:
A Touch of Class
Functions
Using a Function That Has a Return Value
Function Variations
User-Defined Functions
Using a User-Defined Function That Has a Return Value
Placing the using
Directive in Multifunction Programs
Summary
Review Questions
Programming Exercises
Chapter 3. Dealing with Data
Simple Variables
Names for Variables
Integer Types
The short
, int
, and long
Integer Types
Unsigned Types
Choosing an Integer Type
Integer Constants
How C++ Decides What Type a Constant Is
The char
Type: Characters and Small Integers
The bool
Type
The const
Qualifier
Floating-Point Numbers
Writing Floating-Point Numbers
Floating-Point Types
Floating-Point Constants
Advantages and Disadvantages of Floating-Point Numbers
C++ Arithmetic Operators
Order of Operation: Operator Precedence and Associativity
Division Diversions
The Modulus Operator
Type Conversions
Summary
Review Questions
Programming Exercises
Chapter 4. Compound Types
Introducing Arrays
Program Notes
Initialization Rules for Arrays
Strings
Concatenating String Constants
Using Strings in an Array
Adventures in String Input
Reading String Input a Line at a Time
Mixing String and Numeric Input
Introducing the string
Class
Assignment, Concatenation, and Appending
More string
Class Operations
More on string
Class I/O
Introducing Structures
Using a Structure in a Program
Can a Structure Use a string
Class Member?
Other Structure Properties
Arrays of Structures
Bit Fields in Structures
Unions
Enumerations
Setting Enumerator Values
Value Ranges for Enumerations
Pointers and the Free Store
Declaring and Initializing Pointers
Pointer Danger
Pointers and Numbers
Allocating Memory with new
Freeing Memory with delete
Using new
to Create Dynamic Arrays
Pointers, Arrays, and Pointer Arithmetic
Program Notes
Pointers and Strings
Using new
to Create Dynamic Structures
Automatic Storage, Static Storage, and Dynamic Storage
Summary
Review Questions
Programming Exercises
Chapter 5. Loops and Relational Expressions
Introducing for
Loops
for
Loop Parts
Back to the for
Loop
Changing the Step Size
Inside Strings with the for
Loop
The Increment (++
) and Decrement (--
) Operators
Side Effects and Sequence Points
Prefixing Versus Postfixing
The Increment/Decrement Operators and Pointers
Combination Assignment Operators
Compound Statements, or Blocks
The Comma Operator (or More Syntax Tricks)
Relational Expressions
A Mistake You’ll Probably Make
Comparing C-Style Strings
Comparing string
Class Strings
The while
Loop
Program Notes
for
Versus while
Just a Moment—Building a Time-Delay Loop
The do while
Loop
Loops and Text Input
Using Unadorned cin
for Input
cin.get(char)
to the Rescue
Which cin.get()
?
The End-of-File Condition
Yet Another Version of cin.get()
Nested Loops and Two-Dimensional Arrays
Initializing a Two-Dimensional Array
Summary
Review Questions
Programming Exercises
Chapter 6. Branching Statements and Logical Operators
The if
Statement
The if else
Statement
Formatting if else
Statements
The if else if else
Construction
Logical Expressions
The Logical OR Operator: ||
The Logical AND Operator: &&
The Logical NOT Operator: !
Logical Operator Facts
Alternative Representations
The cctype
Library of Character Functions
The ?:
Operator
The switch
Statement
Using Enumerators as Labels
switch
and if else
The break
and continue
Statements
Program Notes
Number-Reading Loops
Program Notes
Simple File Input/Output
Text I/O and Text Files
Writing to a Text File
Reading from a Text File
Summary
Review Questions
Programming Exercises
Chapter 7. Functions: C++’s Programming Modules
Function Review
Defining a Function
Prototyping and Calling a Function
Function Arguments and Passing by Value
Multiple Arguments
Another Two-Argument Function
Functions and Arrays
How Pointers Enable Array-Processing Functions
The Implications of Using Arrays as Arguments
More Array Function Examples
Functions Using Array Ranges
Pointers and const
Functions and Two-Dimensional Arrays
Functions and C-Style Strings
Functions with C-Style String Arguments
Functions That Return C-Style Strings
Functions and Structures
Passing and Returning Structures
Another Example of Using Functions with Structures
Passing Structure Addresses
Functions and string
Class Objects
Recursion
Recursion with a Single Recursive Call
Recursion with Multiple Recursive Calls
Pointers to Functions
Function Pointer Basics
A Function Pointer Example
Summary
Review Questions
Programming Exercises
Chapter 8. Adventures in Functions
C++ Inline Functions
Reference Variables
Creating a Reference Variable
References as Function Parameters
Reference Properties and Oddities
Using References with a Structure
Using References with a Class Object
Another Object Lesson: Objects, Inheritance, and References
When to Use Reference Arguments
Default Arguments
Program Notes
Function Overloading
An Overloading Example
When to Use Function Overloading
Function Templates
Overloaded Templates
Explicit Specializations
Instantiations and Specializations
Which Function Version Does the Compiler Pick?
Summary
Review Questions
Programming Exercises
Chapter 9. Memory Models and Namespaces
Separate Compilation
Storage Duration, Scope, and Linkage
Scope and Linkage
Automatic Storage Duration
Static Duration Variables
Specifiers and Qualifiers
Functions and Linkage
Language Linking
Storage Schemes and Dynamic Allocation
The Placement new
Operator
Program Notes
Namespaces
Traditional C++ Namespaces
New Namespace Features
A Namespace Example
Namespaces and the Future
Summary
Review Questions
Programming Exercises
Chapter 10. Objects and Classes
Procedural and Object-Oriented Programming
Abstraction and Classes
What Is a Type?
Classes in C++
Implementing Class Member Functions
Using Classes
Reviewing Our Story to Date
Class Constructors and Destructors
Declaring and Defining Constructors
Using Constructors
Default Constructors
Destructors
Improving the Stock
Class
Constructors and Destructors in Review
Knowing Your Objects: The this
Pointer
An Array of Objects
The Interface and Implementation Revisited
Class Scope
Class Scope Constants
Abstract Data Types
Summary
Review Questions
Programming Exercises
Chapter 11. Working with Classes
Operator Overloading
Time on Our Hands: Developing an Operator Overloading Example
Adding an Addition Operator
Overloading Restrictions
More Overloaded Operators
Introducing Friends
Creating Friends
A Common Kind of Friend: Overloading the <<
Operator
Overloaded Operators: Member Versus Nonmember Functions
More Overloading: A Vector Class
Using a State Member
Overloading Arithmetic Operators for the Vector
Class
An Implementation Comment
Taking the Vector
Class on a Random Walk
Automatic Conversions and Type Casts for Classes
Program Notes
Conversion Functions
Conversions and Friends
Summary
Review Questions
Programming Exercises
Chapter 12. Classes and Dynamic Memory Allocation
Dynamic Memory and Classes
A Review Example and Static Class Members
Implicit Member Functions
The New, Improved String
Class
Things to Remember When Using new
in Constructors
Observations About Returning Objects
Using Pointers to Objects
Reviewing Techniques
A Queue Simulation
A Queue Class
The Customer
Class
The Simulation
Summary
Review Questions
Programming Exercises
Chapter 13. Class Inheritance
Beginning with a Simple Base Class
Deriving a Class
Constructors: Access Considerations
Using a Derived Class
Special Relationships Between Derived and Base Classes
Inheritance: An Is-a Relationship
Polymorphic Public Inheritance
Developing the Brass
and BrassPlus
Classes
Static and Dynamic Binding
Pointer and Reference Type Compatibility
Virtual Member Functions and Dynamic Binding
Things to Know About Virtual Methods
Access Control: protected
Abstract Base Classes
Applying the ABC Concept
ABC Philosophy
Inheritance and Dynamic Memory Allocation
Case 1: Derived Class Doesn’t Use new
Case 2: Derived Class Does Use new
An Inheritance Example with Dynamic Memory Allocation and Friends
Class Design Review
Member Functions That the Compiler Generates for You
Other Class Method Considerations
Public Inheritance Considerations
Class Function Summary
Summary
Review Questions
Programming Exercises
Chapter 14. Reusing Code in C++
Classes with Object Members
The valarray
Class: A Quick Look
The Student
Class Design
The Student
Class Example
Private Inheritance
The Student
Class Example (New Version)
Multiple Inheritance
How Many Workers?
Which Method?
MI Synopsis
Class Templates
Defining a Class Template
Using a Template Class
A Closer Look at the Template Class
An Array Template Example and Non-Type Arguments
Template Versatility
Template Specializations
Member Templates
Templates as Parameters
Template Classes and Friends
Summary
Review Questions
Programming Exercises
Chapter 15. Friends, Exceptions, and More
Friends
Friend Classes
Friend Member Functions
Other Friendly Relationships
Nested Classes
Nested Classes and Access
Nesting in a Template
Exceptions
Calling abort()
Returning an Error Code
The Exception Mechanism
Using Objects as Exceptions
Unwinding the Stack
More Exception Features
The exception
Class
Exceptions, Classes, and Inheritance
When Exceptions Go Astray
Exception Cautions
RTTI
What Is RTTI For?
How Does RTTI Work?
Type Cast Operators
Summary
Review Questions
Programming Exercises
Chapter 16. The string
Class and the Standard Template Library
The string
Class
Constructing a String
string
Class Input
Working with Strings
What Else Does the string
Class Offer?
The auto_ptr
Class
Using auto_ptr
auto_ptr
Considerations
The STL
The vector
Template Class
Things to Do to Vectors
More Things to Do to Vectors
Generic Programming
Why Iterators?
Kinds of Iterators
Iterator Hierarchy
Concepts, Refinements, and Models
Kinds of Containers
Associative Containers
Function Objects (aka Functors)
Functor Concepts
Predefined Functors
Adaptable Functors and Function Adapters
Algorithms
Algorithm Groups
General Properties of Algorithms
The STL and the string
Class
Functions Versus Container Methods
Using the STL
Other Libraries
vector
and valarray
Summary
Review Questions
Programming Exercises
Chapter 17. Input, Output, and Files
An Overview of C++ Input and Output
Streams and Buffers
Streams, Buffers, and the iostream
File
Redirection
Output with cout
The Overloaded <<
Operator
The Other ostream
Methods
Flushing the Output Buffer
Formatting with cout
Input with cin
How cin >>
Views Input
Stream States
Other istream
Class Methods
Other istream
Methods
File Input and Output
Simple File I/O
Stream Checking and is_open()
Opening Multiple Files
Command-Line Processing
File Modes
Random Access
Incore Formatting
What Now?
Summary
Review Questions
Programming Exercises
Appendix A. Number Bases
Decimal Numbers (Base 10)
Octal Integers (Base 8)
Hexadecimal Numbers (Base 16)
Binary Numbers (Base 2)
Binary and Hex
Appendix B. C++ Reserved Words
C++ Keywords
Alternative Tokens
C++ Library Reserved Names
Appendix C. The ASCII Character Set
Appendix D. Operator Precedence
Appendix E. Other Operators
Bitwise Operators
The Shift Operators
The Logical Bitwise Operators
Alternative Representations of Bitwise Operators
A Few Common Bitwise Operator Techniques
Member Dereferencing Operators
Appendix F. The string
Template Class
Thirteen Types and a Constant
Data Information, Constructors, and Odds and Ends
Default Constructors
Constructors That Use Arrays
Constructors That Use Part of an Array
Copy Constructors
Constructors That Use n
Copies of a Character
Constructors That Use a Range
Memory Miscellany
String Access
Basic Assignment
String Searching
The find()
Family
The rfind()
Family
The find_first_of()
Family
The find_last_of()
Family
The find_first_not_of()
Family
The find_last_not_of()
Family
Comparison Methods and Functions
String Modifiers
Methods for Appending and Adding
More Assignment Methods
Insertion Methods
Erase Methods
Replacement Methods
Other Modifying Methods: copy()
and swap()
Output and Input
Appendix G. The STL Methods and Functions
Members Common to All Containers
Additional Members for Vectors, Lists, and Deques
Additional Members for Sets and Maps
STL Functions
Nonmodifying Sequence Operations
Mutating Sequence Operations
Sorting and Related Operations
Numeric Operations
Appendix H. Selected Readings and Internet Resources
Selected Readings
Internet Resources
Appendix I. Converting to ANSI/ISO Standard C++
Use Alternatives for Some Preprocessor Directives
Use const
Instead of #define
to Define Constants
Use inline
Instead of #define
to Define Short Functions
Use Function Prototypes
Use Type Casts
Become Familiar with C++ Features
Use the New Header Organization
Use Namespaces
Use the autoptr
Template
Use the string
Class
Use the STL
Appendix J. Answers to the Review Questions
Answers to Review Questions for Chapter 2
Answers to Review Questions for Chapter 3
Answers to Review Questions for Chapter 4
Answers to Review Questions for Chapter 5
Answers to Review Questions for Chapter 6
Answers to Review Questions for Chapter 7
Answers to Review Questions for Chapter 8
Answers to Review Questions for Chapter 9
Answers to Review Questions for Chapter 10
Answers to Review Questions for Chapter 11
Answers to Review Questions for Chapter 12
Answers to Review Questions for Chapter 13
Answers to Review Questions for Chapter 14
Answers to Review Questions for Chapter 15
Answers to Review Questions for Chapter 16
Answers to Review Questions for Chapter 17
Index