Preface

This book is devoted to practical C++ programming. It teaches you not only the mechanics of the language, but also style and debugging. The entire life cycle of a program is discussed, including conception, design, writing, debugging, release, documentation, maintenance, and revision.

Style is emphasized. Creating a good program involves more than just typing code. It is an art in which writing and programming skills blend to form a masterpiece. A well-written program not only functions correctly, but also is simple and easy to understand. Comments allow programmers to include descriptive text in their programs. Clearly written, well-commented programs are highly prized.

A program should be as simple as possible. Avoid the use of clever tricks. Cleverness and complexity can kill programs. This book stresses simple, practical rules. For example, the 15 operator-precedence rules in C++ can be simplified to 2:

  1. Multiply and divide before you add and subtract.

  2. Put parentheses around everything else.

Consider two programs. One was written by a clever programmer, using all the tricks. The program contains no comments, but it works. The other is nicely commented and well structured, but doesn’t work. Which program is more useful? In the long run, the “broken” one is more useful because it can be fixed and maintained easily. Although the clever one works now, sooner or later it will have to be modified. The hardest work you will ever have to do is modifying a cleverly written program.

Scope of This Handbook

This handbook is written for people with no previous programming experience, for programmers who know C and want to upgrade their skills to C++, and for those who already know C++ and want to improve their programming style and reliability. You should have access to a computer and know how to use the basic functions such as the text editor and file system.

Computer languages are best learned by writing and debugging programs. Sweating over a broken program at two o’clock in the morning only to find that you typed = where you should have typed == is a very effective teaching tool. This book contains many examples of common programming errors. (They are labeled as broken programs in the text.) You are encouraged to enter these programs into your computer and then run and debug them. This process introduces you to common errors using short programs so you will know how to spot and correct such errors in your own larger programs. (Instructions for obtaining copies of the programs presented in this book are located at the end of this preface.)

Several dialects of C++ are presented:

  • A “generic” Unix compiler that should work on most Unix systems

  • The GNU C++ compiler, named g++ (available for most Unix systems[1])

  • Borland C++ for MS-DOS/Windows

  • Microsoft’s Visual C++ for MS-DOS/Windows

As far as standard C++ is concerned, there are only minor differences among the various compilers. This book clearly indicates where compiler differences can affect the programmer. Specific instructions are given for producing and running programs using each of these compilers. The book also gives examples of using the programming utility make for automated program production.

How This Book Is Organized

You must crawl before you walk. In Part I, you learn how to crawl. These chapters teach you enough to write very simple programs. You start with the mechanics of programming and programming style. Next, you learn how to use variables and very simple decision and control statements.

At this point you will have learned enough to create very simple programs; therefore, in Chapter 7, you embark on a complete tour of the programming process that shows you how real programs are created.

  • Chapter 1 gives you an overview of C++, describes its history and uses, and explains how the language is organized.

  • Chapter 2 explains the basic programming process and gives you enough information to write a very simple program.

  • Chapter 3 discusses programming style. How to comment a program is covered, as well as how to write clear and simple code.

  • Chapter 4 introduces simple C++ statements. Basic variables and the assignment statement are covered in detail along with the arithmetic operators: +, -, *, /, and %.

  • Chapter 5 covers arrays and more complex variables. The shorthand operators ++, -- , *=, =, +=, -=, /=, and %= are described.

  • Chapter 6 explains simple decision statements including if, else, and for . The problem of == versus = is discussed.

  • Chapter 7 takes you through the steps required for creating a simple program, from specification through release. Fast prototyping and debugging are discussed.

Part II describes all the other simple statements and operators that are used in programming. You also learn how to organize these statements into simple functions.

  • Chapter 8 describes additional control statements. Included are while, break, and continue. The switch statement is discussed in detail.

  • Chapter 9 introduces local variables, namespaces, functions, and parameters.

  • Chapter 10 describes the C++ preprocessor, which gives you great flexibility in creating code. It also provides a tremendous number of ways for you to screw up. Simple rules that help keep the preprocessor from becoming a problem are described.

  • Chapter 11 discusses the logical C++ operators that work on bits.

In Part III you learn how basic declarations and statements can be used in the construction of advanced types such as structures, unions, and classes. You also learn about the concept of pointers.

  • Chapter 12 explains structures and other advanced types. The sizeof operator and the enum type are included.

  • Chapter 13 introduces the concept of a class. This is one of the more powerful features of C++. Classes allow you to group data and the operations that can be performed on that data into one object.

  • Chapter 14 describes additional operations that can be performed with classes.

  • Chapter 15 introduces C++ pointer variables and shows some of their uses.

Advanced programming techniques are explored in Part IV. In this section, you explore a number of C++ features that let you create complex, yet easy-to-use objects or classes.

  • Chapter 16 describes both buffered and unbuffered input/output (I/O). ASCII and binary files are discussed and you are shown how to construct a simple file. Old C-style I/O operations are also included.

  • Chapter 17 describes how to debug a program and how to use an interactive debugger. You are shown not only how to debug a program, but also how to write a program so that it is easy to debug. This chapter also describes many optimization techniques to make your programs run faster and more efficiently.

  • Chapter 18 explains that C++ allows you to extend the language by defining additional meanings for the language’s operators. In this chapter, you create a complex type and the operators that work on it.

  • Chapter 19 uses a simple decimal floating-point format to introduce the problems inherent in using floating points, such as roundoff errors, precision loss, overflow, and underflow.

  • Chapter 20 describes advanced use of pointers to construct dynamic structures such as linked lists and trees.

  • Chapter 21 shows how to build complex, derived classes out of simple, base ones.

Several miscellaneous features are described in Part V.

  • Chapter 22 explains how to handle unexpected conditions within a program.

  • Chapter 23 shows how to split a program into several files and use modular programming techniques. The make utility is explained in more detail.

  • Chapter 24 allows you to define a generic function or class that generates a family of functions.

  • Chapter 25 describes the template library that comes with C++. This library consists of a number of “container templates” and related data structures which let you create very complex and robust data structures with very little work.

  • Chapter 26 discusses some of the methodologies used to design programs, such as structured programming and object-oriented design. Not only are the design methods discussed, but also the reasoning that went into the design of the program.

  • Chapter 27 details the steps necessary to take a complex program from conception to completion. Information hiding and modular programming techniques, as well as object-oriented programming, are stressed.

  • Chapter 28 describes how to turn C code into C++ code and addresses many of the traps lurking in C code that bite the C++ programmer.

  • Chapter 29 describes the little used do/while statement, the comma operator, and the ?: operators.

  • Chapter 30 lists programming adages that will help you construct good C++ programs.

Part VI contains additional C++ reference information.

  • Appendix A contains a list of character codes and their values.

  • Appendix B lists the numeric ranges of some C++ variable types.

  • Appendix C lists the rules that determine the order in which operators are evaluated.

  • Appendix D contains a program that shows how the computer can compute the value of the sine function.

  • Appendix E lists information on the programming resources mentioned in the book.

How to Read This Book If You Already Know C

C++ is built on the C language. If you know C, you will find much of the material presented in Chapter 2 through Chapter 12 familiar.

C++ does introduce a number of new minor improvements to C, including:

  • An entirely new I/O system. (The basics are described in Chapter 4. The new file system is discussed in detail in Chapter 16.)

  • Constants and reference variables (described in Chapter 5).

  • Function overloading, inline functions, reference parameters, and default parameters. (Read Chapter 9.)

So you can use C++ as a better C. But C++ has added some entirely new features such as objects, templates, and exceptions. So starting with Chapter 13, you will begin to learn entirely new concepts.

Font Conventions

The following conventions are used in this book:

Italic

Used for directories and to emphasize new terms and concepts when they are introduced. Italic is also used to highlight comments in examples.

Bold

Used for C++ keywords.

Constant width

Used for programs and the elements of a program and in examples to show the contents of files or the output from commands. A reference in text to a word or item used in an example or code fragment is also shown in constant width font.

Constant bold

Used in examples to show commands or other text that should be typed literally by the user. (For example, rm foo means to type “rm foo” exactly as it appears in the text or the example.)

Constant italic

Used in examples to show variables for which a context-specific substitution should be made. (The variable filename, for example, would be replaced by some actual filename.)

“Quotes”

Used to identify system messages or code fragments in explanatory text.

%

The Unix C shell prompt.

$

The Unix Bourne shell or Korn shell prompt.

[ ]

Surround optional values in a description of program syntax. (The brackets themselves should never be typed.)

. . .

Stands for text (usually computer output) that’s been omitted for clarity or to save space.

The notation CTRL-X or ^X indicates use of control characters. It means hold down the “control” key while typing the character “x”. We denote other keys similarly (e.g., RETURN indicates a carriage return).

All examples of command lines are followed by a RETURN unless otherwise indicated.

How to Contact Us

Please address comments and questions concerning this book to:

O’Reilly & Associates, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
1-800-998-9938 (in the United States or Canada)
1-707-829-0515 (international or local)
1-707-829-0104 (fax)

There is a web page for this book, which lists errata, examples, or any additional information. You can access this page at:

http://www.oreilly.com/catalog/cplus2

To comment or ask technical questions about this book, send email to:

For more information about books, conferences, Resource Centers, and the O’Reilly Network, see the O’Reilly web site at:

http://www.oreilly.com/

Acknowledgments for the First Edition

Thanks to Peg Kovar for her proofreading and editing help. Special thanks to Dale Dougherty for ripping apart my first book and forcing me to put it together correctly. I greatly appreciate the hard work put in by Phil Straite and Gregory Satir. I especially thank all those people who reviewed and edited my book. My thanks also go to the production group at O’Reilly & Associates—Nicole Gipson, project manager and production editor; John Files, Juliette Muellner, and Jane Ellin, production assistants; and Mike Sierra, book design implementor. Finally, special thanks go to all the hard-working programmers out there whose code has taught me so much.

Acknowledgments for the Second Edition

For the second edition I wish to thank my editor, Robert J. Denn, for his patience and hard work in getting the book done. Thanks to Ray Lischner for his technical insight. Al Stevens deserves special recognition for his extensive knowledge of C++ and his exacting standards. His efforts helped me to tighten the terminology and refine the examples in the book, resulting in a much more precise manuscript. Any errors in this book are my own and are not the fault of the reviewers or of the staff at O’Reilly.

Also I wish to give credit to all the sales and marketing people at O’Reilly who work so hard to sell my book.



[1] The GNU g++ compiler can be obtained from http://www.gnu.org, or you can contact the Free Software Foundation, Inc., at 675 Massachusetts Avenue, Cambridge, MA 02139, (617) 876-3296.

Get Practical C++ Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.