Practical C++ Programming

Practical C++ Programming

by Steve Oualline

Paperback(Second Edition)

$44.99 $49.99 Save 10% Current price is $44.99, Original price is $49.99. You Save 10%.
View All Available Formats & Editions
Choose Expedited Shipping at checkout for guaranteed delivery by Friday, March 29

Product Details

ISBN-13: 9780596004194
Publisher: O'Reilly Media, Incorporated
Publication date: 01/15/2003
Edition description: Second Edition
Pages: 576
Sales rank: 1,262,291
Product dimensions: 7.00(w) x 9.19(h) x 1.30(d)

About the Author

Steve Oualline lives in Southern California, where he works as a software engineer for a major phone company. In his free time he is a real engineer on the Poway Midland Railroad. Steve has written almost a dozen books on programming and Linux software. His web site is .

Read an Excerpt

Chapter 7: The Programming Process


Good programmers put their programs through a long and rigorous testing process before releasing it to the outside world. Then the first user tries the program and almost immediately finds a bug. This starts the maintenance phase. Bugs are fixed, the program is tested (to make sure the fixes didn't break anything), and the program is released again.


Although the program is officially finished, you are not finished with it. After it is in use for a few months, someone will come to us and ask, "Can you add a modulus operator?" So you revise the specifications, add the change to the program, update the test plan, test the program, and release it again.

As time passes, more people will come to you with additional requests for changes. Soon the program has trig functions, linear regressions, statistics, binary arithmetic, and financial calculations. The design is based on the idea of one-character operators. Soon you find yourself running out of characters to use. At this point the program is doing work far beyond what it was initially designed to do. Sooner or later you reach the point where the program needs to be scrapped and a new one written from scratch. At this point you write a new Preliminary Specification and start the process over again.

Electronic Archaeology

Unfortunately, most programmers don't start a project at the design step. Instead they are immediately thrust into the maintenance or revision stage. This means the programmer is faced with the worst possible job: understanding and modifying someone else's code.

Contrary to popular belief, most C++ programs are not written by disorganized orangutans using Zen programming techniques and poorly commented in Esperanto. They just look that way. Electronic archeology is the art of digging through old code to discover amazing things (like how and why the code works).

Your computer can aid greatly in your search to discover the true meaning of someone else's code. Many tools are available for examining and formatting code. (Be careful with your selection of tools, however. Many C tools have yet to be upgraded for C++. See earlier sections on revisions.) Some of these tools include:

Cross-references. These programs have names like xref, cxref, and cross. System V UNIX has the utility cscope. They print out a list of variables and where the variables are used.

Program indenters. Programs such as cb and indent indent a program "correctly" (correct indentation is something defined by the tool maker).

Pretty printers. A pretty printer such as vgrind or cprint typesets source code for printing on a laser printer.

Call graphs. On System V UNIX the program cflow analyzes the structure of the program. On other systems there is a public domain utility, calls, that produces call graphs, showing who calls whom and who is called by whom.

Class browsers. A class browser allows you to display the class hierarchy so you can tell what components went into building the class as well as its structure. You'll learn what a class is in Chapter 13, Simple Classes.

Which tools should you use? Whichever ones work for you. Different programmers work in different ways. Some techniques for examining code are listed below. Choose the ones that work for you and use them.

Mark Up the Program

Take a printout of the program and make notes all over it. Use red or blue ink so you can tell the difference between the printout and the notes. Use a highlighter to emphasize important sections. These notes are useful; put them in the program as comments, and then make a new printout and start the process over again.

Use the Debugger The debugger is a great tool for understanding how something works. Most debuggers allow you to step through the program one line at a time, examining variables and discovering how things really work. Once you find out what the code does, make notes and put them in as comments.

Use the Text Editor as a Browser

One of the best tools for going through someone else's code is your text editor. Suppose you want to find out what the variable sc is used for. Use the search command to find the first place sc is used. Search again and find the second. Continue searching until you know what the variable does.

Suppose you find out that sc is used as a sequence counter. Since you're already in the editor, you can easily do a global search-and-replace to change the variable sc to sequence_counter. (Disaster warning: Make sure sequence_counter is not already defined as a variable before you make the change. Also make sure you do a word replacement or you'll find you replaced sc in places you didn't intend.) Comment the declaration and you're on your way to creating an understandable program.

Add Comments

Don't be afraid to put any information you have, no matter how little, into the comments. Some of the comments I've used include:

int state; // Controls some sort of state machine

int rmxy; // Something to do with color correction?

Finally, there is a catch-all comment:

int idn; // ???

which means, "I have no idea what this variable does." Even though the purpose is unknown, it is now marked as something that needs more work.

As you go through someone else's code adding comments and improving style, the structure will become clearer to you. By inserting notes (comments), you make the code better and easier to understand for future programmers.

Suppose you are confronted with the following program written by someone from the "The Terser the Better" school of programming. Your assignment is to figure out what this program does....

Table of Contents

Scope of This Handbook;
How This Book Is Organized;
How to Read This Book If You Already Know C;
Font Conventions;
How to Contact Us;
Acknowledgments for the First Edition;
Acknowledgments for the Second Edition;
Part I: The Basics;
Chapter 1: What Is C++?;
1.1 A Brief History of C++;
1.2 C++ Organization;
1.3 How to Learn C++;
Chapter 2: The Basics of Program Writing;
2.1 Programs from Conception to Execution;
2.2 Creating a Real Program;
2.3 Getting Help in Unix;
2.4 Getting Help in an IDE;
2.5 Programming Exercises;
Chapter 3: Style;
3.2 C++ Code;
3.3 Naming Style;
3.4 Coding Religion;
3.5 Indentation and Code Format;
3.6 Clarity;
3.7 Simplicity;
3.8 Consistency and Organization;
3.9 Further Reading;
3.10 Summary;
Chapter 4: Basic Declarations and Expressions;
4.1 Basic Program Structure;
4.2 Simple Expressions;
4.3 The std::cout Output Object;
4.4 Variables and Storage;
4.5 Variable Declarations;
4.6 Integers;
4.7 Assignment Statements;
4.8 Floating-Point Numbers;
4.9 Floating-Point Divide Versus Integer Divide;
4.10 Characters;
4.11 Wide Characters;
4.12 Boolean Type;
4.13 Programming Exercises;
4.14 Answers to Chapter Questions;
Chapter 5: Arrays, Qualifiers, and Reading Numbers;
5.1 Arrays;
5.2 Strings;
5.3 Reading Data;
5.4 Initializing Variables;
5.5 Multidimensional Arrays;
5.6 C-Style Strings;
5.7 Types of Integers;
5.8 Types of Floats;
5.9 Constant and Reference Declarations;
5.10 Qualifiers;
5.11 Hexadecimal and Octal Constants;
5.12 Operators for Performing Shortcuts;
5.13 Side Effects;
5.14 Programming Exercises;
5.15 Answers to Chapter Questions;
Chapter 6: Decision and Control Statements;
6.1 if Statement;
6.2 else Statement;
6.3 How Not to Use std::strcmp;
6.4 Looping Statements;
6.5 while Statement;
6.6 break Statement;
6.7 continue Statement;
6.8 The Assignment Anywhere Side Effect;
6.9 Programming Exercises;
6.10 Answers to Chapter Questions;
Chapter 7: The Programming Process;
7.1 Setting Up Your Work Area;
7.2 The Specification;
7.3 Code Design;
7.4 The Prototype;
7.5 The Makefile;
7.6 Testing;
7.7 Debugging;
7.8 Maintenance;
7.9 Revisions;
7.10 Electronic Archaeology;
7.11 Mark Up the Program;
7.12 Use the Debugger;
7.13 Use the Text Editor as a Browser;
7.14 Add Comments;
7.15 Programming Exercises;
Part II: Simple Programming;
Chapter 8: More Control Statements;
8.1 for Statement;
8.2 switch Statement;
8.3 switch, break, and continue;
8.4 Programming Exercises;
8.5 Answers to Chapter Questions;
Chapter 9: Variable Scope and Functions;
9.1 Scope and Storage Class;
9.2 Namespaces;
9.3 Functions;
9.4 Summary of Parameter Types;
9.5 Recursion;
9.6 Structured Programming Basics;
9.7 Real-World Programming;
9.8 Programming Exercises;
9.9 Answers to Chapter Questions;
Chapter 10: The C++ Preprocessor;
10.1 #define Statement;
10.2 Conditional Compilation;
10.3 #include Files;
10.4 Parameterized Macros;
10.5 Advanced Features;
10.6 Summary;
10.7 Programming Exercises;
10.8 Answers to Chapter Questions;
Chapter 11: Bit Operations;
11.1 Bit Operators;
11.2 The AND Operator (&);
11.3 Bitwise OR (|);
11.4 The Bitwise Exclusive OR (^);
11.5 The Ones Complement Operator (NOT) (~);
11.6 The Left and Right Shift Operators (<<, >>);
11.7 Setting, Clearing, and Testing Bits;
11.8 Bitmapped Graphics;
11.9 Programming Exercises;
11.10 Answers to Chapter Questions;
Part III: Advanced Types and Classes;
Chapter 12: Advanced Types;
12.1 Structures;
12.2 Unions;
12.3 typedef;
12.4 enum Type;
12.5 Bit Members or Packed Structures;
12.6 Arrays of Structures;
12.7 Programming Exercises;
12.8 Answers to Chapter Questions;
Chapter 13: Simple Classes;
13.1 Stacks;
13.2 Improved Stack;
13.3 Using a Class;
13.4 Introduction to Constructors and Destructors;
13.5 Automatically Generated Member Functions;
13.6 Shortcuts;
13.7 Style;
13.8 Structures Versus Classes;
13.9 Programming Exercises;
Chapter 14: More on Classes;
14.1 Friends;
14.2 Constant Functions;
14.3 Constant Members;
14.4 Static Member Variables;
14.5 Static Member Functions;
14.6 The Meaning of static;
14.7 Programming Exercises;
Chapter 15: Simple Pointers;
15.1 const Pointers;
15.2 Pointers and Printing;
15.3 Pointers and Arrays;
15.4 The reinterpret_cast;
15.5 Pointers and Structures;
15.6 Command-Line Arguments;
15.7 Programming Exercises;
15.8 Answers to Chapter Questions;
Part IV: Advanced Programming Concepts;
Chapter 16: File Input/Output;
16.1 C++ File I/O;
16.2 Conversion Routines;
16.3 Binary and ASCII Files;
16.4 The End-of-Line Puzzle;
16.5 Binary I/O;
16.6 Buffering Problems;
16.7 Unbuffered I/O;
16.8 Designing File Formats;
16.9 C-Style I/O Routines;
16.10 C-Style Conversion Routines;
16.11 C-Style Binary I/O;
16.12 C- Versus C++- Style I/O;
16.13 Programming Exercises;
16.14 Answers to Chapter Questions;
Chapter 17: Debugging and Optimization;
17.1 Code Reviews;
17.2 Serial Debugging;
17.3 Going Through the Output;
17.4 Interactive Debuggers;
17.5 Debugging a Binary Search;
17.6 Interactive Debugging Tips and Tricks;
17.7 Runtime Errors;
17.8 Optimization;
17.9 How to Optimize;
17.10 Case Study: Inline Functions Versus Normal Functions;
17.11 Case Study: Optimizing a Color-Rendering Algorithm;
17.12 Programming Exercises;
17.13 Answers to Chapter Questions;
Chapter 18: Operator Overloading;
18.1 Creating a Simple Fixed-Point Class;
18.2 Operator Functions;
18.3 Operator Member Functions;
18.4 Warts;
18.5 Full Definition of the Fixed-Point Class;
18.6 Programming Exercises;
18.7 Answers to Chapter Questions;
Chapter 19: Floating Point;
19.1 Floating-Point Format;
19.2 Floating Addition/Subtraction;
19.3 Multiplication and Division;
19.4 Overflow and Underflow;
19.5 Roundoff Error;
19.6 Accuracy;
19.7 Minimizing Roundoff Error;
19.8 Determining Accuracy;
19.9 Precision and Speed;
19.10 Power Series;
19.11 Programming Exercises;
Chapter 20: Advanced Pointers;
20.1 Pointers, Structures, and Classes;
20.2 delete Operator;
20.3 Linked Lists;
20.4 Ordered Linked Lists;
20.5 Doubly Linked Lists;
20.6 Trees;
20.7 Printing a Tree;
20.8 The Rest of the Program;
20.9 Data Structures for a Chess Program;
20.10 Programming Exercises;
20.11 Answers to Chapter Questions;
Chapter 21: Advanced Classes;
21.1 Derived Classes;
21.2 Virtual Functions;
21.3 Virtual Classes;
21.4 Function Hiding in Derived Classes;
21.5 Constructors and Destructors in Derived Classes;
21.6 The dynamic_cast Operator;
21.7 Summary;
21.8 Programming Exercises;
21.9 Answers to Chapter Questions;
Part V: Other Language Features;
Chapter 22: Exceptions;
22.1 Adding Exceptions to the Stack Class;
22.2 Exceptions Versus assert;
22.3 Programming Exercises;
Chapter 23: Modular Programming;
23.1 Modules;
23.2 Public and Private;
23.3 The extern Storage Class;
23.4 Headers;
23.5 The Body of the Module;
23.6 A Program to Use Infinite Arrays;
23.7 The Makefile for Multiple Files;
23.8 Using the Infinite Array;
23.9 Dividing a Task into Modules;
23.10 Module Design Guidelines;
23.11 Programming Exercises;
Chapter 24: Templates;
24.1 What Is a Template?;
24.2 Templates: The Hard Way;
24.3 Templates: The C++ Way;
24.4 Function Specialization;
24.5 Class Templates;
24.6 Class Specialization;
24.7 Implementation Details;
24.8 Advanced Features;
24.9 Summary;
24.10 Programming Exercises;
Chapter 25: Standard Template Library;
25.1 STL Basics;
25.2 Class List—A Set of Students;
25.3 Creating a Waiting List with the STL List;
25.4 Storing Grades in a STL Map;
25.5 Putting It All Together;
25.6 Practical Considerations When Using the STL;
25.7 Getting More Information;
25.8 Exercises;
Chapter 26: Program Design;
26.1 Design Goals;
26.2 Design Factors;
26.3 Design Principles;
26.4 Coding;
26.5 Objects;
26.6 Real-World Design Techniques;
26.7 Conclusion;
Chapter 27: Putting It All Together;
27.1 Requirements;
27.2 Code Design;
27.3 Coding;
27.4 Functional Description;
27.5 Testing;
27.6 Revisions;
27.7 A Final Warning;
27.8 Program Files;
27.9 Programming Exercises;
Chapter 28: From C to C++;
28.1 K&R-Style Functions;
28.2 struct;
28.3 malloc and free;
28.4 Turning Structures into Classes;
28.5 setjmp and longjmp;
28.6 Mixing C and C++ Code;
28.7 Summary;
28.8 Programming Exercise;
Chapter 29: C++'s Dustier Corners;
29.1 do/while;
29.2 goto;
29.3 The : Construct;
29.4 The Comma Operator;
29.5 Overloading the ( ) Operator;
29.6 Pointers to Members;
29.7 The asm Statement;
29.8 The mutable Qualifier;
29.9 Run Time Type Identification;
29.10 Trigraphs;
29.11 Answers to Chapter Questions;
Chapter 30: Programming Adages;
30.1 General;
30.2 Design;
30.3 Declarations;
30.4 switch Statement;
30.5 Preprocessor;
30.6 Style;
30.7 Compiling;
30.8 The Ten Commandments for C++ Programmers;
30.9 Final Note;
30.10 Answers to Chapter Questions;
Part VI: Appendixes;
Appendix A: ASCII Table;
Appendix B: Ranges;
Appendix C: Operator Precedence Rules;
C.1 Standard Rules;
C.2 Practical Subset of the Operator Precedence Rules;
Appendix D: Computing Sine Using a Power Series;
Appendix E: Resources;
E.1 Compilers;
E.2 Standard Template Library;
E.3 Standards;
E.4 Programming Tools;

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews

Practical C++ Programming 2 out of 5 based on 0 ratings. 4 reviews.
Anonymous More than 1 year ago
I bought this book based on the quality of O'Reilly's Learning Python... In comparison this book is thoroughly awful. The author is overly verbose and yet somehow still manages to not really teach the reader anything at all. I don't have a recommended substitute but do not buy this book to learn c++.
Anonymous More than 1 year ago
Guest More than 1 year ago
The aside from the above reveiw being completely true, the book is now (as of Sept 2002) completely and absolutely outdated. I was very disappointed with this book. The C++ language was not covered in any depth whatsoever. If not for it's outdatedness, it might be usefull for a student with absolutely no prior programming experience. I am surprised that O'Reilly still publishes this book due to the fact that the C++ technologies of today: STL, the 'bool' keyword, and more were standardized after this books writing. The book does not even mention them. To anyone wanting to learn modern C++ I highly recommend "The C++ Programming Language by Bjarne Stroustrup". Stroustrup is the creator of C++. I recommend strongly against purchasing this book and hope that O'Reilly fixes this blemish on their otherwise perfect reputation.
Guest More than 1 year ago
Although I have numerous other ORA books and enjoy them thoroughly, I must say that this book was not at all helpful. I feel that the primary problem with the text is that it did not give a good explanation about how to program in C++. Yes, it does explain how various features of the language work, but not how to implement them. The examples given were limited in scope and little explanation was given about how object oriented programming is really done. I would recommend to anyone planning to learn how to write C++ code and how to work with objects to read 'Thinking in C++' by Bruce Eckel rather than this book. I would also encourage the reader to visit Mr. Eckel¿s web site for more information about C++ programming.