Programming guru Schildt provides the ultimate C++ resource for beginning to intermediate programmers using his clear, informative, step-by-step style.
Author(s): Herbert Schildt
Edition: 3
Publisher: McGraw-Hill Companies
Year: 2003
Language: English
Pages: 624
C++ from the Ground Up
C++ from the Ground Up
About the Author
Contents
Preface
For Further Study
1 The Story of C++
The Origins of C++
The Creation of C
Understanding the Need for C++
C++ Is Born
The Evolution of C++
What Is Object- Oriented Programming?
Encapsulation
Polymorphism
Inheritance
C++ Implements OOP
How C++ Relates to Java and C#
2 An Overview of C++
Your First C++ Program
Entering the Program
Compiling the Program
Run the Program
A Line- by- Line Explanation
Handling Syntax Errors
A Second Simple Program
A More Practical Example
A New Data Type
A Quick Review
Functions
A Program with Two Functions
Function Arguments
Functions Returning Values
The main( ) Function
The General Form of C++ Functions
Some Output Options
Two Simple Commands
The if Statement
The for Loop
Blocks of Code
Semicolons and Positioning
Indentation Practices
C++ Keywords
Identifiers in C++
The Standard C++ Library
3 The Basic Data Types
Declaration of Variables
Local Variables
Formal Parameters
Global Variables
Some Type Modifiers
Literals
Hexadecimal and Octal Literals
String Literals
Character Escape Sequences
Variable Initializations
Operators
Arithmetic Operators
Increment and Decrement
How C++ Got Its Name
Relational and Logical Operators
Expressions
Type Conversion in Expressions
Converting to and from bool
Casts
Spacing and Parentheses
4 Program Control Statements
The if Statement
The Conditional Expression
Nested ifs
The if- else- if Ladder
The for Loop
Some Variations on the for Loop
Missing Pieces
The Infinite Loop
Time Delay Loops
The switch Statement
Nested switch Statements
The while Loop
The do- while Loop
Using continue
Using break to Exit Loops
Nested Loops
Using the goto Statement
Putting Together the Pieces
5 Arrays and Strings
One- Dimensional Arrays
No Bounds Checking
Sorting an Array
Strings
Reading a String from the Keyboard
Some String Library Functions
strcpy
strcat
strcmp
strlen
Using the Null Terminator
Two- Dimensional Arrays
Multidimensional Arrays
Array Initialization
Unsized Array Initializations
Arrays of Strings
An Example Using String Arrays
6 Pointers
What Are Pointers?
The Pointer Operators
The Base Type Is Important
Assigning Values Through a Pointer
Pointer Expressions
Pointer Arithmetic
Pointer Comparisons
Pointers and Arrays
Indexing a Pointer
Are Pointers and Arrays Interchangeable?
Pointers and String Literals
A Comparison Example
Arrays of Pointers
The Null Pointer Convention
Multiple Indirection
Pointers and 16- bit Environments
Problems with Pointers
Uninitialized Pointers
Invalid Pointer Comparisons
Forgetting to Reset a Pointer
7 Functions, Part One: The Fundamentals
Scope Rules of Functions
Local Variables
Formal Parameters
Global Variables
Passing Pointers and Arrays
Calling Functions with Pointers
Calling Functions with Arrays
Passing Strings
argc and argv: Arguments to main( )
Passing Numeric Command Line Arguments
Converting Numeric Strings to Numbers
The return Statement
Returning from a Function
Returning Values
void Functions
Functions That Return Pointers
Function Prototypes
Headers: A Closer Look
Old- Style versus Modern Function Parameter Declarations
Recursion
8 Functions, Part Two: References, Overloading,and Default Arguments
Two Approaches to Argument Passing
How C++ Passes Arguments
Using a Pointer to Create a Call- by- Reference
Reference Parameters
Declaring Reference Parameters 8
Returning References
Creating a Bounded Array
Independent References
A Few Restrictions When Using References
Function Overloading
The overload Anachronism
Default Function Arguments
Default Arguments versus Overloading
Using Default Arguments Correctly
Function Overloading and Ambiguity
9 More Data Types and Operators
The const and volatile Qualifiers
const
volatile
Storage Class Specifiers
auto
extern
static Variables
Register Variables
The Origins of the register Modifier
Enumerations
typedef
More Operators 9
Bitwise Operators
AND, OR, XOR, and NOT
The Shift Operators
The ? Operator
Compound Assignment
The Comma Operator
Multiple Assignments
Using sizeof
Dynamic Allocation Using new and delete
Initializing Dynamically Allocated Memory
Allocating Arrays
C¡¯s Approach to Dynamic Allocation:malloc( ) and free( )
Precedence Summary
10 Structures and Unions
Structures
Accessing Structure Members
Arrays of Structures
A Simple Inventory Example
Passing Structures to Functions
Assigning Structures
Pointers to Structures and the Arrow Operator
References to Structures
Arrays and Structures Within Structures
C Structure Versus C++ Structures
Bit- Fields
Unions
Anonymous Unions
Using sizeof to Ensure Portability
Moving On to Object- Oriented Programming
11 Introducing the Class
Class Fundamentals
The General Form of a class
A Closer Look at Class Member Access
Constructors and Destructors
Parameterized Constructors
An Initialization Alternative
Classes and Structures Are Related
Structures versus Classes
Unions and Classes Are Related
Inline Functions
Creating Inline Functions Inside a Class
Arrays of Objects
Initializing Object Arrays
Pointers to Objects
Object References
12 A Closer Look at Classes
Friend Functions
Overloading Constructors
Dynamic Initialization
Applying Dynamic Initialization to Constructors
Assigning Objects
Passing Objects to Functions
Constructors, Destructors, and Passing Objects
A Potential Problem When Passing Objects
Returning Objects
A Potential Problem When Returning Objects
Creating and Using a Copy Constructor
Copy Constructors and Parameters
Copy Constructors and Initializations
Using Copy Constructors When an Object Is Returned
Copy Constructors ¡ª Is There a Simpler Way?
The this Keyword
13 Operator Overloading
Operator Overloading Using Member Functions
Using Member Functions to Overload Unary Operators
Operator Overloading Tips and Restrictions
Nonmember Operator Functions
Order Matters
Using a Friend to Overload a Unary Operator
Overloading the Relational and Logical Operators
A Closer Look at the Assignment Operator
Overloading [ ]
Overloading ( )
Overloading Other Operators
Another Example of Operator Overloading
14 Inheritance
Introducing Inheritance
Base Class Access Control
Using protected Members
Using protected for Inheritance of a Base Class
Reviewing public, protected, and private
Inheriting Multiple Base Classes
Constructors, Destructors, and Inheritance
When Constructors and Destructors Are Executed
Passing Parameters to Base Class Constructors
Granting Access
Reading C++ Inheritance Graphs
Virtual Base Classes
15 Virtual Functions and Polymorphism
Pointers to Derived Types
References to Derived Types
Virtual Functions
Virtual Functions Are Inherited
Why Virtual Functions?
A Simple Application of Virtual Functions
Pure Virtual Functions and Abstract Classes
Early versus Late Binding
Polymorphism and the Purist
16 Templates
Generic Functions
A Function with Two Generic Types
Explicitly Overloading a Generic Function
Overloading a Function Template
Using Standard Parameters with Template Functions
Generic Function Restrictions
Creating a Generic abs( ) Function
Generic Classes
An Example with Two Generic Data Types
Creating a Generic Array Class
Using Non- Type Arguments with Generic Classes
Using Default Arguments with Template Classes
Explicit Class Specializations
17 Exception Handling
Exception Handling Fundamentals
exit( ) and abort( )
Catching Class Types
Using Multiple catch Statements
Options for Exception Handling
Catching All Exceptions
Restricting Exceptions Thrown by a Function
Rethrowing an Exception
Handling Exceptions Thrown by new
The nothrow Alternative
Overloading new and delete
Overloading the nothrow Version of new
18 The C++ I/O System
Old VS Modern C++ I/ O
C++ Streams
The C++ Predefined Streams
The C++ Stream Classes
Overloading the I/ O Operators
Creating Inserters
Using Friend Functions to Overload Inserters
Overloading Extractors
C I/ O Versus C++ I/ O
Formatted I/ O
Formatting with the ios Member Functions
Using I/ O Manipulators
Creating Your Own Manipulator Functions
File I/ O
Opening and Closing a File
Reading and Writing Text Files
Unformatted Binary I/ O
Reading and Writing Blocks of Data
Detecting EOF
A File Comparison Example
More Binary I/ O Functions
Random Access
Checking I/ O Status
Customized I/ O and Files
19 Run-Time Type ID and the Casting Operators
Run- Time Type Identification ( RTTI)
A Simple Application of Run- Time Type ID
typeid Can Be Applied to Template Classes
The Casting Operators
dynamic_ cast
const_ cast
static_ cast
reinterpret_ cast
The Traditional Cast Versus the Four Casting Operators
20 Namespaces and Other Advanced Topics
Namespaces
Namespace Fundamentals
using
Unnamed Namespaces
The std Namespace
Pointers to Functions
Finding the Address of an Overloaded Function
Static Class Members
const Member Functions and mutable
Explicit Constructors
An Interesting Benefit from
Implicit Constructor Conversion
The Member Initialization Syntax
Using the asm Keyword
Linkage Specification
The .* and ¨C>* Pointer- to- Member Operators
Creating Conversion Functions
21 Introducing the Standard Template Library
An Overview of the STL
The Container Classes
Vectors
Accessing a Vector Through an Iterator
Inserting and Deleting Elements in a Vector
Storing Class Objects in a Vector
The Power of Iterators
Lists
Sort a List
Merging One List with Another
Storing Class Objects in a List
Maps
Storing Class Objects in a Map
Algorithms
Counting
Removing and Replacing Elements
Reversing a Sequence
Transforming a Sequence
Exploring the Algorithms
The string Class 21
Some string Member Functions
Putting Strings into Other Containers
Final Thoughts on the STL
22 The C++ Preprocessor
# define
Function- Like Macros
# error
# include
Conditional Compilation Directives
# if, # else, # elif, and # endif
# ifdef and # ifndef
# undef
Using defined
The Diminishing Role of the Preprocessor
# line
# pragma
The # and ## Preprocessor Operators
Predefined Macro Names
Final Thoughts
A C-Based I/Oased I/ O
C I/ O Uses Streams
Understanding printf( ) and scanf( )
printf( )
scanf( )
The C File System
fopen( )
fputc( )
fgetc( )
feof( )
fclose( ) A
Using fopen( ), fgetc( ), fputc( ), and fclose( )
ferror( ) and rewind( )
fread( ) and fwrite( )
fseek( ) and Random- Access I/ O
fprintf( ) and fscanf( )
Erasing Files
B Working with an Older C++ Compiler
Two Simple Changes
C The .NET Managed Extensions to C++
The . NET Keyword Extensions
_ _ abstract
_ _ box
_ _ delegate
_ _ event
_ _ finally
_ _ gc
_ _ identifier
_ _ interface
_ _ nogc
_ _ pin
_ _ property
_ _ sealed
_ _ try_ cast
_ _ typeof
_ _ value
Preprocessor Extensions
The attribute Attribute
Compiling Managed C++
Index