Summary
- Recipient of Computer Language 1991 JOLT Cola Productivity Award
- One of JOOP Magazine's Top Ten Books of the Decade (three years in a row)
- A 1992 Stacey's best-seller
- Continued critical acclaim in October 1995 Dr. Dobb's
Written by an expert in C++, Advanced C++ Programming Styles and Idioms looks at the design features of the C++ programming language. Assuming a background in the syntax of C++, Coplien shows how to become an expert C++ programmer by learning the idioms of the language. His approach is organized around the abstractions that C++ supports: abstract data types, combining types in inheritance structures, object-oriented programming, and multiple inheritance. Using small, but rich examples, he shows how these abstractions can be combined to use the language effectively. Experienced C++ programmers will appreciate the comprehensive coverage of Release 3.0.
0201548550B04062001
Author Biography
James O. Coplien is a premier expert and writer on the object paradigm and C++, having worked with the language since its inception at AT&T. Currently a member of Bell Laboratories Research at Lucent Technologies, his work focuses on multi-paradigm development methods and organizational anthropology for software development processes. His previous books include Pattern Languages of Program Design (with Douglas C. Schmidt), Pattern Languages of Program Design, Volume 2 (with John M. Vlissides and Norman L. Kerth), and Advanced C++ Programming Styles and Idioms.
0201548550AB04062001
Table of Contents
| Preface | |
| Introduction | |
| C++: An Evolving Language | |
| Handling Complexity with Idioms | |
| Objects for the Nineties | |
| Design and Language | |
| Data Abstraction and Abstract Data Types | |
| Classes | |
| Object Inversion | |
| Constructors and Destructors | |
| Inline Functions | |
| Initialization of Static Data Members | |
| Static Member Functions | |
| Scoping and const | |
| Initialization Ordering of Global Objects, Constants, and Static Class Members | |
| Enforcement of const for Class Object Member Functions | |
| Pointers to Member Functions | |
| Program Organization Conventions | |
| Concrete Data Types | |
| The Orthodox Canonical Class Form | |
| Scoping and Access Control | |
| Overloading: Redefining the Semantics of Operators and Functions | |
| Type Conversion | |
| Reference Counting: Making Variables Use “Magic Memory | |
| ” Operators new and delete | |
| Separating Initialization from Instantiation | |
| Inheritance | |
| Simple Inheritance | |
| Scoping and Access Control | |
| Constructors and Destructors | |
| Class Pointer Conversion | |
| Type Selector Fields | |
| Object-Oriented Programming | |
| C++ Run-Time Type Support: Virtual Functions | |
| Destructor Interaction and Virtual Destructors | |
| Virtual Functions and Scoping | |
| Pure Virtual Functions and Abstract Base Classes | |
| Envelope and Letter Classes | |
| Functors: Functions as Objects | |
| Multiple Inheritance | |
| The Inheritance Canonical Form | |
| Object-Oriented Design | |
| Types and Classes | |
| The Activities of Object-Oriented Design | |
| Object-Oriented Analysis and Domain Analysis | |
| Object and Class Relationships | |
| Subtyping, Inheritance and Forwarding | |
| Rules of Thumb for Subtyping, Inheritance, and Independence | |
| Reuse and Objects | |
| All Analogies Break Down Somewhere | |
| Design Reuse | |
| Four Code Reuse Mechanisms | |
| Parameterized Types, or Templates | |
| Private Inheritance: Does Inheritance Support Reuse? | |
| Storage Reuse | |
| Interface Reuse: Variants | |
| Reuse, Inheritance, and Forwarding | |
| Architectural Alternatives for Source Reuse | |
| Generalizations on Reuse and Objects | |
| Programming with Exemplars in C++ | |
| An Example: Employee Exemplars | |
| Exemplars and Generic Constructors: The Exemplar Community Idiom | |
| Autonomous Generic Constructors | |
| Abstract Base Exemplars | |
| Toward a Frame Exemplar Idiom | |
| A Word About Notation | |
| Exemplars and Program Administration | |
| Emulating Symbolic Language Styles in C++ | |
| Incremental C++ Development | |
| Symbolic Canonical Form | |
| An Example: A General Collection Class | |
| Code and Idioms To Support Incremental Loading | |
| Garbage Collection | |
| Primitive Type Encapsulation | |
| Multi-Methods under the Symbolic Idiom | |
| Dynamic Multiple Inheritance | |
| An Example: A Multi-Technology Window System | |
| Caveats | |
| Systemic Issues | |
| Static System Design | |
| Dynamic System Design | |
| Appendix A: C in a C++ Environment | |
| Function Calls | |
| Function Parameters | |
| Function Prototypes | |
| Call-by-Reference Parameters | |
| Variable Number of Parameters | |
| Function Pointers | |
| The const Type Modifier | |
| Interfacing with C Code | |
| Shapes Program: C++ Code | |
| Reference Return Values from Operators | |
| Why Bitwise Copy Doesn't Work | |
| Why Member-by-Member Copy Isn& | |
| Table of Contents provided by Publisher. All Rights Reserved. |
Excerpts
This book is designed to help programmers who have already learned C++ develop their programming expertise. To understand how programmers achieve proficiency, we need to understand not only how people learn a new language (such as a programming language), but also how a language is used to solve software problems effectively. Learning Programming Languages Not everything you need to know about a product is described in the owner''s manual. Before the arrival of our first child, my wife and I were admonished by a friend that no book, and no training, could completely prepare us for the art of parenting. We must of course learn minimal, essential skills. But the interesting, challenging, and rewarding aspects of raising a child go beyond this basic knowledge. For example, no book or "owner''s manual" will help you understand why your three-year-old daughter rubs toothpaste in your one-year-old''s hair, or why your children hang their socks in the refrigerator. The same is true of programming languages. Programming language syntax shapes our thinking to a degree, but what we learn in the "owner''s manual" about syntax alone only gets us started. Most of what guides the structure of our programs, and therefore of the systems we build, is thestylesandidiomswe adopt to express design concepts. Style distinguishes excellence from accomplishment. An effective parenting style, or programming style, comes from personal experience or by building on the experience of others. A software engineer who knows how to match a programming language to the needs of an application, writes excellent programs. To achieve this level of expertise, we need to go beyond rules and rote, into convention and style, and ultimately into abstractions of concept and structure. It is in that sense that this book is "advanced." The rules, conventions, and concepts of programming drive the structure of the systems we build: They give us a model of how to build systems. A model for problem decomposition and system composition is aparadigm, a pattern for dividing the world into manageable parts. C++ is a multiparadigm language. C programmers use C++ as a better C. Object-oriented advocates do everything polymorphically. In fact, a variety of approaches is usually necessary to express the solution to a software problem efficiently and elegantly. Learning a programming language is much like learning a natural language. Knowledge of basic syntax lets a programmer write simple procedures and build them into nontrivial programs, just as someone with a vocabulary of a few hundred German words can write a story far richer than see-Dick-run. But mastery of language is quite another issue. That such stories are nontrivial does not make them elegant or demonstrate fluency. Learning language syntax and basic semantics is like taking a 13-hour course in German: It prepares you for the task of ordering a bratwurst, but not for going to Germany to make a living, and certainly not for getting a job as a German language journalist or poet. The difference is in learning theidiomsof the language. For example, there is nothing in C itself that establishes while (*cp1++ = *cp2++); as a fundamental building block, but a programmer unfamiliar with this construct would not be perceived as a fluent C programmer. In programming, as in natural language, important idioms underly the suitability and expressiveness of linguistic constructs even in everyday situations. Good idioms make the application programmer''s job easier, just as idioms in any language enrich communication. Programming idioms are reusable "expressions" of programming semantics, in the same sense that classes are reusable units of design and code. Simple idioms (like thewhileloop above) are notational conveniences, but seldom are central to program design. This book focuses on idioms that influence how the language is used in the overall structure and implementation of a design. Such idioms take insight and time to refine, more so than the simple notational idioms. The idioms usually involve some intricacy and complexity, details that can be written once and stashed away. Once established, programming language idioms can be used with convenience and power. The Book''s Approach Assuming a background in the basic syntax of C++, this book imparts the proficiency that expert C++ programmers gain through experience by giving a feel for the styles and idioms of the language. It shows how different styles let C++ be used for simple data abstraction, full-fledged abstract data type implementation, and various styles of object-oriented programming. It also explores idioms that the core of the C++ language does not directly support, such as functional and frame-based programming, and advanced garbage collection techniques. The Book''s Structure Rather than taking a "flat" approach to learning the advanced features of C++ by organizing around language features, this book looks at these increasingly powerful abstractions from the perspective of the C++ features required to support them. Each chapter of this book is organized around a family of such idioms. The idioms progressively build on each other in successive chapters. Chapter 1 provides a historical perspective on C++ idioms. It provides some motivation as to why idioms came about, and varying degrees to which different idioms can be thought of as part of the language or as outside the language. Chapter 2 introduces the fundamental C++ language building blocks: classes and member functions. Though much of the material is basic, the chapter establishes idioms and vocabulary that recur in later chapters. It introduces compiler type systems, and their relationship to user-defined types and classes, from a design perspective. It also presents idioms that makeconstmore useful. Chapter 3 introduces idioms that make classes "complete" types. C++ has been evolving to automate more and more of the work of copying and initializing objects, but programmers still need to customize assignment and default constructors for most nontrivial classes. This chapter provides a framework for that customization. I call the idioms described in this chaptercanonical forms, meaning that they define principles and standards to make the underlying mechanics of objects work. In addition to the most commonly used canonical form, idioms are presented to apply reference counting to new and existing classes. These are the first idioms of the book to go beyond straightforward application of the base C++ syntax. A variation on reference counting, counted pointers, is shown as a way to move C++ a step further away from the machine, abandoning pointers in deference to smarter, pointer-like objects. Lastly, the chapter looks at how to separate the creation of an object from its initialization. To someone familiar with basic C++, this might seem an unnatural idiom: C++ tightly couples these two operations. The need to separate them arises in the design of device drivers and in systems with mutually dependent resources. Chapter 4 introduces inheritance; Chapter 5 adds polymorphism to inheritance to introduce object-oriented programming. Many new C++ programmers get "inheritance fever," using it at every occasion. While it is true that inheritance is used mostly to support the object paradigm, it has a distinctly separate application for software reuse. Introducing inheritance apart from polymorphism helps the reader separate the two concepts and avoids the confusion that often arises from trying to internalize two foreign concepts at once. Chapter 6 approaches the constructs, styles and idioms of C++ from the perspectives of architecture and design. It examines what classesmeanat the level of an application, high above the level of syntax. Appreciating the relationships between the design abstractions of an applicati