TOC PREV NEXT INDEX


I've just released a deduplicating backup product for
VMWare Workstation and Server.
Do you need to reduce the storage needed to maintain multiple backups?
Or do you need multiple snapshots on VMWare Server?
If you have either of these needs, just click
here to get started for only $29 (limited time offer ends July 31st, 2011)!

C++: A Dialog


Table of Contents
List of Figures xix
How to Buy This Book xxxvii
Foreword xxxix
Preface xli
Acknowledgements xlvii
Letter from a Novice xlix
CHAPTER 1 Introduction to Programming 1
How to Write a Program 5
Baby Steps 7
On with the Show 9
CHAPTER 2 Hardware Fundamentals 11
Objectives of This Chapter 13
Inside the Box 14
Disk 14
Seventeen Years of Progress 17
RAM 18
Memory Addresses 19
The CPU 22
How Caches Improve Performance 27
How Registers Improve Performance 28
The Binary Number System 32
Signed Variables 37
A (Very) Short Course in Hexadecimal Notation 41
Exercises 48
Using the 16-bit Register Names 48
Revisiting the Memory Hierarchy 51
The Advantages and Disadvantages of Using Registers 52
The Effect of Registers on Program Size 54
Reading Instructions into Memory in Advance 61
Review 63
Conclusion 64
Answers to Exercises 64
CHAPTER 3 Basics of Programming 67
Objectives of This Chapter 68
Rapid and Accurate Calculation 69
The Real Reason for "Computer Problems" 70
Nonnumeric Variables 71
The Compiler 72
How the CPU Stores and Manipulates Data in Memory 77
The Layout of Data in Memory 80
Exercises, First Set 82
Playing Compiler 82
Executing Our Little Program Fragment 92
The char and string Types 96
using, namespace, and std 98
int main() 100
A Byte by Any Other Name... 106
Nonprinting Characters 107
Exercises, Second Set 109
Input and Output 110
Changing the Course of Execution 112
The while Loop 115
Exercises, Third Set 117
Our First Slightly Realistic Program 118
Susan Tries to Write the Pumpkin Program Herself 126
Try the Pumpkin Program Yourself 128
Exercises, Fourth Set 128
Review 129
Conclusion 132
Answers to Exercises 133
CHAPTER 4 More Basics 149
Algorithmic Thinking 150
Susan Finds a Bug 154
Even a Simple Change to a Program Can Cause Errors 159
Handling Any Number of Prizes 159
The vector Data Type 161
The Vec Data Type 162
A Historical Example of One-based Indexing 167
Index Variables 169
The Selection Sort 178
The Selection Sort in More Detail 189
Making Assumptions Can Be Dangerous 191
Why We're Using Vec Rather Than vector 194
Uninitialized Variables 194
Program Failure 200
Why We Need to Initialize Variables Explicitly 202
Preventing Improper Data Input 203
Review 207
Exercises 209
Conclusion 211
Answers to Exercises 212
CHAPTER 5 Functional Literacy 219
Objectives of This Chapter 223
Modules vs. Functions 223
An Example of Using a Function 229
Returning Data to the Calling Function 231
Function Arguments 233
How the Average Function Works 238
Using a Function 239
Software Is a Virtual Computer 247
Object Files 248
Operating System Facilities 250
Library Modules 252
Why All Variables Aren't Automatically Initialized 254
Nested Functions 258
Returning to the Calling Function 260
The Layout of Data in a Stack 262
Scope of Variables 267
Automatic vs. Static Allocation 268
The Scope Resolution Operator 273
The Disadvantages of Global Variables 282
Using a Very Primitive BASIC Language 283
The Solution to My Problem with BASIC 284
A Scope Defines a namespace 285
More on Using the Stack 285
Review 287
Exercises 290
Conclusion 291
Answers to Exercises 291
CHAPTER 6 Taking Inventory 295
Objectives of This Chapter 298
User-defined Data Types 299
The StockItem class 305
More Definitions 308
Concrete Data Types 309
The class Scope 327
More about the StockItem class Interface 333
Referring to Standard Library Identifiers in Header Files 335
The Need for More Than One Constructor 336
Handling a Number of StockItems with a Vec 344
Working around the Standard Library 348
The Second Version of the StockItem Interface 349
Reference Arguments 350
Fencepost Errors 357
Other Possible Transactions with the Inventory 360
Attempting to Access private Member Variables 363
Making the Program More User-friendly 367
The Inventory class 372
Using a Null Object 377
Checking Inventory for a Misplaced Item 391
Review 392
Exercises 401
Conclusion 402
Answers to Exercises 402
CHAPTER 7 Creating a Homegrown string class 405
C String Literals vs. strings 406
Pointers 414
Dynamic Memory Allocation via new and delete 417
Constructing a string from a C String 424
Calling the string(char*) Constructor 436
Assignment Operator Issues 438
Solving the Assignment Operator Problem 441
The const Modifier for Reference Arguments 444
Calling operator= 448
The Keyword this 450
Memory Allocation Errors 453
More on operator = 457
The Destructor 463
Review 466
Exercises 467
Conclusion 469
Answers to Exercises 470
CHAPTER 8 Finishing Our homegrown string class 473
Why We Need a Reference Argument for operator = 474
Premature Destruction 475
The Compiler Generates a Temporary Variable 478
The string Copy Constructor 483
Screen Output 485
The Array 489
The Equivalence of Arrays and Pointers 491
The char As a Very Short Numeric Variable 494
Running off the End of an Array 495
More about the private Access Specifier 500
The Advantages of Encapsulation 503
First Review 506
Adding Further Facilities to our string class 510
Implementing operator < 514
Details of the Comparison Algorithm 516
The Logical AND Operator 524
Other Possible Results of the Comparison 527
Using a Standard Library Function to Simplify the Code 528
Implementing operator == 531
Implementation vs. Declaration Revisited 533
Using cout With User-defined Types 533
How cout Works With Pre-existing Types 535
Writing Our Own Standard Library-Compatible operator << 536
The friend Keyword 538
Reading a string from an istream 540
Initialization vs. Assignment 542
The memset Standard Library Function 544
Second Review 549
Exercises 551
Conclusion 553
Answers to Exercises 554
CHAPTER 9 Inheritance 563
Objectives of This Chapter 565
Two Reasons to Use Inheritance 566
Taking Inventory 574
Adding ReorderItems to the Inventory class 575
Adding Expiration Dates 581
Reducing Maintenance Via Inheritance 584
A Note on Proper Object-oriented Design 589
Overriding a Base class Function 589
The protected Access Specifier 596
public Inheritance 613
private Inheritance 613
static Member Functions 616
The stream classes 621
More about stringstream 624
Controlling Output Formatting 629
Using iostream Manipulators 632
Base class Constructors 634
The Reorder Function for the DatedStockItem class 642
Review 651
Exercises 654
Conclusion 656
CHAPTER 10 Polymorphism 657
Objectives of This Chapter 659
Introduction to Polymorphism 659
The virtual Keyword 662
Problems with Using Pointers for Polymorphism 676
Using a virtual Function for I/O 687
References to Pointers 689
Exercises, First Set 693
Polymorphic Objects 694
More Definitions 694
Why We Need Polymorphic Objects 695
Using a Base class Pointer to Point to Derived class Objects 699
Implementing Safe Polymorphism 700
Reimplementing the Standard Member Functions for the New Version of StockItem 715
Avoiding an Infinite Regress During Construction 720
Reference Counting 723
Sharing a Worker Object 724
The Order of Destructor Calls 735
Tracing the Destruction of Our Polymorphic Objects 740
Why We Need m_Count in StockItem 743
Executing Code before the Beginning of main 744
Review 746
Exercises, Second Set 751
Conclusion 752
CHAPTER 11 The Home Inventory Project 753
Objectives of This Chapter 754
Data Items for the Home Inventory Project 755
Choosing Categories for Items 756
The Manager/Worker Idiom Again 758
Similarities and Differences between Polymorphic Objects 759
Hiding Unnecessary Information from the class User 763
The Initial HomeItem Test Program 765
Creating Local Variables When They Are Needed 778
The Scope of an Index Variable 779
One of the Oddities of the Value 0 781
Reusing Code Via a virtual Function 783
The Problem with Calling resize Repeatedly 791
Saving the Number of the Elements in the File 793
Handling the End-of-File Condition Properly 795
Creating a Data File Programmatically 797
Comparing Two streams 804
Inheriting the Implementation of a virtual Function 808
Adding the Ability to Edit a Record 814
The New Implementation of operator >> 818
Reducing Code Duplication 820
The enum Type 824
Automatic Conversion from enum 827
The Implementation of HomeItem::Read 828
The Implementation of HomeItemBasic::ReadInteractive 829
The Implementation of HomeItemBasic::ReadFromFile 833
The Implementation of HomeItemBasic::Edit 834
The Implementation of HomeItemBasic::FormattedDisplay 835
The Implementation of HomeItemBasic::EditField 836
The New Member Functions of HomeItemMusic 839
Keeping Track of the Next Field Number 840
Review 846
Exercises 853
Conclusion 854
CHAPTER 12 More on the Home Inventory Project 855
Objectives of This Chapter 857
Extending the Functionality of strings 857
How to Implement Our New string Functionality 858
Why Our New xstring class Is a Good Design Choice 860
The Interface of the New xstring class 862
The Include Guard 863
The Constructors for the xstring class 866
Automatic Conversion from the Standard string class 867
Preventing Accidental Initialization by 0 873
The explicit Keyword 875
Lessons of the xstring class Implementation 878
One More Design Decision Regarding the xstring class 879
Case-Insensitive Searching 880
Searching for an Item by a Substring 888
Putting It All Together 895
First Test Session: Change Requests and Problem Reports 897
Second Test Session: Change Requests and Problem Reports 900
Third Test Session: Change Requests and Problem Reports 902
Fourth Test Session: Change Requests and Problem Reports 903
Fifth Test Session: Change Requests and Problem Reports 905
How Software Development Really Works 906
Review 907
Conclusion 910
CHAPTER 13 Analyzing the Home Inventory Project 911
Objectives of This Chapter 912
The Final Version of the Home Inventory Program 913
Using a namespace to Group Utility Functions 925
The Advantages of Using namespaces 926
Default Arguments 927
The Functions of the HomeUtility namespace 930
Returning More Than One Value from a Function 931
Reading a Date Field 931
Reading Numeric Data from a Line Oriented File 936
A Tradeoff between Convenience and Error Control 937
Single Keystroke Data Entry 938
Echoing the Typed Character to the Screen 943
Getting the Rest of the Numeric Value 945
Clearing Part of the Screen 947
The Implementation of HomeUtility::SelectItem 949
Checking the Inventory 955
Sorting the Inventory 965
Selecting by Category 969
Finishing up the HomeItem class 974
Are We Having Fun Yet? 983
Review 983
Exercises 988
Conclusion 989
APPENDIX A Tying up Loose Ends 991
Operator Precedence 991
Another Native Data Type 992
Wrapping Up 993
APPENDIX B Glossary 995
Special Characters 995
A 999
B 1002
C 1004
D 1010
E 1013
F 1014
G 1017
H 1017
I 1018
K 1021
L 1021
M 1023
N 1025
O 1028
P 1030
R 1033
S 1036
T 1041
U 1042
V 1044
W 1045
Y 1045
Z 1045
About the Author 1047
Bibliography 1049
Index 1051

TOC PREV NEXT INDEX