Sams Teach Yourself C++ in One Hour a Day [6 ed.]
0672329417, 9780672329418
Sams Teach Yourself C++ in One Hour a Day ¿ In just one hour a day, you’ll have all the skills you need to begin program
298
18
45KB
English
Pages 888
[886]
Year 2008
Report DMCA / Copyright
DOWNLOAD PDF FILE
Table of contents :
Sams Teach Yourself C++ in One Hour a Day......Page 1
Table of Contents......Page 6
Organization of This Book......Page 28
Conventions Used in This Book......Page 29
Sample Code for This Book......Page 30
PART I: The Basics......Page 32
LESSON 1: Getting Started......Page 34
A Brief History of C++......Page 35
The ANSI Standard......Page 41
Preparing to Program......Page 42
Your Development Environment......Page 43
The Process of Creating the Program......Page 44
The Development Cycle......Page 45
HELLO.cpp—Your First C++ Program......Page 46
Getting Started with Your Compiler......Page 49
Compile Errors......Page 50
Q&A......Page 51
Workshop......Page 52
LESSON 2: The Anatomy of a C++ Program......Page 54
A Simple Program......Page 55
A Brief Look at cout......Page 57
Using the Standard Namespace......Page 59
Commenting Your Programs......Page 62
Functions......Page 64
Q&A......Page 68
Workshop......Page 69
LESSON 3: Using Variables, Declaring Constants......Page 70
What Is a Variable?......Page 71
Defining a Variable......Page 75
Determining Memory Consumed by a Variable Type......Page 78
Assigning Values to Your Variables......Page 80
Creating Aliases with typedef......Page 82
When to Use short and When to Use long......Page 83
Working with Characters......Page 86
Constants......Page 89
Enumerated Constants......Page 91
Q&A......Page 94
Workshop......Page 95
LESSON 4: Managing Arrays and Strings......Page 98
What Is an Array?......Page 99
Multidimensional Arrays......Page 107
char Arrays and Strings......Page 111
Using the strcpy() and strncpy() Methods......Page 114
String Classes......Page 116
Q&A......Page 118
Workshop......Page 119
LESSON 5: Working with Expressions, Statements, and Operators......Page 120
Starting with Statements......Page 121
Expressions......Page 122
Working with Operators......Page 124
Incrementing and Decrementing......Page 127
Understanding Operator Precedence......Page 130
Nesting Parentheses......Page 131
The Nature of Truth......Page 132
The if Statement......Page 134
Using Braces in Nested if Statements......Page 142
Using the Logical Operators......Page 145
Short Circuit Evaluation......Page 146
More About Truth and Falsehood......Page 147
The Conditional (Ternary) Operator......Page 148
Q&A......Page 150
Workshop......Page 151
LESSON 6: Organizing Code with Functions......Page 154
What Is a Function?......Page 155
Declaring and Defining Functions......Page 156
Execution of Functions......Page 160
Determining Variable Scope......Page 161
Parameters Are Local Variables......Page 164
More About Function Arguments......Page 168
More About Return Values......Page 169
Default Parameters......Page 172
Overloading Functions......Page 174
Special Topics About Functions......Page 178
How Functions Work—A Peek Under the Hood......Page 185
Q&A......Page 190
Workshop......Page 191
LESSON 7: Controlling Program Flow......Page 194
Programming Loops......Page 195
Using while Loops......Page 196
Implementing do...while Loops......Page 204
Using do...while......Page 205
Looping with the for Statement......Page 207
Summing Up Loops......Page 216
Controlling Flow with switch Statements......Page 219
Q&A......Page 226
Workshop......Page 227
LESSON 8: Pointers Explained......Page 230
What Is a Pointer?......Page 231
The Stack and the Free Store (Heap)......Page 243
Another Look at Memory Leaks......Page 248
Deleting Objects from the Free Store......Page 249
Stray, Wild, or Dangling Pointers......Page 251
Using const Pointers......Page 254
Q&A......Page 255
Workshop......Page 256
LESSON 9: Exploiting References......Page 258
What Is a Reference?......Page 259
Using the Address-Of Operator (&) on References......Page 260
Passing Function Arguments by Reference......Page 264
Returning Multiple Values......Page 269
Passing by Reference for Efficiency......Page 273
Knowing When to Use References Versus Pointers......Page 281
Mixing References and Pointers......Page 282
Returning Out-of-Scope Object References......Page 283
Summary......Page 286
Workshop......Page 287
PART II: Fundamentals of Object-Oriented Programming and C++......Page 290
LESSON 10: Classes and Objects......Page 292
Is C++ Object-Oriented?......Page 293
Creating New Types......Page 294
Introducing Classes and Members......Page 295
Accessing Class Members......Page 298
Private Versus Public Access......Page 299
Implementing Class Methods......Page 305
Adding Constructors and Destructors......Page 308
Including const Member Functions......Page 313
Where to Put Class Declarations and Method Definitions......Page 314
Inline Implementation......Page 315
Classes with Other Classes as Member Data......Page 318
Exploring Structures......Page 322
Summary......Page 323
Q&A......Page 324
Workshop......Page 325
LESSON 11: Implementing Inheritance......Page 328
What Is Inheritance?......Page 329
Private Versus Protected......Page 333
Inheritance with Constructors and Destructors......Page 336
Overriding Base Class Functions......Page 343
Virtual Methods......Page 349
Private Inheritance......Page 362
Summary......Page 365
Q&A......Page 366
Workshop......Page 367
LESSON 12: Polymorphism......Page 370
Problems with Single Inheritance......Page 371
Multiple Inheritance......Page 378
Abstract Data Types......Page 395
Q&A......Page 409
Workshop......Page 410
LESSON 13: Operator Types and Operator Overloading......Page 412
What Are Operators in C++?......Page 413
Unary Operators......Page 414
Binary Operators......Page 423
Function operator()......Page 438
Operators That Cannot Be Redefined......Page 439
Q&A......Page 440
Workshop......Page 441
LESSON 14: Casting Operators......Page 442
The Need for Casting......Page 443
The C++ Casting Operators......Page 444
Problems with the C++ Casting Operators......Page 450
Workshop......Page 452
LESSON 15: An Introduction to Macros and Templates......Page 454
The #define Preprocessor Directive......Page 455
Macro Functions......Page 456
An Introduction to Templates......Page 461
Q&A......Page 468
Workshop......Page 469
PART III: Learning the Standard Template Library (STL)......Page 472
LESSON 16: An Introduction to the Standard Template Library......Page 474
STL Containers......Page 475
STL Iterators......Page 478
STL Algorithms......Page 479
The Interaction Between Containers and Algorithms Using Iterators......Page 480
Q&A......Page 482
Workshop......Page 483
LESSON 17: The STL string Class......Page 484
The Need for String Manipulation Classes......Page 485
Working with the STL string Class......Page 486
Q&A......Page 498
Workshop......Page 499
LESSON 18: STL Dynamic Array Classes......Page 500
Typical Vector Operations......Page 501
Understanding size() and capacity()......Page 511
The STL deque Class......Page 513
Q&A......Page 515
Workshop......Page 516
LESSON 19: STL list......Page 518
Basic list Operations......Page 519
Reversing and Sorting Elements in a list......Page 527
Q&A......Page 538
Workshop......Page 539
LESSON 20: STL set and multiset......Page 540
Basic STL set and multiset Operations......Page 541
Summary......Page 556
Workshop......Page 557
LESSON 21: STL map and multimap......Page 560
Basic STL map and multimap Operations......Page 561
Supplying a Custom Sort Predicate......Page 570
Q&A......Page 574
Workshop......Page 575
PART IV: More STL......Page 578
LESSON 22: Understanding Function Objects......Page 580
Typical Applications of Function Objects......Page 581
Q&A......Page 593
Workshop......Page 594
LESSON 23: STL Algorithms......Page 596
Classification of STL Algorithms......Page 597
Usage of STL Algorithms......Page 600
Q&A......Page 625
Workshop......Page 626
LESSON 24: Adaptive Containers: stack and queue......Page 628
The Behavioral Characteristics of Stacks and Queues......Page 629
Using the STL stack Class......Page 630
Using the STL queue Class......Page 633
Using the STL Priority Queue......Page 637
Workshop......Page 642
LESSON 25: Working with Bit Flags Using STL......Page 644
The bitset Class......Page 645
Using std::bitset and Its Members......Page 646
The vector......Page 650
Q&A......Page 652
Workshop......Page 653
PART V: Advanced C++ Concepts......Page 654
LESSON 26: Understanding Smart Pointers......Page 656
What Are Smart Pointers?......Page 657
How Are Smart Pointers Implemented?......Page 658
Types of Smart Pointers......Page 659
Using the std::auto_ptr......Page 665
Summary......Page 667
Workshop......Page 668
LESSON 27: Working with Streams......Page 670
Overview of Streams......Page 671
Standard I/O Objects......Page 674
Redirection of the Standard Streams......Page 675
Input Using cin......Page 676
Other Member Functions of cin......Page 681
Outputting with cout......Page 690
Streams Versus the printf() Function......Page 698
File Input and Output......Page 702
Binary Versus Text Files......Page 707
Command-Line Processing......Page 709
Summary......Page 713
Workshop......Page 714
LESSON 28: Exception Handling......Page 716
Bugs, Errors, Mistakes, and Code Rot......Page 717
The Idea Behind Exceptions......Page 719
How Catching Exceptions Work......Page 729
Data in Exceptions and Naming Exception Objects......Page 736
Exceptions and Templates......Page 743
Exceptions Without Errors......Page 746
Bugs and Debugging......Page 748
Summary......Page 749
Q&A......Page 750
Workshop......Page 751
LESSON 29: Tapping Further into the Preprocessor......Page 754
The #define Preprocessor Directive......Page 755
Inclusion and Inclusion Guards......Page 758
Predefined Macros......Page 760
The assert() Macro......Page 761
Bit Twiddling......Page 771
Programming Style......Page 777
Next Steps in Your C++ Development......Page 783
Summary......Page 784
Q&A......Page 785
Workshop......Page 786
Appendixes......Page 788
APPENDIX A: Working with Numbers: Binary and Hexadecimal......Page 790
Using Other Bases......Page 791
Converting to Different Bases......Page 792
Hexadecimal......Page 795
APPENDIX B: C++ Keywords......Page 800
APPENDIX C: Operator Precedence......Page 802
APPENDIX D: Answers......Page 804
A......Page 856
B......Page 858
C......Page 859
D......Page 864
F......Page 866
H......Page 868
I......Page 869
L......Page 870
M......Page 871
O......Page 873
P......Page 875
R......Page 877
S......Page 878
T......Page 881
V......Page 883
W......Page 884