Contents i What’s New with This Edition Teach Yourself ANSI C++ in 21 Days, Premier Edition, is - Pdf 68

i
Contents
P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3
What’s New with This Edition
Teach Yourself ANSI C++ in 21 Days, Premier Edition, is a new edition of the
international bestseller, Teach Yourself C++ in 21 Days. This Premier Edition offers
more than just 21 days of learning ANSI/ISO C++.
Major changes throughout this edition have been made to reflect the latest ANSI/
ISO C++ draft standard. In addition, seven new lessons, referred to as “Bonus Days,”
have been added to provide you with additional information for creating more
sophisticated programs. The Bonus Day lessons are not required reading in order
for you to learn to program in C++. As a matter of fact, on the very first day you will
write a simple C++ program.
On Bonus Day 22, “Advanced C++ Features,” you learn to use a collection of
specialized ANSI C++ tools that many programmers don’t even realize exist. These
tools are not essential for every C++ programmer to know but are indispensable in
many circumstances.
Bonus Day 23, “More About Streams,” covers some of the more advanced streams
programming techniques. After completing this lesson, you will have all you need
to create new manipulators for the stream classes you learn to create on Day 16. You
will also learn a great deal about other streams programming techniques that space
would not allow us to cover on Day 16.
On Bonus Day 24, “Object-Oriented Design,” you learn more about this important
subject. Without object-oriented design techniques, C++ is “just another C” with
few advantages over its predecessor. This lesson helps you think in a way that builds
on the object-oriented design paradigm.
Bonus Days 25 and 26, “Data Structures” and “Simple Sorting and Searching
Algorithms,” respectively, introduce you to the inner sanctum of computer science.
On these two days you learn how to solve several common categories of problems.
“Common Mistakes and Basic Debugging,” on Bonus Day 27, is designed to help
you avoid some of the common mistakes that many beginning programmers make.

S MS
Teach Yourself ANSI C++ in 21 Days
vi
P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3
Acquisitions Editor
Bradley L. Jones
Development Editor
Anthony Amico
Production Editor
Ryan Rader
Copy Editors
Howard Jones
Marla Reece
Technical Reviewer
Justin Bell
John W. Charlesworth
Editorial Coordinator
Bill Whitmer
Technical Edit Coordinator
Lynette Quinn
Formatter
Frank Sinclair
Editorial Assistants
Sharon Cox
Andi Richter
Rhonda Tinch-Mize
Cover Designer
Tim Amrhein
Book Designer
Gary Adair

99 98 97 96 4 3 2 1
Interpretation of the printing code: the rightmost double-digit number is
the year of the book’s printing; the rightmost single-digit, the number of the
book’s printing. For example, a printing code of 96-1 shows that the first
printing of the book occurred in 1996.
Composed in Agaramond and MCPdigital by Macmillan Computer Publishing
Printed in the United States of America
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to
the accuracy of this information. Use of a term in this book should not be
regarded as affecting the validity of any trademark or service mark.
Publisher and President
Richard K. Swadley
Acquisitions Manager
Greg Wiegand
Development Manager
Dean Miller
Managing Editor
Cindy Morrow
Marketing Manager
Gregg Bushyeager
vii
Contents
P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3
Overview
Week 1 At A Glance 1
1 Getting Started 3
2 The Parts of a Program 19
3 Variables and Constants 31
4 Expressions and Statements 55

26 Simple Sorting and Searching Algorithms 701
27 Common Mistakes and Basic Debugging 721
28 What’s Next? 733
Appendixes
A Operator Precedence 741
B C++ Keywords 745
C Binary and Hexadecimal 747
D Answers 757
Index 831
ix
Contents
P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3
Contents
Week 1 At A Glance 1
A Note to C Programmers .......................................................................... 2
Where You Are Going ................................................................................ 2
Day 1 Getting Started 3
A Brief History of C++ ............................................................................... 3
ANSI/ISO C++ .......................................................................................... 5
What Is a Program? .................................................................................... 5
Sophisticated Tools for Sophisticated Programming ................................... 6
Procedural, Structured, and Object-Oriented Programming .................. 6
C++ and Object-Oriented Programming ............................................... 8
How C++ Evolved ................................................................................. 9
Should I Learn C First? ........................................................................ 10
Preparing to Program ............................................................................... 10
Your Development Environment.............................................................. 11
Compiling the Source Code ..................................................................... 12
Creating an Executable File with the Linker ............................................. 12
The Development Cycle ........................................................................... 13

Fundamental Variable Types ............................................................... 35
Defining a Variable................................................................................... 36
Case Sensitivity .................................................................................... 37
Keywords ............................................................................................. 38
Creating More Than One Variable
at a Time................................................................................................ 39
Assigning Values to Your Variables ........................................................... 39
typedef
.................................................................................................... 40
Choosing Between
short
and
long
........................................................... 41
Wrapping Around in Unsigned Integers .............................................. 42
Wrapping Around a Signed Integer ..................................................... 43
Characters ................................................................................................ 43
chars
and Numbers ............................................................................. 44
Special Printing Characters .................................................................. 44
Constants ................................................................................................. 46
Literal Constants.................................................................................. 46
Symbolic Constants ............................................................................. 46
Enumerated Constants ............................................................................. 49
Summary .................................................................................................. 50
Q&A ........................................................................................................ 51
Quiz ......................................................................................................... 52
Exercises ................................................................................................... 53
4 Expressions and Statements 55
Statements ................................................................................................ 56

Advanced
if
Statements ...................................................................... 75
Using Braces in Nested
if
Statements.................................................. 76
Logical Operators ..................................................................................... 79
Logical AND ....................................................................................... 79
Logical OR .......................................................................................... 80
Logical NOT ....................................................................................... 80
Relational Precedence ............................................................................... 80
More About Truth and Falsehood ............................................................ 81
Conditional (Ternary) Operator ............................................................... 82
Summary .................................................................................................. 84
Q&A ........................................................................................................ 84
Quiz ......................................................................................................... 85
Exercises ................................................................................................... 85
5 Functions 87
What Is a Function? ................................................................................. 88
Declaring and Defining Functions............................................................ 88
Declaring the Function ........................................................................ 89
Function Prototypes ............................................................................ 89
Defining the Function ......................................................................... 91
Execution of Functions ........................................................................ 93
Local Variables ......................................................................................... 93
Global Variables ....................................................................................... 95
More on Local Variables ........................................................................... 97
Function Statements ................................................................................. 99
Function Arguments ................................................................................. 99
Using Functions as Parameters to Functions ........................................ 99

Make Member Data Private ............................................................... 130
Privacy Versus Security ...................................................................... 132
Implementing Class Methods ................................................................. 134
Constructors and Destructors ................................................................. 136
Default Constructors and Destructors................................................ 137
const
Member Functions ....................................................................... 140
Interface Versus Implementation ............................................................ 141
Where to Put Class Declarations and Method Definitions...................... 143
Inline Implementation............................................................................ 145
Classes with Other Classes
as Member Data ................................................................................... 147
Structures ............................................................................................... 150
Why Two Keywords Do the Same Thing .......................................... 151
Summary ................................................................................................ 152
Q&A ...................................................................................................... 152
Quiz ....................................................................................................... 153
Exercises ................................................................................................. 154
7 More Program Flow 155
Looping .................................................................................................. 156
The Roots of Looping
goto
............................................................... 156
Why
goto
Is Shunned........................................................................ 157
while
Loops ........................................................................................... 157
More Complicated
while

switch
Statement with a Menu ............................................. 180
Summary ................................................................................................ 183
Q&A ...................................................................................................... 183
Quiz ....................................................................................................... 184
Exercises ................................................................................................. 184
Week 1 In Review 187
Week In Review ..................................................................................... 192
Week 2 At A Glance 193
Where You Are Going ............................................................................ 193
Day 8 Pointers 195
What Is a Pointer? .................................................................................. 195
Storing the Address in a Pointer......................................................... 197
The Indirection Operator .................................................................. 199
Pointers, Addresses, and Variables...................................................... 200
Manipulating Data by Using Pointers ................................................ 201
Examining the Address ...................................................................... 202
Why Would You Use Pointers? .............................................................. 204
The Stack and the Free Store .................................................................. 204
new
..................................................................................................... 206
delete
............................................................................................... 206
Memory Leaks ................................................................................... 208
Creating Objects on the Free Store .................................................... 209
Deleting Objects ................................................................................ 210
Accessing Data Members ................................................................... 211
Member Data on the Free Store......................................................... 212
The
this

Returning Multiple Values ..................................................................... 235
Returning Values by Reference .......................................................... 237
Passing by Reference for Efficiency ......................................................... 238
Passing a
const
Pointer ...................................................................... 241
References as an Alternative ............................................................... 243
Choosing Between References and Pointers ............................................ 245
Pointer References .................................................................................. 246
Mixing References and Pointers.............................................................. 248
Don’t Return a Reference to an Object that Isn’t in Scope! .................... 249
Returning a Reference to an Object
on the Heap ......................................................................................... 251
Who Has the Pointer? ............................................................................ 253
Summary ................................................................................................ 253
Q&A ...................................................................................................... 254
Quiz ....................................................................................................... 254
Exercises ................................................................................................. 254
10 Advanced Functions 257
Overloaded Member Functions .............................................................. 257
Using Default Values .............................................................................. 260
Choosing Between Default Values and Overloaded Functions................ 262
The Default Constructor ........................................................................ 262
Overloading Constructors ...................................................................... 263
Initializing Objects ................................................................................. 264
The Copy Constructor ........................................................................... 265
Operator Overloading ............................................................................ 270
Writing an Increment Function ......................................................... 271
Overloading the Prefix Operator........................................................ 272
Returning Types in Overloaded Operator Functions ......................... 273

Fence Post Errors .................................................................................... 298
Initializing Arrays ................................................................................... 298
Declaring Arrays ..................................................................................... 299
Arrays of Objects .................................................................................... 301
Multidimensional Arrays ........................................................................ 302
Initializing Multidimensional Arrays....................................................... 303
A Word About Memory ......................................................................... 304
Arrays of Pointers ................................................................................... 305
Declaring Arrays on the Free Store ......................................................... 306
A Pointer to an Array Versus an Array of Pointers .................................. 307
Pointers and Array Names ...................................................................... 307
Deleting Arrays on the Free Store ........................................................... 309
char
Arrays ............................................................................................. 310
strcpy()
and
strncpy()
........................................................................ 312
String Classes.......................................................................................... 313
Linked Lists and Other Structures .......................................................... 320
Array Classes........................................................................................... 325
Summary ................................................................................................ 326
Q&A ...................................................................................................... 327
Quiz ....................................................................................................... 327
Exercises ................................................................................................. 328
12 Inheritance 329
What Is Inheritance? ............................................................................... 329
Inheritance and Derivation ................................................................ 330
The Animal Kingdom ........................................................................ 331
The Syntax of Derivation................................................................... 331

Pure Virtual Functions....................................................................... 384
Implementing Pure Virtual Functions ............................................... 385
Complex Hierarchies of Abstraction .................................................. 389
Which Types Are Abstract?................................................................ 392
Summary ................................................................................................ 393
Q&A ...................................................................................................... 393
Quiz ....................................................................................................... 394
Exercises ................................................................................................. 394
14 Special Classes and Functions 395
Static Member Data ............................................................................... 395
Static Member Functions ....................................................................... 400
Pointers to Functions.............................................................................. 402
Why Use Function Pointers? ............................................................. 406
Arrays of Pointers to Functions .......................................................... 408
Passing Pointers to Functions to Other Functions ............................. 410
Using
typedef
with Pointers to Functions ......................................... 412
Pointers to Member Functions ............................................................... 413
Arrays of Pointers to Member Functions............................................ 415
Summary ................................................................................................ 417
Q&A ...................................................................................................... 418
Quiz ....................................................................................................... 418
Exercises ................................................................................................. 419
Week 2 In Review 421
Week 3 At A Glance 431
Where You Are Going ............................................................................ 431
Day 15 Advanced Inheritance 433
Containment .......................................................................................... 433
Accessing Members of the Contained Class ....................................... 439

Strings ............................................................................................... 476
String Problems ................................................................................. 477
operator>>
Returns a Reference to an
istream
Object ...................... 479
Other Member Functions of
cin
............................................................ 480
Single Character Input ....................................................................... 480
Getting Strings from Standard Input ................................................. 482
Using
cin.ignore()
.......................................................................... 485
peek()
and
putback()
....................................................................... 486
Output with
cout
................................................................................... 487
Flushing the Output .......................................................................... 487
Related Functions ................................................................................... 488
Manipulators, Flags, and Formatting Instructions .................................. 489
Using
cout.width()
.......................................................................... 490
Setting the Fill Characters .................................................................. 491
Set Flags ............................................................................................ 491
Streams Versus the

#define
for Tests ..................................................................... 511
The
#else
Precompiler Command .................................................... 511
The
#error
Directive.............................................................................. 512
The
#pragma
Directive ............................................................................ 513
Inclusion and Inclusion Guards .............................................................. 513
Defining on the Command Line........................................................ 514
Undefining ........................................................................................ 514
Conditional Compilation .................................................................. 516
Macro Functions .................................................................................... 516
Why All the Parentheses? ................................................................... 517
Macros Versus Functions and Templates ........................................... 519
Inline Functions ..................................................................................... 519
String Manipulation ............................................................................... 521
Stringizing ......................................................................................... 521
Concatenation ................................................................................... 521
Predefined Macros .................................................................................. 522
assert()
................................................................................................ 522
Debugging with
assert()
................................................................. 524
assert()
Versus Exceptions............................................................... 524

Parameterized Types ............................................................................... 552
Template Definition ............................................................................... 552
Using the Name................................................................................. 554
Implementing the Template .............................................................. 554
Template Functions................................................................................ 558
Templates and Friends............................................................................ 558
Nontemplate Friend Classes and Functions ....................................... 559
General Template Friend Class or Function ...................................... 561
A Type-Specific Template Friend Class or Function .......................... 563
Using Template Items ............................................................................ 564
Specialized Functions......................................................................... 567
Static Members and Templates .......................................................... 573
The Standard Template Library.............................................................. 575
Summary ................................................................................................ 576
Q&A ...................................................................................................... 577
Quiz ....................................................................................................... 578
Exercises ................................................................................................. 578
20 Exceptions and Error Handling 581
Bugs, Errors, Mistakes, and Code Rot .................................................... 582
Exceptions ......................................................................................... 583
A Word About Code Rot ................................................................... 583
Preparing for Exceptions With C++........................................................ 584
How Exceptions Are Used ................................................................. 584
Using
try
Blocks and
catch
Blocks ........................................................ 589
Catching Exceptions .......................................................................... 590
More Than One

Summary ................................................................................................ 621
Q&A ...................................................................................................... 621
Quiz ....................................................................................................... 622
Exercises ................................................................................................. 622
Week 3 In Review 625
Bonus Week At A Glance 635
Day 22 Advanced C++ Features 637
Namespaces and Class Libraries .............................................................. 638
Namespaces ....................................................................................... 638
Class Library Design .......................................................................... 640
How Polymorphism Works .................................................................... 643
Casting and Run-Time Type Identification ............................................ 644
Const
Cast ......................................................................................... 645
Dynamic Cast .................................................................................... 646
Static Cast.......................................................................................... 649
Reinterpret Cast................................................................................. 650
Type Info........................................................................................... 651
Explicit Constructors .............................................................................. 651
Data Alignment and Assembly Language ................................................ 652
Packing Data by Understanding Data Alignment .............................. 653
Spiking with Assembly Language ....................................................... 653
Summary ................................................................................................ 654
Q&A ...................................................................................................... 655
Quiz ....................................................................................................... 655
Exercises ................................................................................................. 656
23 More About Streams 659
The Streams Hierarchy ........................................................................... 659
Using
istrstream

Quiz ....................................................................................................... 681
Exercises ................................................................................................. 681
25 Data Structures 683
Looking Back at Arrays ........................................................................... 683
Queues and Stacks .................................................................................. 684
A Simple Queue Example .................................................................. 684
A Simple Stack Example .................................................................... 688
Trees ...................................................................................................... 689
Summary ................................................................................................ 698
Q&A ...................................................................................................... 698
Quiz ....................................................................................................... 699
Exercises ................................................................................................. 699
26 Simple Sorting and Searching 701
Sorting.................................................................................................... 702
The Bubble Sort ................................................................................ 703
The Selection Sort ............................................................................. 706
The Shell Sort .................................................................................... 707
The Quick Sort.................................................................................. 710
Sorting Disk Files .............................................................................. 712
Teach Yourself ANSI C++ in 21 Days
xxii
P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3
Searching ................................................................................................ 712
The Straightforward String Search ..................................................... 712
The Binary Search ............................................................................. 714
Summary ................................................................................................ 717
Q&A ...................................................................................................... 717
Quiz ....................................................................................................... 718
Exercises ................................................................................................. 718
27 Common Mistakes and Basic Debugging 721

B C++ Keywords 745
xxiii
Contents
P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3
C Binary and Hexadecimal 747
Other Bases ............................................................................................ 748
Around the Bases .................................................................................... 749
Binary ................................................................................................ 750
Why Base 2? ...................................................................................... 751
Bits, Bytes, and Nibbles ..................................................................... 751
Binary Numbers ................................................................................ 752
Hexadecimal ........................................................................................... 752
D Answers 757
Day 1 ..................................................................................................... 758
Quiz .................................................................................................. 758
Exercises ............................................................................................ 758
Day 2 ..................................................................................................... 758
Quiz .................................................................................................. 758
Exercises ............................................................................................ 759
Day 3 ..................................................................................................... 760
Quiz .................................................................................................. 760
Exercises ............................................................................................ 761
Day 4 ..................................................................................................... 761
Quiz .................................................................................................. 761
Exercises ............................................................................................ 762
Day 5 ..................................................................................................... 764
Quiz .................................................................................................. 764
Exercises ............................................................................................ 765
Day 6 ..................................................................................................... 767
Quiz .................................................................................................. 767

Exercises ............................................................................................ 794
Day 16 ................................................................................................... 798
Quiz .................................................................................................. 798
Exercises ............................................................................................ 799
Day 17 ................................................................................................... 800
Quiz .................................................................................................. 800
Exercises ............................................................................................ 801
Day 18 ................................................................................................... 803
Quiz .................................................................................................. 803
Exercises ............................................................................................ 804
Day 19 ................................................................................................... 805
Quiz .................................................................................................. 805
Exercises ............................................................................................ 806
Day 20 ................................................................................................... 810
Quiz .................................................................................................. 810
Exercises ............................................................................................ 811
Day 21 ................................................................................................... 816
Quiz .................................................................................................. 816
Exercises ............................................................................................ 816
Day 22 ................................................................................................... 818
Quiz .................................................................................................. 818
Exercises ............................................................................................ 819
Day 23 ................................................................................................... 821
Quiz .................................................................................................. 821
Exercises ............................................................................................ 822
Day 24 ................................................................................................... 823
Quiz .................................................................................................. 823
Exercises ............................................................................................ 823
Day 25 ................................................................................................... 824
Quiz .................................................................................................. 824

therefore acknowledge Tom Hottenstein, Jay Leve, David Rollert, David Shnaider, and
Robert Spielvogel. I also want to thank the many people at Sams Publishing who worked
so hard to create this book. If any of what I’ve written is especially clear, it is thanks to the
editors.
Finally, I’d like to thank Mrs. Kalish, who taught my sixth-grade class how to do binary
arithmetic in 1965, when neither she nor we knew why.
—Jesse Liberty
The first people that come to mind are my family. They’ve been there through thick and thin,
shared my enthusiasm and bore the brunt of my frustrations. Praise God for their love and
devotion.
Maggie, if I had to do it all again, I’d still say “I do.” Timmy, you’re everything I could
ever want for a son; I’m very proud. Heather, thank you for your kindness and gentle spirit,
and for always loving Daddy even when he was cranky. Alicia, though you are small, your
love has always been big; you have been a wonderful daughter. Amber, thank you for your
gift of joy and fun that helped me keep focus on the things that really matter.
—J. Mark Hord


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status