ENCAPSULATING C++ BOOKS

C++ resources for your bookshelf

Andrew Schulman

Andrew is a software engineer in Cambridge, Mass. and is a contributing editor to DDJ. Andrew can be reached at 32 Andrew St., Cambridge, MA 02139.


For a long time, if you wanted to learn C++ and happened not to work down the hall from its inventor, Bjarne Stroustrup, the only resource you could turn to was Stroustrup's book, The C++ Programming Language (Reading, Mass.: Addison-Wesley, 1986, 328 pages, $22.95). But trying to learn C++ from the Stroustrup book is like trying to learn chemistry from the Periodic Table of the Elements. That situation has changed, however, and there are finally some good books from which you can learn C++.

The definitive book on C++ 2.0 is Stanley B. Lippman's C++ Primer (Reading, Mass.: Addison-Wesley, 1989, 464 pages, $31.25). Like Stroustrup, Lippman works at AT&T Bell Laboratories. This is a tutorial, not a reference. After a brief discussion of the more C-like features of C++, Lippman moves quickly into a discussion of class. The book presents thorough discussions of the three key features of object-oriented programming: abstract data types, inheritance, and dynamic binding. There are also several discussions of when not to use such C++ features such as operator- and function-overloading. I might quibble with a few aspects of the book, such as the odd interface for a BitVector class, but if you can afford only one C++ book, this is the one to get. While the book assumes the reader is using C++ 2.0 (and the unsuspecting reader using C++ 1.2 might get tripped up in a few places), an appendix presents a clear discussion of the differences between C++ 1.2 and 2.0. Another appendix discusses the differences between C and C++.

Like Stroustrup's book, though, Lippman's primer largely presents the language features, and provides little direction on how to use C++. Another new C++ book makes up for this deficiency by encouraging the reader to think in C++, and introduces general principles of data abstraction and object-oriented programming: Programming in C++, by Stephen C. Dewhurst and Kathy T. Stark (Englewood Cliffs, New Jersey: Prentice Hall, 1989, 233 pages, $22.00). They've included some cutesy code in this book (for example, a File class in which the class constructor opens the file and the destructor closes the file, and an iterator object that uses operator( )( )), but one of the authors' goals is to show how C++ supports a wide variety of programming methods and techniques. An entire chapter is devoted to libraries, a crucial topic entirely missing from other books on C++. This book appears in the superb "Prentice Hall Software Series," edited by Brian Kernighan.

Object-oriented design is largely bottom-up, emphasizing the creation of reusable classes that are then soldered together to form different applications. Brad Cox, author of Objective-C, has popularized this notion under the name "Software-IC" (which Cox unfortunately then went on to trademark). True to its spartan C heritage, however, C++ compilers don't come with extensive libraries of such reusable classes. Therefore, beginning C++ programmers need, above all, some useful classes such as those that Smalltalk programmers take for granted.

One new C++ book addresses this problem by providing you with tons of reusable classes: The C++ Answer Book, by Tony L. Hansen (Reading, Mass.: Addison-Wesley, 1990, 578 pages, $26.95). As the C Answer Book is to K&R, so the C++ Answer Book is to Stroustrup. It provides answers for every exercise in the book. For example, Exercise 6.11 is "Define a class implementing arbitrary precision arithmetic"; Hansen's answer runs over 30 pages. Exercise 7.10 (marked as difficulty-level *5 by Stroustrup) is "Design and implement a library for writing event-driven simulations. Hint: <task.h>"; Hansen's answer runs almost 40 pages. As the book is tied to Stroustrup, there is little use of C++ 2.0 here. Surprisingly, the book not only employs Unix, but also recognizes the importance of MS-DOS. In addition to using AT&T's cfront Translator, the answers were tested on the Zortech C++ compiler 1.5. Where exercise 5.5 for defining an arithmetic-expression class also suggests toying with different ways of printing the expression, including assembly code, Hansen proceeds to do so using not only the AT&T WE32100, but also the Intel 8086. An amazing book! My only quibble is the large number of typographical errors (like most books out of AT&T, this one was typeset by the author). The forthcoming fourth printing will correct these errors.

Where does C++ fit into the hierarchy of programming languages? How does it differ from Smalltalk? from Modula-2? from Ada? from C? If you are interested in programming languages in general, and want to view C++ in some larger context, get Ravi Sethi's textbook, Programming Languages: Concepts and Constructs (Reading, Mass.: Addison-Wesley, 1989, 478 pages, $40.95). Sethi is one of the co-authors of the famous "dragon book" on compilers and, again, works at Bell Labs. Sethi's chapters on "Data Encapsulation" and "Inheritance" both contain extensive discussions of C++.

In general, when there are both trade and academic books on a subject such as C++, it is safer to go with the academic book. Fortunately, this is changing: Bruce Eckel's Using C++ (Berkeley: Osborne/McGraw-Hill, 1989, 617 pp., $24.95) is a wonderful book. I don't normally buy books from Osborne/ McGraw-Hill (which publishes a lot of books that are either identical to, or worse than, the manuals they purport to supplement), but I made an exception here because of Eckel's name on the cover (Bruce wrote the brilliant "Programmer's Guide to the Parallel Port" in Vol. 1, No. 1 of the late lamented Turbo Technix). One of the interesting examples is a program for the game of Life: Bruce realized that Life is actually a problem in simulation and modeling, so his code presents a framework in which the rules of the simulation can be easily changed. Other examples include a tiny AWK interpreter named TAWK (which appeared in DDJ in May 1989), a clock-based control system, and a drawing program called Micro-CAD, which uses the mouse and graphics package from Zortech C++. All the code is available on disk. Great stuff!

If you are already proficient in C++ and are looking for advanced material, check out the proceedings of the C++ conferences and workshops given by USENIX (USENIX Proceedings, C++ Workshop, Santa Fe, New Mexico: 1987, 468 pp., and USENIX Proceedings, C++ Conference, Denver, Colo., 1988, 362 pp., USENIX Association, P.O. Box 2299, Berkeley, CA 94710, $30.00 each). Some trade publisher ought to pick these up and make them more widely available.

The Denver volume includes Stroustrup's papers on "Parameterized Types for C++" and "Type-safe Linkage for C++." Other papers include "Exception Handling without Language Extensions" by William M. Miller, and "Pointer to Class Members in C++" by Lippman and Stroustrup.

The Santa Fe proceedings includes these papers by Stroustrup: "The Evolution of C++ from 1985 to 1987," "What is 'Object-Oriented Programming'?" "Possible Directions for C++," and "A Set of C++ Classes" (with Jon Shopiro). Other useful papers in the Santa Fe volume are "Two Extensions to C++: A Dynamic Link Editor and Inner Data" by Gautron and Shopiro, "C++ on the Macintosh" by Friedenbach, and "An Object-Oriented Class Library for C++ Programs" by Keith Gorlen.

Keith Gorlen is at the National Institute of Health (NIH), and is the author of the NIH class library, a Smalltalk-like class hierarchy for C++ (class Object is at the root of the class inheritance tree). Even those who disagree with this pushing of C++ in the direction of Smalltalk, and want to keep the language closer to its C origins, would still benefit from examining Gorlen's code, which is probably the largest body of public-domain C++ source code available today. Michael Murphy of Cambridge, Mass. ported the NIH library to MS-DOS, and it may be found on the ImageSoft BBS (516-767-9074) and on BIX (listings area c.plus.plus). There is also an active C++ forum (c.plus.plus) on BIX, in which Bjarne Stroustrup (bstroustrup) participates.