Bruce Eckel President, MindView Inc.

还看Design Pattern,发现好多OO的概念都不清楚了,继续回归~~

Preface 1
What’s new in
the second edition …. 2
What’s in Volume 2
of this book……………………. 3
How to get Volume 2………… 3
Prerequisites………… 3
Learning C++ ………. 4
Goals …………………. 5
Chapters …………….. 7
Exercises…………… 12
Exercise solutions ……………12
Source code……….. 12
Language
standards ………….. 14
Language support ……………15
The book’s
CD ROM ……………. 15
CD ROMs, seminars,
and consulting……. 16
Errors ………………. 16
About the cover…… 17
Book design and
production …………. 18
Acknowledgements . 19

1: Introduction to Objects 21
The progress of abstraction…………. 22
An object has an interface ……………. 25
The hidden implementation …… 28

The goal of the class creator is to build a class that exposes only what’s necessary to the client programmer and keeps everything else hidden. Why? Because if it’s hidden, the client programmer can’t use it, which means that the class creator can change the hidden portion at will without worrying about the impact to anyone else.
Reusing the implementation …… 29
Inheritance: reusing the interface ………. 31
Is-a vs. is-like-a relationships…………………. 35
Interchangeable objects with polymorphism …….. 37
Creating and destroying objects… 41
Exception handling: dealing with errors .. 43
Analysis and design …………. 44
Phase 0: Make a plan………. 47
Phase 1: What are we making?………………….. 48
Phase 2: How will we build it? ………………….. 52
Phase 3: Build the core ……. 56
Phase 4: Iterate the use cases ……………….. 57 

Phase 5: Evolution …………..58
Plans pay off ………………….60

Extreme rogramming ……… 61
    Write tests first……………….61
    Pair programming ……………63
Why C++
succeeds …………… 64
A better C……………………..65
You’re already on
the learning curve……………65
Efficiency ………………………66
Systems are easier to
express and understand…….66
Maximal leverage
with libraries ………………….67
Source-code reuse
with templates ……………….67
Error handling………………..67
Programming in the large…..68
Strategies for transition…………… 68
Guidelines……………………..69
Management obstacles ……..71
Summary ………….. 73

2: Making & Using
Objects 75
The process of
language
translation …………. 76
Interpreters …………………..77
Compilers ……………………..77
The compilation process…….79
Tools for separate
compilation………… 80
Declarations vs. definitions…81
Linking………………………… 87
Using libraries ………………..88
Your first
C++ program …….. 90
Using the iostreams class…..90
Namespaces…………………..91
Fundamentals of
program structure……………93
"Hello, world!" ………………..94
Running the compiler ……….95
More about
iostreams ………….. 96
Character array
concatenation ……………….. 96
Reading input ……………….. 97
Calling other programs…….. 98
Introducing
strings………………. 98
Reading and
writing files ………. 100
Introducing
vector …………….. 102
Summary…………. 108
Exercises …………. 109

3: The C in C++ 111
Creating
functions …………. 112
Function return values …… 115
Using the C
function library ……………. 116
Creating your own
libraries with the librarian.. 117
Controlling
execution…………. 117
True and false …………….. 117
if-else……………………….. 118
while ………………………… 119
do-while ……………………. 120
for …………………………… 121
The break and
continue keywords ……….. 122
switch……………………….. 123
Using and misusing goto … 125
Recursion…………………… 126
Introduction to
operators…………. 127
Precedence…………………. 127
Auto increment
and decrement ……………. 128
Introduction to
data types ……….. 129
Basic built-in types ……….. 129
bool, true, & false ………… 131
Specifiers …………………… 132
Introduction to pointers …. 133
Modifying the
outside object ………………137
Introduction to
C++ references …………….140
Pointers and references
as modifiers …………………141
Scoping…………….143
Defining variables
on the fly…………………….145
Specifying storage
allocation ………….147
Global variables …………….147
Local variables ……………..149
static …………………………149
extern ………………………..151
Constants ……………………153
volatile ……………………….155
Operators and
their use …………..156
Assignment………………….156
Mathematical operators …..156
Relational operators ……….158
Logical operators …………..158
Bitwise operators …………..159
Shift operators ……………..160
Unary operators…………….163
The ternary operator ………164
The comma operator ………165
Common pitfalls
when using operators ……..166
Casting operators ………….166
C++ explicit casts………….167
sizeof – an operator
by itself ………………………172
The asm keyword ………….173
Explicit operators …………..173
Composite type
creation ……………174
Aliasing names
with typedef…………………174
Combining variables
with struct …………………..175
Clarifying programs
with enum …………………..179
Saving memory
with union …………………..181
Arrays ………………………..182
Debugging hints ….193
Debugging flags…………….194
Turning variables and
expressions into strings …..196
The C assert( ) macro……..197
Function
addresses ………… 198
Defining a
function pointer …………… 198
Complicated declarations
& definitions ……………….. 199
Using a function pointer …. 200
Arrays of pointers
to functions ………………… 201
Make: managing
separate
compilation ………. 202
Make activities…………….. 204
Makefiles in this book ……. 207
An example makefile …….. 208
Summary…………. 210
Exercises …………. 210

4: Data Abstraction 217
A tiny C-like
library …………….. 219 =========================
Dynamic
storage allocation…………. 223
Bad guesses ……………….. 227
What’s wrong?…… 229
The basic object…. 230
What’s an object? . 238
Abstract
data typing………. 239
Object details ……. 240
Header file
etiquette………….. 241
Importance of
header files ………………… 242
The multiple-declaration
problem…………………….. 244
The preprocessor directives
#define, #ifdef,
and #endif …………………. 245
A standard for header files 246
Namespaces in headers …. 247
Using headers in projects .. 248
Nested structures.. 248
Global scope resolution ….. 253
Summary…………. 253

Exercises…………..254

5: Hiding the
Implementation 259
Setting limits ……..260
C++ access
control ……………..261
protected…………………….263
Friends……………..263
Nested friends ………………266
Is it pure?……………………269
Object layout ……..269
The class…………..270
Modifying Stash to use
access control ………………273
Modifying Stack to use
access control ………………274
Handle classes ……275
Hiding the
implementation …………….276
Reducing recompilation……276
Summary ………….279
Exercises…………..280

6: Initialization &
Cleanup 283
Guaranteed
initialization with the
constructor ………..285
Guaranteed cleanup
with the
destructor …………287
Elimination of the
definition block……289
for loops ……………………..291
Storage allocation ………….292
Stash with
constructors and
destructors ………..294
Stack with constructors
& destructors ……..298
Aggregate
initialization ……… 301
Default
constructors ……… 304
Summary…………. 305
Exercises …………. 306

7: Function Overloading
& Default
Arguments 309
More name
decoration ……….. 311
Overloading on
return values ………………. 312
Type-safe linkage…………. 313
Overloading
example ………….. 314
unions…………….. 318
Default
arguments ……….. 321
Placeholder arguments…… 323
Choosing overloading
vs. default
arguments ……….. 324
Summary…………. 329
Exercises …………. 330

8: Constants 333
Value substitution . 334
const in header files ……… 335
Safety consts………………. 336
Aggregates…………………. 337
Differences with C ………… 338
Pointers …………… 340
Pointer to const …………… 340
const pointer ………………. 341
Assignment and
type checking……………… 343
Function arguments &
return values…….. 344
Passing by const value…… 344
Returning by const value …345
Passing and returning
addresses ……………………349
Classes …………….352
const in classes …………….353
Compile-time constants
in classes…………………….356
const objects &
member functions………….359
volatile……………..365
Summary ………….367
Exercises…………..367

9: Inline Functions 371
Preprocessor
pitfalls ……………..372
Macros and access …………376
Inline functions …..377
Inlines inside classes ………378
Access functions ……………379
Stash & Stack
with inlines ………..385
Inlines &
the compiler ………390
Limitations…………………..390
Forward references ………..391
Hidden activities in
constructors &
destructors ………………….392
Reducing clutter ….393
More preprocessor
features ……………395
Token pasting………………396
Improved error
checking………….. 396
Summary ………….400
Exercises…………..400

10: Name Control 405
Static elements
from C ……………..406
static variables
inside functions …………….406
Controlling linkage …………412
Other storage
class specifiers…………….. 414
Namespaces……… 414
Creating a namespace …… 415
Using a namespace ………. 417
The use of namespaces….. 422
Static members
in C++ ……………. 423
Defining storage for
static data members……… 424
Nested and local classes…. 428
static member functions…. 429
Static initialization
dependency ……… 432
What to do …………………. 434
Alternate linkage
specifications…….. 442
Summary…………. 443
Exercises …………. 443

11: References & the
Copy-Constructor 449
Pointers in C++ …. 450
References
in C++ ……………. 451
References in functions ….. 452
Argument-passing
guidelines ………………….. 455
The copyconstructor
………. 455
Passing & returning
by value…………………….. 455
Copy-construction ………… 462
Default copy-constructor … 468
Alternatives to copyconstruction
……………….. 471
Pointers
to members ……… 473
Functions …………………… 475
Summary…………. 478
Exercises …………. 479

12: Operator
Overloading 485
Warning &
reassurance ……….486
Syntax ……………..487
Overloadable
operators ………….488
Unary operators…………….489
Binary operators ……………493
Arguments &
return values………………..505
Unusual operators………….508
Operators you
can’t overload ………………517
Non-member
operators ………….518
Basic guidelines …………….520
Overloading
assignment………..521
Behavior of operator= …….522
Automatic type
conversion…………533
Constructor conversion ……534
Operator conversion……….535
Type conversion example…538
Pitfalls in automatic
type conversion …………….539
Summary ………….542
Exercises…………..542

13: Dynamic
Object Creation 547
Object creation……549
C’s approach to the heap …550
operator new………………..552
operator delete……………..553
A simple example ………….553
Memory manager
overhead …………………….554
Early examples
redesigned ………..555
delete void* is
probably a bug……………..555
Cleanup responsibility
with pointers ………………..557
Stash for pointers ………… 558
new & delete
for arrays…………. 563
Making a pointer
more like an array………… 564
Running out
of storage ………… 565
Overloading
new & delete …….. 566
Overloading global
new & delete ………………. 568
Overloading
new & delete for a class …. 570
Overloading
new & delete for arrays….. 573
Constructor calls ………….. 576
placement new & delete …. 577
Summary…………. 580
Exercises …………. 580

14: Inheritance &
Composition 583
Composition
syntax…………….. 584
Inheritance
syntax…………….. 586
The constructor
initializer list …….. 588
Member object
initialization………………… 589
Built-in types in the
initializer list……………….. 589
Combining composition
& inheritance…….. 591
Order of constructor &
destructor calls ……………. 592
Name hiding……… 595
Functions that
don’t automatically
inherit …………….. 600
Inheritance and static
member functions ………… 604
Choosing composition
vs. inheritance ……604
Subtyping…………………… 606
private inheritance …………609
protected ………….610
protected inheritance ……..611
Operator overloading
& inheritance ……..612
Multiple
inheritance ………..613
Incremental
development………614
Upcasting…………. 615
Why “upcasting?”…………..617
Upcasting and the
copy-constructor……………617
Composition vs.
inheritance (revisited) …….620
Pointer & reference
upcasting…………………….622
A crisis ……………………….622
Summary ………….623
Exercises…………..623

15: Polymorphism &
Virtual Functions 627
Evolution of C++
programmers……..628
Upcasting…………. 629
The problem ………631
Function call binding………631
virtual functions ….632
Extensibility …………………633
How C++ implements
late binding……….636
Storing type information ….637
Picturing virtual functions…639
Under the hood …………….642
Installing the vpointer …….643
Objects are different ………644
Why virtual
functions?………….645
Abstract base classes
and pure virtual
functions …………. 646
Pure virtual definitions …… 651
Inheritance and
the VTABLE ………. 652
Object slicing………………. 655
Overloading &
overriding………… 658
Variant return type……….. 660
virtual functions &
constructors ……… 662
Order of constructor calls .. 663
Behavior of virtual functions
inside constructors ……….. 664
Destructors and
virtual destructors. 665
Pure virtual destructors….. 668
Virtuals in destructors……. 670
Creating an
object-based hierarchy ….. 671
Operator
overloading………. 675
Downcasting…….. 678
Summary…………. 681
Exercises …………. 682

16: Introduction to
Templates 689
Containers ……….. 690
The need for containers …. 692
Overview
of templates……… 693
The template solution ……. 696
Template syntax … 697
Non-inline
function definitions ……….. 699
IntStack as a template…… 701
Constants in templates ….. 703
Stack and Stash
as templates …….. 705
Templatized pointer Stash . 707
Turning ownership

on and off………….713
Holding objects
by value ……………716
Introducing
iterators ……………719
Stack with iterators………..728
PStash with iterators ………732
Why iterators? ……738
Function templates…………742
Summary ………….743
Exercises…………..744
A: Coding Style 747
B: Programming
Guidelines 759
C: Recommended
Reading 775
C…………………….776
General C++………776
My own list of books……….777
Depth &
dark corners ………778
Analysis & design ..779
Index 783

Advertisements