Thinking
In
C++
Bruce Eckel
President, MindView Inc.
Prentice Hall PTR
Upper Saddle River, New Jersey 07458
Publisher:
Alan Apt
Production Editor:
Mona Pompilli
Development Editor:
Sondra Chavez
Book Design, Cover Design and Cover Photo:
Daniel Will-Harris,
Copy Editor:
Shirley Michaels
Production Coordinator:
Lori Bulwin
Editorial Assistant:
Shirley McGuire
© 1999 by Bruce Eckel, MindView, Inc.
Published by Prentice Hall Inc.
A Paramount Communications Company
Englewood Cliffs, New Jersey 07632
The information in this book is distributed on an “as is” basis, without warranty. While every precaution
has been taken in the preparation of this book, neither the author nor the publisher shall have any
liability to any person or entitle with respect to any liability, loss or damage caused or alleged to be
caused directly or indirectly by instructions contained in this book or by the computer software or
hardware products described herein.
All rights reserved. No part of this book may be reproduced in any form or by any electronic or
mechanical means including information storage and retrieval systems without permission in writing
from the publisher or author, except by a reviewer who may quote brief passages in a review. Any of the
names used in the examples and text of this book are fictional; any relationship to persons living or dead
or to fictional characters in other works is purely coincidental.
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
ISBN 0-13-917709-4
Prentice-Hall International (UK) Limited,
London
Prentice-Hall of Australia Pty. Limited,
Sydney
Prentice-Hall Canada, Inc.,
Toronto
Prentice-Hall Hisapnoamericana, S.A.,
Mexico
Prentice-Hall of India Private Limited,
New Delhi
Prentice-Hall of Japan, Inc.,
Tokyo
Simon & Schuster Asia Pte. Ltd.,
Singapore
Editora Prentice-Hall do Brasil, Ltda.,
Rio de Janeiro
dedication
to the scholar, the healer, and the muse
What’s inside...
Thinking in C++ 2
nd
edition VERSION
TICA17 .............................................. 1
Preface 17
Prerequisites...............................17
Thinking in C....................................17
Learning C++.............................18
Goals ..........................................19
Chapters......................................20
Exercises ....................................23
Source code................................24
Coding standards .............................. 25
Language standards....................26
Language support.............................26
Seminars & CD Roms................26
Errors..........................................27
Acknowledgements ....................27
1: Introduction to objects 29
The progress of abstraction ........29
An object has an interface ..........31
The hidden implementation........33
Reusing the implementation......34
Inheritance: reusing the interface35
Is-a vs. is-like-a relationships ........... 39
Interchangeable objects with
polymorphism ............................40
Creating and destroying objects .43
Exception handling: dealing with
errors ..........................................44
Analysis and design....................45
Phase 0: Make a plan........................ 47
Phase 1: What are we making?.........48
Phase 2: How will we build it? ......... 50
Phase 3: Build it ...............................53
Phase 4: Iteration.............................. 53
Plans pay off..................................... 55
Why C++ succeeds.....................55
A better C.........................................56
You’re already on the learning curve 56
Efficiency .........................................56
Systems are easier to express and
understand ........................................57
Maximal leverage with libraries ....... 57
Source-code reuse with templates..... 57
Error handling...................................57
Programming in the large ................. 58
Strategies for transition..............58
Guidelines......................................... 58
Management obstacles...................... 60
Summary....................................61
2: Making & using objects 63
The process of language translation63
Interpreters........................................ 64
Compilers.........................................64
The compilation process...................65
Tools for separate compilation... 66
Declarations vs. definitions...............67
Linking.............................................71
Using libraries ..................................72
Your first C++ program.............. 73
Using the iostreams class.................. 73
Namespaces...................................... 74
Fundamentals of program structure ..75
"Hello, world!".................................76
Running the compiler .......................77
More about iostreams................. 77
Character array concatenation........... 78
Reading input ...................................78
Simple file manipulation...................79
Introducing
string
s ....................80
Reading and writing files ........... 82
Introducing
vector
.....................84
Summary....................................87
Exercises.................................... 88
3: The C in C++ 91
Creating functions......................91
Using the C function library ............. 94
Creating your own libraries with the
librarian............................................94
Controlling execution.................95
True and false................................... 95
if-else
................................................ 95
while
................................................. 96
do-while
........................................... 97
for
..................................................... 98
The
break
and
continue
Keywords.. 99
switch
............................................. 100
Recursion........................................ 102
Introduction to operators..........102
Precedence...................................... 103
Auto increment and decrement....... 103
Introduction to data types.........104
Basic built-in types......................... 104
bool
,
true
, &
false
.......................... 105
Specifiers........................................ 106
Introduction to Pointers .................. 108
Modifying the outside object .......... 112
Introduction to C++ references....... 114
Pointers and references as modifiers115
Scoping.....................................117
Defining variables on the fly ..........118
Specifying storage allocation ...120
Global variables.............................. 120
Local variables ............................... 121
static
............................................... 121
extern
............................................. 123
Constants........................................ 124
volatile
........................................... 125
Operators and their use.............126
Assignment..................................... 126
Mathematical operators .................. 126
Relational operators........................ 128
Logical operators............................ 128
Bitwise operators............................129
Shift operators ................................ 129
Unary operators.............................. 132
The ternary operator ....................... 133
The comma operator....................... 133
Common pitfalls when using operators134
Casting operators............................ 134
sizeof
– an operator by itself........... 135
The
asm
keyword........................... 135
Explicit operators............................135
Composite type creation...........136
Aliasing names with
typedef
.......... 136
Combining variables with
struct
.... 137
Clarifying programs with
enum
..... 140
Saving memory with
union
............ 141
Arrays............................................. 142
Debugging hints .......................150
Debugging flags ............................. 151
Turning variables and expressions into
strings.............................................152
The C
assert( )
macro.....................153
Make: an essential tool for separate
compilation ..............................154
Make activities................................155
Makefile
s in this book.................... 157
An example
makefile
.....................158
Summary..................................160
Exercises.................................. 160
4: Data abstraction 163
A tiny C-like library.................164
Dynamic storage allocation ............167
Bad guesses ....................................170
What's wrong?..........................171
The basic object ....................... 172
What's an object? ..................... 178
Abstract data typing................. 179
Object details ...........................179
Header file etiquette.................181
Importance of header files..............181
The multiple-declaration problem...183
The preprocessor directives
#define
,
#ifdef
and
#endif
............................183
A standard for header files..............184
Namespaces in headers...................185
Using headers in projects................ 185
Nested structures......................186
Global scope resolution ..................189
Summary..................................190
Exercises.................................. 190
5: Hiding the implementation
195
Setting limits............................195
C++ access control...................196
protected
........................................198
Friends .....................................198
Nested friends................................. 200
Is it pure?........................................ 203
Object layout............................ 203
The class .................................. 204
Modifying
Stash
to use access control206
Modifying
Stack
to use access control207
Handle classes.......................... 208
Hiding the implementation ............. 208
Reducing recompilation.................. 208
Summary..................................211
Exercises.................................. 211
6: Initialization & cleanup 213
Guaranteed initialization with the
constructor................................214
Guaranteed cleanup with the
destructor..................................215
Elimination of the definition block217
for
loops......................................... 219
Storage allocation........................... 220
Stash
with constructors and
destructors................................221
Stack
with constructors &
destructors................................224
Aggregate initialization............227
Default constructors .................229
Summary..................................230
Exercises ..................................231
7: Function overloading &
default arguments 233
More name decoration..............234
Overloading on return values..........235
Type-safe linkage ........................... 235
Overloading example ...............236
unions.......................................239
Default arguments ....................243
Placeholder arguments.................... 244
Choosing overloading vs. default
arguments.................................244
Summary..................................249
Exercises ..................................249
8: Constants 251
Value substitution.....................251
const
in header files........................ 252
Safety
const
s .................................. 253
Aggregates...................................... 254
Differences with C.......................... 254
Pointers.....................................255
Pointer to
const
.............................. 256
const
pointer................................... 256
Assignment and type checking ....... 257
Function arguments & return
values........................................258
Passing by
const
value ................... 258
Returning by
const
value................ 259
Passing and returning addresses...... 262
Classes......................................265
const
and
enum
in classes.............. 265
Compile-time constants in classes..268
const
objects & member functions.271
ROMability.....................................275
volatile
.....................................275
Summary..................................277
Exercises.................................. 277
9: Inline functions 281
Preprocessor pitfalls.................281
Macros and access..........................284
Inline functions ........................285
Inlines inside classes.......................285
Access functions............................. 286
Stash & Stack with inlines.......292
Inlines & the compiler..............292
Limitations...................................... 292
Order of evaluation......................... 293
Hidden activities in constructors &
destructors ...................................... 294
Forward referencing.................295
Reducing clutter....................... 295
More preprocessor features......296
Token pasting ................................. 297
Improved error checking..........297
Summary..................................300
Exercises.................................. 300
10: Name control 303
Static elements from C............. 303
static
variables inside functions......303
Controlling linkage......................... 307
Other storage class specifiers.......... 309
Namespaces .............................309
Creating a namespace .....................309
Using a namespace ......................... 311
Static members in C++.............315
Defining storage for static data
members .........................................315
Nested and local classes..................317
static
member functions.................318
Static initialization dependency321
What to do...................................... 322
Alternate linkage specifications324
Summary..................................325
Exercises.................................. 325
11: References & the copy-
constructor 327
Pointers in C++........................ 327
References in C++....................328
References in functions...................328
Argument-passing guidelines..........331
The copy-constructor ............... 331
Passing & returning by value.......... 331
Copy-construction .......................... 336
Default copy-constructor ................341
Alternatives to copy-construction...344
Pointers to members.................345
Functions........................................ 346
Summary..................................349
Exercises ..................................349
12: Operator overloading 351
Warning & reassurance.............351
Syntax.......................................352
Overloadable operators.............353
Unary operators.............................. 353
Binary operators ............................. 358
Arguments & return values............. 369
Unusual operators........................... 371
Operators you can’t overload.......... 375
Nonmember operators..............375
Basic guidelines.............................. 377
Overloading assignment...........378
Behavior of
operator=
................... 379
Automatic type conversion.......389
Constructor conversion................... 389
Operator conversion ....................... 391
A perfect example: strings.............. 393
Pitfalls in automatic type conversion395
Summary..................................397
Exercises ..................................397
13: Dynamic object creation
399
Object creation .........................400
C’s approach to the heap................. 401
operator new
................................. 402
operator delete
.............................. 403
A simple example...........................403
Memory manager overhead ............ 404
Early examples redesigned.......405
Stash
for pointers...........................405
The stack ........................................ 409
new
&
delete
for arrays ...........411
Making a pointer more like an array412
Running out of storage.............412
Overloading new & delete........413
Overloading global new & delete...414
Overloading new & delete for a class416
Overloading new & delete for arrays418
Constructor calls............................. 420
Object placement............................ 421
Summary..................................423
Exercises ..................................423
14: Inheritance & composition
425
Composition syntax ................. 425
Inheritance syntax.................... 427
The constructor initializer list .. 429
Member object initialization........... 429
Built-in types in the initializer list...429
Combining composition &
inheritance................................ 430
Order of constructor & destructor calls432
Name hiding................................... 434
Functions that don’t automatically
inherit .............................................435
Choosing composition vs.
inheritance................................ 436
Subtyping ....................................... 438
Specialization .................................440
private
inheritance.........................442
protected
.................................443
protected
inheritance ..................... 444
Multiple inheritance................. 445
Incremental development......... 445
Upcasting ................................. 446
Why “upcasting”?........................... 447
Upcasting and the copy-constructor
(not indexed)...................................447
Composition vs. inheritance (revisited)450
Pointer & reference upcasting.........451
A crisis ...........................................452
Summary..................................452
Exercises.................................. 452
15: Polymorphism & virtual
functions 455
Evolution of C++ programmers......455
Upcasting ................................. 456
The problem.............................457
Function call binding......................458
virtual
functions...................... 458
Extensibility.................................... 459
How C++ implements late binding462
Storing type information.................463
Picturing virtual functions ..............464
Under the hood ............................... 466
Installing the vpointer.....................467
Objects are different ....................... 467
Why
virtual
functions?............ 468
Abstract base classes and pure
virtual
functions...................... 469
Pure
virtual
definitions ..................474
Inheritance and the
VTABLE
..475
virtual
functions & constructors479
Order of constructor calls ............... 480
Behavior of virtual functions inside
constructors .................................... 481
Destructors and
virtual
destructors482
Virtuals in destructors..................... 483
Summary..................................484
Exercises ..................................484
16: Introduction to templates
487
Containers & iterators ..............487
The need for containers .................. 489
Overview of templates..............490
The C approach ..............................490
The Smalltalk approach.................. 490
The template approach.................... 492
Template syntax........................493
Non-inline function definitions....... 495
The stack as a template................... 496
Constants in templates.................... 498
Stash
and
stack
as templates ..500
The ownership problem.................. 500
Stash
as a template......................... 500
stack
as a template ......................... 506
Polymorphism & containers.....509
Summary..................................513
Exercises ..................................513
Part 2: The Standard C++
Library 515
Library overview ......................516
17: Strings 519
What’s in a string......................519
Creating and initializing C++ strings521
Operating on strings.................523
Appending, inserting and concatenating
strings............................................. 524
Replacing string characters............. 525
Concatenation using non-member
overloaded operators.......................529
Searching in strings..................530
Finding in reverse...........................535
Finding first/last of a set................. 536
Removing characters from strings ..537
Comparing strings .......................... 541
Using iterators ................................ 545
Strings and character traits..............547
A string application..................550
Summary..................................553
Exercises.................................. 554
18: Iostreams 555
Why iostreams?........................ 555
True wrapping ................................557
Iostreams to the rescue............. 559
Sneak preview of operator overloading560
Inserters and extractors...................561
Common usage............................... 562
Line-oriented input .........................564
File iostreams........................... 566
Open modes.................................... 568
Iostream buffering....................568
Using
get( )
with a streambuf .........570
Seeking in iostreams ................ 570
Creating read/write files .................572
stringstreams ............................573
strstreams .................................573
User-allocated storage .................... 573
Automatic storage allocation ..........576
Output stream formatting.........579
Internal formatting data .................. 580
An exhaustive example................... 584
Formatting manipulators.......... 587
Manipulators with arguments .........589
Creating manipulators..............592
Effectors ......................................... 593
Iostream examples....................595
Code generation..............................595
A simple datalogger........................ 603
Counting editor...............................610
Breaking up big files.......................611
Summary..................................613
Exercises.................................. 613
19: Templates in depth 615
Nontype template arguments.... 615
Default template arguments .....616
The typename keyword............616
Typedefing a typename...................618
Using
typename
instead of
class
....618
Function templates................... 618
A memory allocation system .......... 619
Type induction in function
templates..................................623
Taking the address of a generated
function template ..................... 624
Applying a function to an STL
sequence................................... 625
Template-templates..................628
Member function templates .....629
Why virtual member template
functions are disallowed ................. 631
Nested template classes .................. 631
Template specializations...........631
Full specialization........................... 631
Partial Specialization...................... 631
A practical example........................ 631
Design & efficiency........................ 635
Preventing template bloat ............... 635
Explicit instantiation ................636
Explicit specification of template
functions.........................................637
Controlling template instantiation637
The inclusion vs. separation models639
The export keyword........................ 639
Template programming idioms.639
The “curiously-recurring template” 639
Traits .............................................. 639
Summary..................................639
20: STL Containers & Iterators
641
Containers and iterators............641
STL reference documentation......... 643
The Standard Template Library 643
The basic concepts ...................645
Containers of strings.................649
Inheriting from STL containers 651
A plethora of iterators ..............653
Iterators in reversible containers..... 655
Iterator categories...........................656
Predefined iterators......................... 657
Basic sequences: vector, list &
deque........................................663
Basic sequence operations.............. 663
vector........................................666
Cost of overflowing allocated storage667
Inserting and erasing elements........ 671
deque........................................673
Converting between sequences....... 675
Cost of overflowing allocated storage676
Checked random-access.................. 678
list.............................................679
Special list operations..................... 681
Swapping all basic sequences.........685
Robustness of lists..........................686
Performance comparison..........687
set .............................................692
Eliminating
strtok( )
...................... 693
StreamTokenizer
: a more flexible
solution........................................... 695
A completely reusable tokenizer..... 697
stack .........................................702
queue........................................705
Priority queues.........................710
Holding bits..............................720
bitset<n>
........................................720
vector<bool>
.................................724
Associative containers.............. 726
Generators and fillers for associative
containers .......................................730
The magic of maps .........................733
Multimaps and duplicate keys ........738
Multisets.........................................741
Combining STL containers......744
Cleaning up containers of pointers747
Creating your own containers .. 749
Freely-available STL extensions751
Summary..................................753
Exercises.................................. 754
21: STL Algorithms 757
Function objects....................... 757
Classification of function objects.... 759
Automatic creation of function objects759
SGI extensions................................ 773
A catalog of STL algorithms.... 779
Support tools for example creation. 781
Filling & generating........................785
Counting......................................... 787
Manipulating sequences.................. 788
Searching & replacing ....................793
Comparing ranges........................... 799
Removing elements ........................ 802
Sorting and operations on sorted ranges805
Heap operations.............................. 816
Applying an operation to each element
in a range........................................817
Numeric algorithms........................825
General utilities ..............................828
Creating your own STL-style
algorithms ................................830
Summary..................................831
Exercises.................................. 831
Part 3: Advanced Topics 835
22: Multiple inheritance 836
Perspective ............................... 836
Duplicate subobjects................ 838
Ambiguous upcasting............... 839
virtual
base classes.................. 840
The "most derived" class and virtual
base initialization............................ 842
"Tying off" virtual bases with a default
constructor...................................... 843
Overhead ..................................845
Upcasting..................................846
Persistence...................................... 849
Avoiding MI.............................856
Repairing an interface ..............856
Summary..................................861
Exercises ..................................861
23: Exception handling 863
Error handling in C...................863
Throwing an exception.............866
Catching an exception..............866
The
try
block.................................. 867
Exception handlers ......................... 867
The exception specification ............ 868
Better exception specifications? ..... 871
Catching any exception ..................871
Rethrowing an exception ................ 872
Uncaught exceptions....................... 872
Function-level try blocks................874
Cleaning up ..............................874
Constructors .............................878
Making everything an object .......... 879
Exception matching..................882
Standard exceptions..................884
Programming with exceptions..885
When to avoid exceptions............... 885
Typical uses of exceptions.............. 886
Overhead ..................................890
Summary..................................890
Exercises ..................................891
24: Run-time type
identification 893
The “Shape” example...............893
What is RTTI?..........................894
Two syntaxes for RTTI................... 894
Syntax specifics........................898
typeid( )
with built-in types............ 898
Producing the proper type name ..... 899
Nonpolymorphic types ................... 899
Casting to intermediate levels......... 900
void
pointers................................... 902
Using RTTI with templates ............902
References................................903
Exceptions...................................... 904
Multiple inheritance .................905
Sensible uses for RTTI .............906
Revisiting the trash recycler ........... 907
Mechanism & overhead of RTTI910
Creating your own RTTI ..........910
Explicit cast syntax .................. 914
static_cast
......................................915
const_cast
......................................917
reinterpret_cast
.............................918
Summary..................................920
Exercises.................................. 921
XX: Maintaining system
integrity 923
The canonical object form........923
An extended canonical form........... 923
Dynamic aggregation...............923
Reference counting ..................927
Reference-counted class
hierarchies................................ 927
Exercises.................................. 927
25: Design patterns 929
The pattern concept..................929
The singleton.................................. 930
Classifying patterns.................. 934
Features, idioms, patterns ...............935
Basic complexity hiding ................. 935
Factories: encapsulating object
creation.....................................935
Polymorphic factories.....................938
Abstract factories............................ 941
Virtual constructors ........................ 943
Callbacks..................................949
Functor/Command..........................949
Strategy........................................... 949
Observer.........................................949
Multiple dispatching ................959
Visitor, a type of multiple dispatching962
Efficiency................................. 966
Flyweight........................................ 966
The composite..........................966
Evolving a design: the trash
recycler.....................................966
Improving the design ............... 971
“Make more objects”......................971
A pattern for prototyping creation .. 976
Abstracting usage.....................987
Applying double dispatching... 992
Implementing the double dispatch..992
Applying the visitor pattern ..... 997
RTTI considered harmful? .....1003
Summary................................1006
Exercises................................ 1007
26: Tools & topics 1008
The code extractor..................1008
Debugging..............................1030
assert( )
........................................ 1030
Trace macros ................................ 1030
Trace file ...................................... 1031
Abstract base class for debugging. 1032
Tracking
new
/
delete
&
malloc
/
free
1032
CGI programming in C++......1038
Encoding data for CGI.................. 1039
The CGI parser ............................. 1040
Using POST..................................1047
Handling mailing lists................... 1048
A general information-extraction CGI
program........................................ 1059
Parsing the data files..................... 1065
Summary................................1072
Exercises ................................1072
A: Coding style 1073
File names ..............................1073
Begin and end comment tags..1073
Parens, braces and indentation1074
Order of header inclusion.......1077
Include guards on header files1077
Use of namespaces................. 1078
Use of
require( )
and
assure( )
1078
B: Programming guidelines
1079
C: Recommended reading 1089
C............................................. 1089
General C++........................... 1089
My own list of books.................... 1090
Depth & dark corners............. 1090
Analysis & Design................. 1090
The STL................................. 1092
Design Patterns ......................1092
D:Compiler specifics 1093
Index 1101
17
Preface
Like any human language, C++ provides a way to express
concepts. If successful, this medium of expression will be
significantly easier and more flexible than the alternatives as
problems grow larger and more complex.
You can’t just look at C++ as a collection of features; some of the features make no sense in
isolation. You can only use the sum of the parts if you are thinking about
design
, not simply
coding. And to understand C++ in this way, you must understand the problems with C and
with programming in general. This book discusses programming problems, why they are
problems, and the approach C++ has taken to solve such problems. Thus, the set of features I
explain in each chapter will be based on the way I see a particular type of problem being
solved with the language. In this way I hope to move you, a little at a time, from
understanding C to the point where the C++ mindset becomes your native tongue.
Throughout, I’ll be taking the attitude that you want to build a model in your head that allows
you to understand the language all the way down to the bare metal; if you encounter a puzzle
you’ll be able to feed it to your model and deduce the answer. I will try to convey to you the
insights which have rearranged my brain to make me start “thinking in C++.”
Prerequisites
In the first edition of this book, I decided to assume that someone else had taught you C and
that you have at least a reading level of comfort with it. My primary focus was on simplifying
what I found difficult – the C++ language. In this edition I have added a chapter that is a very
rapid introduction to C, assuming that you have some kind of programming experience
already. In addition, just as you learn many new words intuitively by seeing them in context
in a novel, it’s possible to learn a great deal about C from the context in which it is used in the
rest of the book.
Thinking in C
For those of you who need a gentler introduction to C than the chapter in this book, I have
created with Chuck Allison a CD ROM called “Thinking in C: foundations for Java and C++”
which will introduce you to the aspects of C that are necessary for you to move on to C++ or
Java (leaving out the nasty bits that C programmers must deal with on a day-to-day basis but
that the C++ and Java languages steer you away from). This CD can be ordered at
Preface 18
. [Note: the CD will not be available until late Fall 98, at the
earliest – watch the Web site for updates]
Learning C++
I clawed my way into C++ from exactly the same position as I expect the readers of this book
will: As a C programmer with a very no-nonsense, nuts-and-bolts attitude about
programming. Worse, my background and experience was in hardware-level embedded
programming, where C has often been considered a high-level language and an inefficient
overkill for pushing bits around. I discovered later that I wasn’t even a very good C
programmer, hiding my ignorance of structures,
malloc( )
&
free( )
,
setjmp( )
&
longjmp( )
,
and other “sophisticated” concepts, scuttling away in shame when the subjects came up in
conversation rather than reaching out for new knowledge.
When I began my struggle to understand C++, the only decent book was Stroustrup’s self-
professed “expert’s guide,
1
” so I was left to simplify the basic concepts on my own. This
resulted in my first C++ book,
2
which was essentially a brain dump of my experience. That
was designed as a reader’s guide, to bring programmers into C and C++ at the same time.
Both editions
3
of the book garnered an enthusiastic response and I still feel it is a valuable
resource.
At about the same time that
Using C++
came out, I began teaching the language. Teaching
C++ has become my profession; I’ve seen nodding heads, blank faces, and puzzled
expressions in audiences all over the world since 1989. As I began giving in-house training
with smaller groups of people, I discovered something during the exercises. Even those
people who were smiling and nodding were confused about many issues. I found out, by
chairing the C++ track at the Software Development Conference for the last three years, that I
and other speakers tended to give the typical audience too many topics, too fast. So
eventually, through both variety in the audience level and the way that I presented the
material, I would end up losing some portion of the audience. Maybe it’s asking too much, but
because I am one of those people resistant to traditional lecturing (and for most people, I
believe, such resistance results from boredom), I wanted to try to keep everyone up to speed.
For a time, I was creating a number of different presentations in fairly short order. Thus, I
ended up learning by experiment and iteration (a technique that also works well in C++
program design). Eventually I developed a course using everything I had learned from my
teaching experience, one I would be happy giving for a long time. It tackles the learning
1
Bjarne Stroustrup,
The C++ Programming Language
, Addison-Wesley, 1986 (first edition).
2
Using C++
,
ibid.
3
Using C++
and
C++ Inside & Out
,
ibid.
Preface 19
problem in discrete, easy-to-digest steps and for a hands-on seminar (the ideal learning
situation), there are exercises following each of the short lessons.
This book developed over the course of two
years, and the material in this book has been
road-tested in many forms in many different seminars. The feedback that I’ve gotten from
each seminar has helped me change and refocus the material until I feel it works well as a
teaching medium. But it isn’t just a seminar handout – I tried to pack as much information as I
could within these pages, and structure it to draw you through, onto the next subject. More
than anything, the book is designed to serve the solitary reader, struggling with a new
programming language.
Goals
My goals in this book are to:
1.
Present the material a simple step at a time, so the reader can easily digest
each concept before moving on.
2.
Use examples that are as simple and short as possible. This sometimes
prevents me from tackling “real-world” problems,
but I’ve found that beginners are usually happier when they can understand
every detail of an example rather than being impressed by the scope of the
problem it solves. Also, there’s a severe limit to the amount of code that can
be absorbed in a classroom situation. For this I will no doubt receive
criticism for using “toy examples,” but I’m willing to accept that in favor of
producing something pedagogically useful. Those who want more complex
examples can refer to the later chapters of
C++ Inside & Out
.
4
3.
Carefully sequence the presentation of features so that you aren’t seeing
something you haven’t been exposed to. Of course, this isn’t always
possible; in those situations, a brief introductory description will be given.
4.
Give you what I think is important for you to understand about the
language, rather than everything I know. I believe there is an “information
importance hierarchy,” and there are some facts that 95% of programmers
will never need to know, but would just confuse people and add to their
perception of the complexity of the language – and C++ is now considered
to be more complex than ADA! To take an example from C, if you
memorize the operator precedence table (I never did) you can write clever
code. But if you have to think about it, it will confuse the reader/maintainer
4
Ibid.
Preface 20
of that code. So forget about precedence, and use parentheses when things
aren’t clear. This same attitude will be taken with some information in the
C++ language, which I think is more important for compiler writers than for
programmers.
5.
Keep each section focused enough so the lecture time – and the time
between exercise periods – is small. Not only does this keep the audience’
minds more active and involved during a hands-on seminar, but it gives the
reader a greater sense of accomplishment.
6.
Provide the reader with a solid foundation so they can understand the issues
well enough to move on to more difficult coursework and books.
7.
I’ve endeavored not to use any particular vendor’s version of C++ because,
for learning the language, I don’t feel like the details of a particular
implementation are as important as the language itself. Most vendors’
documentation concerning their own implementation specifics is adequate.
Chapters
C++ is a language where new and different features are built on top of an existing syntax.
(Because of this it is referred to as a
hybrid
object-oriented programming language.) As more
people have passed through the learning curve, we’ve begun to get a feel for the way C
programmers move through the stages of the C++ language features. Because it appears to be
the natural progression of the C-trained mind, I decided to understand and follow this same
path, and accelerate the process by posing and answering the questions that came to me as I
learned the language and that came from audiences as I taught it.
This course was designed with one thing in mind: the way people learn the C++ language.
Audience feedback helped me understand which parts were difficult and needed extra
illumination. In the areas where I got ambitious and included too many features all at once, I
came to know – through the process of presenting the material – that if you include a lot of
new features, you have to explain them all, and the student’s confusion is easily compounded.
As a result, I’ve taken a great deal of trouble to introduce the features as few at a time as
possible; ideally, only one at a time per chapter.
The goal, then, is for each chapter to teach a single feature, or a small group of associated
features, in such a way that no additional features are relied upon. That way you can digest
each piece in the context of your current knowledge before moving on. To accomplish this, I
leave many C features in place much longer than I would prefer. For example, I would like to
be using the C++ iostreams IO library right away, instead of using the
printf( )
family of
functions so familiar to C programmers, but that would require introducing the subject
prematurely, and so many of the early chapters carry the C library functions with them. This
is also true with many other features in the language. The benefit is that you, the C
Preface 21
programmer, will not be confused by seeing all the C++ features used before they are
explained, so your introduction to the language will be gentle and will mirror the way you will
assimilate the features if left to your own devices.
Here is a brief description of the chapters contained in this book [[ Please note this section
will not be updated until all the chapters are in place ]]
(0) The evolution of objects.
When projects became too big and too complicated to easily
maintain, the “software crisis” was born, saying, “We can’t get projects done, and if we can
they’re too expensive!” This precipitated a number of responses, which are discussed in this
chapter along with the ideas of object-oriented programming (OOP) and how it attempts to
solve the software crisis. You’ll also learn about the benefits and concerns of adopting the
language and suggestions for moving into the world of C++.
(1) Data abstraction.
Most features in C++ revolve around this key concept: the ability to
create new data types. Not only does this provide superior code organization, but it lays the
ground for more powerful OOP abilities. You’ll see how this idea is facilitated by the simple
act of putting functions inside structures, the details of how to do it, and what kind of code it
creates.
(2) Hiding the implementation.
You can decide that some of the data and functions in your
structure are unavailable to the user of the new type by making them
private
. This means you
can separate the underlying implementation from the interface that the client programmer
sees, and thus allow that implementation to be easily changed without affecting client code.
The keyword
class
is also introduced as a fancier way to describe a new data type, and the
meaning of the word “object” is demystified (it’s a variable on steroids).
(3) Initialization & cleanup.
One of the most common C errors results from uninitialized
variables. The
constructor
in C++ allows you to guarantee that variables of your new data
type (“objects of your class”) will always be properly initialized. If your objects also require
some sort of cleanup, you can guarantee that this cleanup will always happen with the C++
destructor
.
(4) Function overloading & default arguments.
C++ is intended to help you build big,
complex projects. While doing this, you may bring in multiple libraries that use the same
function name, and you may also choose to use the same name with different meanings within
a single library. C++ makes this easy with
function overloading
, which allows you to reuse
the same function name as long as the argument lists are different. Default arguments allow
you to call the same function in different ways by automatically providing default values for
some of your arguments.
(5) Introduction to iostreams.
One of the original C++ libraries – the one that provides the
essential I/O facility – is called iostreams. Iostreams is intended to replace C’s
stdio.h
with an
I/O library that is easier to use, more flexible, and extensible – you can adapt it to work with
your new classes. This chapter teaches you the ins and outs of how to make the best use of the
existing iostream library for standard I/O, file I/O, and in-memory formatting.
Preface 22
(6) Constants.
This chapter covers the
const
and
volatile
keywords that have additional
meaning in C++, especially inside classes. It also shows how the meaning of
const
varies
inside and outside classes and how to create compile-time constants in classes.
(7) Inline functions.
Preprocessor macros eliminate function call overhead, but the
preprocessor also eliminates valuable C++ type checking. The inline function gives you all
the benefits of a preprocessor macro plus all the benefits of a real function call.
(8) Name control.
Creating names is a fundamental activity in programming, and when a
project gets large, the number of names can be overwhelming. C++ allows you a great deal of
control over names: creation, visibility, placement of storage, and linkage. This chapter shows
how names are controlled using two techniques. First, the
static
keyword is used to control
visibility and linkage, and its special meaning with classes is explored. A far more useful
technique for controlling names at the global scope is C++’s
namespace
feature, which
allows you to break up the global name space into distinct regions.
(9) References & the copy-constructor.
C++ pointers work like C pointers with the
additional benefit of stronger C++ type checking. There’s a new way to handle addresses;
from Algol and Pascal, C++ lifts the
reference
which lets the compiler handle the address
manipulation while you use ordinary notation. You’ll also meet the copy-constructor, which
controls the way objects are passed into and out of functions by value. Finally, the C++
pointer-to-member is illuminated.
(10) Operator overloading.
This feature is sometimes called “syntactic sugar.” It lets you
sweeten the syntax for using your type by allowing operators as well as function calls. In this
chapter you’ll learn that operator overloading is just a different type of function call and how
to write your own, especially the sometimes-confusing uses of arguments, return types, and
making an operator a member or friend.
(11) Dynamic object creation.
How many planes will an air-traffic system have to handle?
How many shapes will a CAD system need? In the general programming problem, you can’t
know the quantity, lifetime or type of the objects needed by your running program. In this
chapter, you’ll learn how C++’s
new
and
delete
elegantly solve this problem by safely
creating objects on the heap.
(12) Inheritance & composition.
Data abstraction allows you to create new types from
scratch; with composition and inheritance, you can create new types from existing types. With
composition you assemble a new type using other types as pieces, and with inheritance you
create a more specific version of an existing type. In this chapter you’ll learn the syntax, how
to redefine functions, and the importance of construction and destruction for inheritance &
composition.
(13) Polymorphism & virtual functions.
On your own, you might take nine months to
discover and understand this cornerstone of OOP. Through small, simple examples you’ll see
how to create a family of types with inheritance and manipulate objects in that family through
their common base class. The
virtual
keyword allows you to treat all objects in this family
generically, which means the bulk of your code doesn’t rely on specific type information.
Preface 23
This makes your programs extensible, so building programs and code maintenance is easier
and cheaper.
(14) Templates & container classes.
Inheritance and composition allow you to reuse object
code, but that doesn’t solve all your reuse needs. Templates allow you to reuse
source
code by
providing the compiler with a way to substitute type names in the body of a class or function.
This supports the use of
container class
libraries, which are important tools for the rapid,
robust development of object-oriented programs. This extensive chapter gives you a thorough
grounding in this essential subject.
(15) Multiple inheritance.
This sounds simple at first: A new class is inherited from more
than one existing class. However, you can end up with ambiguities and multiple copies of
base-class objects. That problem is solved with virtual base classes, but the bigger issue
remains: When do you use it? Multiple inheritance is only essential when you need to
manipulate an object through more than one common base class. This chapter explains the
syntax for multiple inheritance, and shows alternative approaches – in particular, how
templates solve one common problem. The use of multiple inheritance to repair a “damaged”
class interface is demonstrated as a genuinely valuable use of this feature.
(16) Exception handling.
Error handling has always been a problem in programming. Even if
you dutifully return error information or set a flag, the function caller may simply ignore it.
Exception handling is a primary feature in C++ that solves this problem by allowing you to
“throw” an object out of your function when a critical error happens. You throw different
types of objects for different errors, and the function caller “catches” these objects in separate
error handling routines. If you throw an exception, it cannot be ignored, so you can guarantee
that
something
will happen in response to your error.
(17) Run-time type identification.
Run-time type identification (RTTI) lets you find the
exact type of an object when you only have a pointer or reference to the base type. Normally,
you’ll want to intentionally ignore the exact type of an object and let the virtual function
mechanism implement the correct behavior for that type. But occasionally it is very helpful to
know the exact type of an object for which you only have a base pointer; often this
information allows you to perform a special-case operation more efficiently. This chapter
explains what RTTI is for and how to use it.
Exercises
I’ve discovered that simple exercises are exceptionally useful during a seminar to complete a
student’s understanding, so you’ll find a set at the end of each chapter.
These are fairly simple, so they can be finished in a reasonable amount of time in a classroom
situation while the instructor observes, making sure all the students are absorbing the material.
Some exercises are a bit more challenging to keep advanced students entertained. They’re all
designed to be solved in a short time and are only there to test and polish your knowledge
rather than present major challenges (presumably, you’ll find those on your own – or more
likely they’ll find you).
Preface 24
Source code
The source code for this book is copyrighted freeware, distributed via the web site
. The copyright prevents you from republishing the code in print
media without permission.
To unpack the code, you download the text version of the book and run the program
ExtractCode
(from chapter 23), the source for which is also provided on the Web site. The
program will create a directory for each chapter and unpack the code into those directories. In
the starting directory where you unpacked the code you will find the following copyright
notice:
//:! :CopyRight.txt
Copyright (c) Bruce Eckel, 1999
Source code file from the book "Thinking in C++"
All rights reserved EXCEPT as allowed by the
following statements: You can freely use this file
for your own work (personal or commercial),
including modifications and distribution in
executable form only. Permission is granted to use
this file in classroom situations, including its
use in presentation materials, as long as the book
"Thinking in C++" is cited as the source.
Except in classroom situations, you cannot copy
and distribute this code; instead, the sole
distribution point is
(and official mirror sites) where it is
freely available. You cannot remove this
copyright and notice. You cannot distribute
modified versions of the source code in this
package. You cannot use this file in printed
media without the express permission of the
author. Bruce Eckel makes no representation about
the suitability of this software for any purpose.
It is provided "as is" without express or implied
warranty of any kind, including any implied
warranty of merchantability, fitness for a
particular purpose or non-infringement. The entire
risk as to the quality and performance of the
software is with you. Bruce Eckel and the
publisher shall not be liable for any damages
suffered by you or any third party as a result of
using or distributing software. In no event will
Preface 25
Bruce Eckel or the publisher be liable for any
lost revenue, profit, or data, or for direct,
indirect, special, consequential, incidental, or
punitive damages, however caused and regardless of
the theory of liability, arising out of the use of
or inability to use software, even if Bruce Eckel
and the publisher have been advised of the
possibility of such damages. Should the software
prove defective, you assume the cost of all
necessary servicing, repair, or correction. If you
think you've found an error, please submit the
correction using the form you will find at
www.BruceEckel.com. (Please use the same
form for non-code errors found in the book.)
///:~
You may use the code in your projects and in the classroom as long as the copyright notice is
retained.
Coding standards
In the text of this book, identifiers (function, variable, and class names) will be set in
bold
.
Most keywords will also be set in bold, except for those keywords which are used so much
that the bolding can become tedious, like class and virtual.
I use a particular coding style for the examples in this book. It was developed over a number
of years, and was inspired by Bjarne Stroustrup’s style in his original
The C++ Programming
Language
.
5
The subject of formatting style is good for hours of hot debate, so I’ll just say I’m
not trying to dictate correct style via my examples; I have my own motivation for using the
style that I do. Because C++ is a free-form programming language, you can continue to use
whatever style you’re comfortable with.
The programs in this book are files that are automatically extracted from the text of the book,
which allows them to be tested to ensure they work correctly. (I use a special format on the
first line of each file to facilitate this extraction; the line begins with the characters ‘
/’ ‘/’
‘
:’
and the file name and path information.) Thus, the code files printed in the book should all
work without compiler errors when compiled with an implementation that conforms to
Standard C++ (note that not all compilers support all language features). The errors that
should
cause compile-time error messages are commented out with the comment
//!
so they
can be easily discovered and tested using automatic means. Errors discovered and reported to
the author will appear first in the electronic version of the book (at
www.BruceEckel.com
)
and later in updates of the book.
5
Ibid.
Preface 26
One of the standards in this book is that all programs will compile and link without errors
(although they will sometimes cause warnings). To this end, some of the programs, which
only demonstrate a coding example and don’t represent stand-alone programs, will have
empty
main( )
functions, like this
int main() {}
This allows the linker to complete without an error.
The standard for
main( )
is to return an
int
, but Standard C++ states that if there is no
return
statement inside
main( )
, the compiler will automatically generate code to
return 0
. This
option will be used in this book (although some compilers may still generate warnings for
this).
Language standards
Throughout this book, when referring to conformance to the ANSI/ISO C standard, I will
generally just say ‘
C
.’ Only if it is necessary to distinguish between Standard C and older,
pre-Standard versions of C will I make the distinction.
At this writing the ANSI/ISO C++ committee was finished working on the language. Thus, I
will use the term
Standard C++
to refer to the standardized language. If I simply refer to C++
you should assume I mean “Standard C++.”
Language support
Your compiler may not support all the features discussed in this book, especially if you don’t
have the newest version of your compiler. Implementing a language like C++ is a Herculean
task, and you can expect that the features will appear in pieces rather than all at once. But if
you attempt one of the examples in the book and get a lot of errors from the compiler, it’s not
necessarily a bug in the code or the compiler – it may simply not be implemented in your
particular compiler yet.
Seminars & CD Roms
My company provides public hands-on training seminars based on the material in this book.
Selected material from each chapter represents a lesson, which is followed by a monitored
exercise period so each student receives personal attention. Information and sign-up forms for
upcoming seminars can be found at
. If you have specific
questions, you may direct them to
.
Preface 27
Errors
No matter how many tricks a writer uses to detect errors, some always creep in and these
often leap off the page for a fresh reader. If you discover anything you believe to be an error,
please use the correction form you will find at
. Your help is
appreciated.
Acknowledgements
The ideas and understanding in this book have come from many sources: friends like Dan
Saks, Scott Meyers, Charles Petzold, and Michael Wilk; pioneers of the language like Bjarne
Stroustrup, Andrew Koenig, and Rob Murray; members of the C++ Standards Committee like
Nathan Myers (who was particularly helpful and generous with his insights), Tom Plum, Reg
Charney, Tom Penello, Chuck Allison, Sam Druker, and Uwe Steinmueller; people who have
spoken in my C++ track at the Software Development Conference; and very often students in
my seminars, who ask the questions I need to hear in order to make the material clearer.
I have been presenting this material on tours produced by Miller Freeman Inc. with my friend
Richard Hale Shaw. Richard’s insights and support have been very helpful (and Kim’s, too).
Thanks also to KoAnn Vikoren, Eric Faurot, Jennifer Jessup, Nicole Freeman, Barbara
Hanscome, Regina Ridley, Alex Dunne, and the rest of the cast and crew at MFI.
The book design, cover design, and cover photo were created by my friend Daniel Will-
Harris, noted author and designer, who used to play with rub-on letters in junior high school
while he awaited the invention of computers and desktop publishing. However, I produced the
camera-ready pages myself, so the typesetting errors are mine. Microsoft
®
Word for Windows
97 was used to write the book and to create camera-ready pages. The body typeface is [Times
for the electronic distribution] and the headlines are in [Times for the electronic distribution].
The people at Prentice Hall were wonderful. Thanks to Alan Apt, Sondra Chavez, Mona
Pompili, Shirley McGuire, and everyone else there who made life easy for me.
A special thanks to all my teachers, and all my students (who are my teachers as well).
Personal thanks to my friends Gen Kiyooka and Kraig Brockschmidt. The supporting cast of
friends includes, but is not limited to: Zack Urlocker, Andrew Binstock, Neil Rubenking,
Steve Sinofsky, JD Hildebrandt, Brian McElhinney, Brinkley Barr, Larry O’Brien, Bill Gates
at Midnight Engineering Magazine, Larry Constantine & Lucy Lockwood, Tom Keffer, Greg
Perry, Dan Putterman, Christi Westphal, Gene Wang, Dave Mayer, David Intersimone, Claire
Sawyers, Claire Jones, The Italians (Andrea Provaglio, Laura Fallai, Marco Cantu, Corrado,
Ilsa and Christina Giustozzi), Chris & Laura Strand, The Almquists, Brad Jerbic, Marilyn
Cvitanic, The Mabrys, The Haflingers, The Pollocks, Peter Vinci, The Robbins Families, The
Moelter Families (& the McMillans), The Wilks, Dave Stoner, Laurie Adams, The Penneys,
The Cranstons, Larry Fogg, Mike & Karen Sequeira, Gary Entsminger & Allison Brody,
Preface 28
Chester Andersen, Joe Lordi, Dave & Brenda Bartlett, The Rentschlers, The Sudeks, Lynn &
Todd, and their families. And of course, Mom & Dad.
29
1: Introduction
to objects
The genesis of the computer revolution was in a machine.
The genesis of our programming languages thus tends to
look like that machine.
But computers are not so much machines as they are mind amplification tools (“bicycles for
the mind,” as Steve Jobs is fond of saying) and a different kind of expressive medium. As a
result, the tools are beginning to look less like machines and more like parts of our minds, and
also like other expressive mediums such as writing, painting, sculpture, animation and
filmmaking. Object-oriented programming is part of this movement toward the computer as
an expressive medium.
This chapter will introduce you to the basic concepts of object-oriented programming (OOP),
including an overview of OOP development methods. This chapter, and this book, assume
you have had experience in some programming language, although not necessarily C. If you
feel you need more preparation in programming and the syntax of C before tackling this book,
you may want to consider MindView’s “Thinking in C: Foundations for C++ and Java”
training CD ROM, available at .
This chapter is background and supplementary material. Many people do not feel comfortable
wading into object-oriented programming without understanding the big picture first. Thus,
there are many concepts that are introduced here to give you a solid overview of OOP.
However, many other people don’t get the big picture concepts until they’ve seen some of the
mechanics first; these people may become bogged down and lost without some code to get
their hands on. If you’re part of this latter group and are eager to get to the specifics of the
language, feel free to jump past this chapter – skipping it at this point will not prevent you
from writing programs or learning the language. However, you will want to come back here
eventually, to fill in your knowledge so that you can understand why objects are important
and how to design with them.
The progress of abstraction
All programming languages provide abstractions. It can be argued that the complexity of the
problems you’re able to solve is directly related to the kind and quality of abstraction. By
“kind” I mean “what is it that you are abstracting?” Assembly language is a small abstraction