Practical C++ Programming

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

C++ is a powerful, highly flexible, and adaptable programming language that allows software engineers to organize and process information quickly and effectively. But this high-level language is relatively difficult to master, even if you already know the C programming language. The 2nd edition of Practical C++ Programming is a complete introduction to the C++ language for programmers who are learning C++. Reflecting the latest changes to the C++ standard, this 2nd edition takes a useful down-to-earth approach, placing a strong emphasis on how to design clean, elegant code. In short, to-the-point chapters, all aspects of programming are covered including style, software engineering, programming design, object-oriented design, and debugging. It also covers common mistakes and how to find (and avoid) them. End of chapter exercises help you ensure you've mastered the material. Practical C++ Programming thoroughly covers: C++ Syntax Coding standards and style Creation and use of object classes Templates Debugging and optimization Use of the C++ preprocessor File input/output Steve Oualline's clear, easy-going writing style and hands-on approach to learning make Practical C++ Programming a nearly painless way to master this complex but powerful programming language.

Author(s): Steve Oualline
Edition: 2nd
Year: 2009

Language: English
Pages: 576

Main Page......Page 1
Table of content......Page 2
Copyright......Page 11
Preface......Page 943
Scope of This Handbook......Page 945
How This Book Is Organized......Page 16
How to Read This Book If You Already Know C......Page 20
Font Conventions......Page 21
How to Contact Us......Page 953
Acknowledgments for the First Edition......Page 954
Acknowledgments for the Second Edition......Page 955
Part I: The Basics......Page 26
Chapter 1. What Is C++?......Page 958
1.1 A Brief History of C++......Page 30
1.2 C++ Organization......Page 31
1.3 How to Learn C++......Page 964
Chapter 2. The Basics of Program Writing......Page 966
2.1 Programs from Conception to Execution......Page 970
2.2 Creating a Real Program......Page 42
2.3 Getting Help in Unix......Page 54
2.4 Getting Help in an IDE......Page 55
2.5 Programming Exercises......Page 56
Chapter 3. Style......Page 57
3.1 Comments......Page 989
3.2 C++ Code......Page 996
3.3 Naming Style......Page 999
3.4 Coding Religion......Page 1000
3.5 Indentation and Code Format......Page 71
3.6 Clarity......Page 74
3.7 Simplicity......Page 1005
3.8 Consistency and Organization......Page 76
3.9 Further Reading......Page 1007
3.10 Summary......Page 1008
Chapter 4. Basic Declarations and Expressions......Page 79
4.1 Basic Program Structure......Page 81
4.2 Simple Expressions......Page 83
4.3 The std::cout Output Object......Page 1015
4.4 Variables and Storage......Page 1017
4.5 Variable Declarations......Page 89
4.6 Integers......Page 1020
4.7 Assignment Statements......Page 1021
4.8 Floating-Point Numbers......Page 93
4.9 Floating-Point Divide Versus Integer Divide......Page 1024
4.10 Characters......Page 1026
4.11 Wide Characters......Page 98
4.12 Boolean Type......Page 1029
4.13 Programming Exercises......Page 101
4.14 Answers to Chapter Questions......Page 1032
Chapter 5. Arrays, Qualifiers, and Reading Numbers......Page 1033
5.1 Arrays......Page 1035
5.2 Strings......Page 107
5.3 Reading Data......Page 1040
5.4 Initializing Variables......Page 113
5.5 Multidimensional Arrays......Page 119
5.6 C-Style Strings......Page 1052
5.7 Types of Integers......Page 131
5.8 Types of Floats......Page 1064
5.9 Constant and Reference Declarations......Page 135
5.10 Qualifiers......Page 1067
5.11 Hexadecimal and Octal Constants......Page 1070
5.12 Operators for Performing Shortcuts......Page 142
5.13 Side Effects......Page 1074
5.14 Programming Exercises......Page 147
5.15 Answers to Chapter Questions......Page 148
Chapter 6. Decision and Control Statements......Page 1080
6.1 if Statement......Page 152
6.2 else Statement......Page 1084
6.3 How Not to Use std::strcmp......Page 156
6.4 Looping Statements......Page 157
6.5 while Statement......Page 158
6.6 break Statement......Page 161
6.7 continue Statement......Page 1093
6.8 The Assignment Anywhere Side Effect......Page 1095
6.9 Programming Exercises......Page 1097
6.10 Answers to Chapter Questions......Page 1099
Chapter 7. The Programming Process......Page 1101
7.1 Setting Up Your Work Area......Page 1106
7.2 The Specification......Page 177
7.3 Code Design......Page 180
7.4 The Prototype......Page 181
7.5 The Makefile......Page 183
7.6 Testing......Page 1117
7.7 Debugging......Page 188
7.8 Maintenance......Page 191
7.9 Revisions......Page 192
7.10 Electronic Archaeology......Page 1123
7.11 Mark Up the Program......Page 1125
7.12 Use the Debugger......Page 196
7.13 Use the Text Editor as a Browser......Page 1127
7.14 Add Comments......Page 1128
7.15 Programming Exercises......Page 201
Part II: Simple Programming......Page 1132
Chapter 8. More Control Statements......Page 204
8.1 for Statement......Page 206
8.2 switch Statement......Page 211
8.3 switch, break, and continue......Page 218
8.4 Programming Exercises......Page 220
8.5 Answers to Chapter Questions......Page 222
Chapter 9. Variable Scope and Functions......Page 223
9.1 Scope and Storage Class......Page 225
9.2 Namespaces......Page 229
9.3 Functions......Page 233
9.4 Summary of Parameter Types......Page 1182
9.5 Recursion......Page 253
9.6 Structured Programming Basics......Page 256
9.7 Real-World Programming......Page 258
9.8 Programming Exercises......Page 260
9.9 Answers to Chapter Questions......Page 1191
Chapter 10. The C++ Preprocessor......Page 1192
10.1 #define Statement......Page 1194
10.2 Conditional Compilation......Page 1201
10.3 #include Files......Page 274
10.4 Parameterized Macros......Page 1206
10.5 Advanced Features......Page 280
10.6 Summary......Page 1211
10.7 Programming Exercises......Page 1212
10.8 Answers to Chapter Questions......Page 283
Chapter 11. Bit Operations......Page 286
11.1 Bit Operators......Page 1219
11.2 The AND Operator (&)......Page 290
11.3 Bitwise OR (|)......Page 1223
11.4 The Bitwise Exclusive OR (^)......Page 294
11.5 The Ones Complement Operator (NOT) (~)......Page 1225
11.6 The Left and Right Shift Operators (<<, >>)......Page 1226
11.7 Setting, Clearing, and Testing Bits......Page 298
11.8 Bitmapped Graphics......Page 1233
11.9 Programming Exercises......Page 310
11.10 Answers to Chapter Questions......Page 1241
Part III: Advanced Types and Classes......Page 312
Chapter 12. Advanced Types......Page 314
12.1 Structures......Page 1246
12.2 Unions......Page 1250
12.3 typedef......Page 1254
12.4 enum Type......Page 326
12.5 Bit Members or Packed Structures......Page 328
12.6 Arrays of Structures......Page 331
12.7 Programming Exercises......Page 1263
12.8 Answers to Chapter Questions......Page 334
Chapter 13. Simple Classes......Page 336
13.1 Stacks......Page 338
13.2 Improved Stack......Page 344
13.3 Using a Class......Page 347
13.4 Introduction to Constructors and Destructors......Page 351
13.5 Automatically Generated Member Functions......Page 357
13.6 Shortcuts......Page 360
13.7 Style......Page 361
13.8 Structures Versus Classes......Page 365
13.9 Programming Exercises......Page 1296
Chapter 14. More on Classes......Page 1299
14.1 Friends......Page 371
14.2 Constant Functions......Page 1304
14.3 Constant Members......Page 1307
14.4 Static Member Variables......Page 1309
14.5 Static Member Functions......Page 382
14.6 The Meaning of static......Page 1314
14.7 Programming Exercises......Page 1315
Chapter 15. Simple Pointers......Page 387
15.1 const Pointers......Page 395
15.2 Pointers and Printing......Page 1327
15.3 Pointers and Arrays......Page 398
15.4 The reinterpret_cast......Page 1338
15.5 Pointers and Structures......Page 1339
15.6 Command-Line Arguments......Page 411
15.7 Programming Exercises......Page 417
15.8 Answers to Chapter Questions......Page 418
Part IV: Advanced Programming Concepts......Page 422
Chapter 16. File Input/Output......Page 1354
16.1 C++ File I/O......Page 426
16.2 Conversion Routines......Page 433
16.3 Binary and ASCII Files......Page 1369
16.4 The End-of-Line Puzzle......Page 1371
16.5 Binary I/O......Page 1374
16.6 Buffering Problems......Page 446
16.7 Unbuffered I/O......Page 448
16.8 Designing File Formats......Page 454
16.9 C-Style I/O Routines......Page 457
16.10 C-Style Conversion Routines......Page 461
16.11 C-Style Binary I/O......Page 1396
16.12 C- Versus C++- Style I/O......Page 469
16.13 Programming Exercises......Page 1403
16.14 Answers to Chapter Questions......Page 474
Chapter 17. Debugging and Optimization......Page 476
17.1 Code Reviews......Page 478
17.2 Serial Debugging......Page 1412
17.3 Going Through the Output......Page 1416
17.4 Interactive Debuggers......Page 487
17.5 Debugging a Binary Search......Page 1423
17.6 Interactive Debugging Tips and Tricks......Page 509
17.7 Runtime Errors......Page 1441
17.8 Optimization......Page 1444
17.9 How to Optimize......Page 1452
17.10 Case Study: Inline Functions Versus Normal Functions......Page 1455
17.11 Case Study: Optimizing a Color-Rendering Algorithm......Page 1456
17.12 Programming Exercises......Page 527
17.13 Answers to Chapter Questions......Page 528
Chapter 18. Operator Overloading......Page 529
18.1 Creating a Simple Fixed-Point Class......Page 1461
18.2 Operator Functions......Page 1468
18.3 Operator Member Functions......Page 1482
18.4 Warts......Page 1484
18.5 Full Definition of the Fixed-Point Class......Page 1485
18.6 Programming Exercises......Page 568
18.7 Answers to Chapter Questions......Page 1499
Chapter 19. Floating Point......Page 570
19.1 Floating-Point Format......Page 1502
19.2 Floating Addition/Subtraction......Page 574
19.3 Multiplication and Division......Page 576
19.4 Overflow and Underflow......Page 1508
19.5 Roundoff Error......Page 579
19.6 Accuracy......Page 1510
19.7 Minimizing Roundoff Error......Page 581
19.8 Determining Accuracy......Page 1512
19.9 Precision and Speed......Page 1514
19.10 Power Series......Page 1516
19.11 Programming Exercises......Page 589
Chapter 20. Advanced Pointers......Page 590
20.1 Pointers, Structures, and Classes......Page 593
20.2 delete Operator......Page 596
20.3 Linked Lists......Page 1528
20.4 Ordered Linked Lists......Page 603
20.5 Doubly Linked Lists......Page 606
20.6 Trees......Page 611
20.7 Printing a Tree......Page 615
20.8 The Rest of the Program......Page 1546
20.9 Data Structures for a Chess Program......Page 1551
20.10 Programming Exercises......Page 623
20.11 Answers to Chapter Questions......Page 624
Chapter 21. Advanced Classes......Page 626
21.1 Derived Classes......Page 1558
21.2 Virtual Functions......Page 637
21.3 Virtual Classes......Page 1575
21.4 Function Hiding in Derived Classes......Page 648
21.5 Constructors and Destructors in Derived Classes......Page 649
21.6 The dynamic_cast Operator......Page 653
21.7 Summary......Page 1584
21.8 Programming Exercises......Page 1585
21.9 Answers to Chapter Questions......Page 656
Part V: Other Language Features......Page 1587
Chapter 22. Exceptions......Page 1589
22.1 Adding Exceptions to the Stack Class......Page 1593
22.2 Exceptions Versus assert......Page 1601
22.3 Programming Exercises......Page 1602
Chapter 23. Modular Programming......Page 673
23.1 Modules......Page 1605
23.2 Public and Private......Page 676
23.3 The extern Storage Class......Page 1607
23.4 Headers......Page 681
23.5 The Body of the Module......Page 1613
23.6 A Program to Use Infinite Arrays......Page 684
23.7 The Makefile for Multiple Files......Page 1617
23.8 Using the Infinite Array......Page 693
23.9 Dividing a Task into Modules......Page 1630
23.10 Module Design Guidelines......Page 701
23.11 Programming Exercises......Page 1632
Chapter 24. Templates......Page 704
24.1 What Is a Template?......Page 1636
24.2 Templates: The Hard Way......Page 1637
24.3 Templates: The C++ Way......Page 709
24.4 Function Specialization......Page 711
24.5 Class Templates......Page 1643
24.6 Class Specialization......Page 716
24.7 Implementation Details......Page 717
24.8 Advanced Features......Page 1651
24.9 Summary......Page 1654
24.10 Programming Exercises......Page 1655
Chapter 25. Standard Template Library......Page 726
25.1 STL Basics......Page 728
25.2 Class List—A Set of Students......Page 1661
25.3 Creating a Waiting List with the STL List......Page 1664
25.4 Storing Grades in a STL Map......Page 1665
25.5 Putting It All Together......Page 1667
25.6 Practical Considerations When Using the STL......Page 1678
25.7 Getting More Information......Page 1680
25.8 Exercises......Page 1681
Chapter 26. Program Design......Page 752
26.1 Design Goals......Page 754
26.2 Design Factors......Page 1685
26.3 Design Principles......Page 1687
26.4 Coding......Page 1689
26.5 Objects......Page 768
26.6 Real-World Design Techniques......Page 1702
26.7 Conclusion......Page 1711
Chapter 27. Putting It All Together......Page 782
27.1 Requirements......Page 784
27.2 Code Design......Page 1717
27.3 Coding......Page 1720
27.4 Functional Description......Page 791
27.5 Testing......Page 1727
27.6 Revisions......Page 798
27.7 A Final Warning......Page 1729
27.8 Program Files......Page 1730
27.9 Programming Exercises......Page 1756
Chapter 28. From C to C++......Page 827
28.1 K&R-Style Functions......Page 829
28.2 struct......Page 831
28.3 malloc and free......Page 1762
28.4 Turning Structures into Classes......Page 1765
28.5 setjmp and longjmp......Page 837
28.6 Mixing C and C++ Code......Page 839
28.7 Summary......Page 840
28.8 Programming Exercise......Page 1771
Chapter 29. C++'s Dustier Corners......Page 842
29.1 do/while......Page 1774
29.2 goto......Page 1775
29.3 The ?: Construct......Page 1778
29.4 The Comma Operator......Page 1779
29.5 Overloading the ( ) Operator......Page 1780
29.6 Pointers to Members......Page 1781
29.7 The asm Statement......Page 1783
29.8 The mutable Qualifier......Page 1784
29.9 Run Time Type Identification......Page 1785
29.10 Trigraphs......Page 856
29.11 Answers to Chapter Questions......Page 857
Chapter 30. Programming Adages......Page 858
30.1 General......Page 1790
30.2 Design......Page 861
30.3 Declarations......Page 1792
30.4 switch Statement......Page 1793
30.5 Preprocessor......Page 864
30.6 Style......Page 1795
30.7 Compiling......Page 866
30.8 The Ten Commandments for C++ Programmers......Page 1797
30.9 Final Note......Page 1799
30.10 Answers to Chapter Questions......Page 870
Part VI: Appendixes......Page 871
Appendix A. ASCII Table......Page 1803
Appendix B. Ranges......Page 1811
Appendix C. Operator Precedence Rules......Page 1814
C.1 Standard Rules......Page 886
C.2 Practical Subset of the Operator Precedence Rules......Page 1818
Appendix D. Computing Sine Using a Power Series......Page 1819
Appendix E. Resources......Page 1827
E.1 Compilers......Page 1829
E.2 Standard Template Library......Page 900
E.3 Standards......Page 901
E.4 Programming Tools......Page 902
Colophon......Page 903
Index......Page 905
Index SYMBOL......Page 906
Index A......Page 908
Index B......Page 909
Index C......Page 910
Index D......Page 913
Index E......Page 915
Index F......Page 916
Index G......Page 918
Index H......Page 919
Index I......Page 920
Index J......Page 922
Index K......Page 923
Index L......Page 924
Index M......Page 925
Index N......Page 927
Index O......Page 928
Index P......Page 930
Index Q......Page 932
Index R......Page 933
Index S......Page 934
Index T......Page 937
Index U......Page 939
Index V......Page 940
Index W......Page 941
Index X......Page 942