Learn ANSI C++ right from the start!
If you're going to learn C++, learn it right from the start! Navigating C++ and Object-Oriented Design is the first book that teaches ANSI C++ and the object-oriented techniques that make C++ a software development breakthrough.
This comprehensive tutorial reflects the authors' extensive experience teaching C++ to professional developers. Start with the clearest introduction to object modeling you'll ever read. Then learn all the essentials of C++ and more advanced topics such as exceptions, namespaces, and dynamic memory allocation.
Next, discover C++ classes, the basic building block of object-oriented design. Through proven analogies and examples, the authors introduce containment and inheritance, overloading, templates, RTTI, and exception handling. And, the authors' unique presentation on multiple inheritance will give you invaluable insights into this complex topic.
Learn to apply C++ and object-oriented techniques to real-world development. Discover how to design classes effectively using the authors' class design boilerplate. Understand how to design container classes. Walk through class designs using the powerful OMT methodology.
Navigating C++ and Object-Oriented Design is one of the first C++ tutorials that covers in detail powerhouse ANSI C++ features like:
Why learn C++ simply as a "better C" when you can write C++ code that really meets its potential? One book shows you how: Navigating C++ and Object-Oriented Design!
1. Getting Started.
Object-Oriented Design. Object Modeling Technique (OMT). Object Model Notation. Scenarios. Putting It All Together. Key Point Summary. Exercises.
Data Representation and Built-in Types. Preprocessor Directives. Comments. Type-Safe I/O. Operators and Expressions. Control Flow Constructs. Putting It All Together. Key Point Summary. Exercises.
Functions. Structures and Unions. References with Functions. Storage Classes. Exceptions. Namespaces. Dynamic Memory Allocation. Putting It All Together. Key Point Summary. Exercises.
What Is Encapsulation? Classes. Constructors. Destructors. What's this? Exception Objects. Const Objects. Volatile Objects. Copy Constructors. The Problem with Public Data Members. Data Member Objects. Class Member Initialization. Putting It All Together. Key Point Summary. Exercises.
Using explicit. Object Lifetimes. Static Data Members. Static Member Functions. Static Objects. Arrays of Class Objects. Pointers to Class Members. Friend Classes. Nested Classes. Local Classes. Putting It All Together. Key Point Summary. Exercises.
Why Overload Functions? Function Overloading. Overloading Resolution. Why Overload Operators? Overloadable Operators. Operator Functions. Putting It All Together. Key Point Summary. Exercises.
What Is Class Design? A Class Design Boilerplate. A String Class. A Range Integer Class. Putting It All Together. Key Point Summary. Exercises.
Global new and delete. Class-Specific new and delete. Reference Counts. Putting It All Together. Key Point Summary. Exercises.
Why Should Functions Be Generic? Template Function Definition. Template Function Instantiation. Overloading Template Functions. Specializing Template Functions. Putting It All Together. Key Point Summary. Exercises.
Why Should Classes Be Generic? Template Class Definition. Template Class Instantiation. Specializing Template Classes. Containment with Template Classes. Composite Templates. Template Class Static Members. Constant Expression Parameters. Template Friend Functions. Template Friend Classes. Template Nested Classes. Member Templates. Putting It All Together. Key Point Summary. Exercises.
Why Use Inheritance? Public Derivation. Subtypes. Virtual Functions. Protected Access. Using Declarations. Private Derivation. Protected Derivation. Summary of Derivation Options. Polymorphism with a Generic PtrList Class. Abstract Base Classes. Virtual Constructors. A Class Design Boilerplate with Inheritance. Putting It All Together. Key Point Summary. Exercises.
Why Is RTTI Necessary? The Dynamic Cast Operator. The Typeid Operator. RTTI Applications. Putting It All Together. Key Point Summary. Exercises.
Why Use Exceptions? Designing with Exceptions. Exception Hierarchies. Resource Management. Uncaught Exceptions. Exception Specifications. Unexpected Exceptions. Putting It All Together. Key Point Summary. Exercises.
Why Use Multiple Inheritance? Multiple Inheritance Format. Multiple Inheritance Characteristics. Distinct Base Classes (Pattern 1). Multiple Inclusion (Pattern 2). Virtual Base Classes (Pattern 3). Interface and Implementation. Putting It All Together. Key Point Summary. Exercises.
Why Use the IOStream Library? IOStream Overview. IOStream Examples.
Why Use STL? STL Overview. STL Examples. STL References.
C++ Operator Precedence.
An Expedition, Not a Tour
A packaged tour to a foreign country coddles groups of tourists who are led from tourist trap to tourist trap, getting a watered-down simplistic view of life. Most details are pre-planned, and tourists do not have to make many decisions en route. The experience is definitely low tech, with easy participation from toddler through senior citizen. The couch- potato is "at home," simply turning into a "coach potato."
By contrast, an expedition requires a high degree of planning by the participants, who require the latest in technology for equipment and strategy. Training occurs before the expedition begins and continues throughout the trek. Whether the expedition involves exploration, goal attainment, field study, or humanitarian work, the experience demands more from the participant than a tour. Any detailed expedition requires a navigating guide; hence, the title of this book.
Our aim with this book is to provide the reader with the opportunity of making an expedition with C++. We intend the material to be detailed and highly technical, with all the latest and greatest that ANSI C++ has become. We expect the reader to be an active participant, to seek challenge, and shun a necessarily watered-down approach that promises expertise in several weeks.
Navigating C++ and Object-Oriented Design is an ambitious book. Our goal is to provide a C++ reference book, a tutorial for C++ beginners, and an advanced book-all in one. The book has evolved over many years, changing with the ANSI C++ standard and with our experiences teaching C++ to industry professionals.
Navigating C++ and Object-Oriented Design started out as a book to teach C++. With years of classroom feedback, we honed the examples to teach specific concepts. Which programs really show how initialization and assignment differ with C++? Which examples make students understand the importance of virtual destructors? Why are container classes tricky to design, and what are the trade-offs? How does one go about designing classes? The best people to answer these questions are students, and we have benefited tremendously from their input.
The material in each chapter progresses from introductory to more advanced. Each chapter concludes with a "Putting It All Together" section that draws upon several features presented in the chapter. The examples presented here are frequently more involved and therefore more advanced. The Key Point Summary at the end of each chapter summarizes important points, and a set of Exercises is included in each chapter to augment the material. All chapters include Tips, which caution the user about common mistakes, give guidelines about design issues, or touch on interesting points that are asides to the main thread. We mark Tips with a compass icon, as shown.
The enclosed CD-ROM contains all of the sample programs and solutions to the exercises presented in this text. You will find implementations for several platforms, including Windows and UNIX-based C++ compilers. For revisions to this code, visit our web site at:
C++ is a big language, and it's not all easy. In an effort to be complete, we present some advanced material. Overloading the new and delete operators, protected derivation, and multiple inheritance, for example, are all advanced topics that a C++ newcomer can skip initially. Finally, one cannot use C++ effectively without using object-oriented design. While we do not attempt to teach object-oriented design methodologies in this book, we do want to teach C++ in the context of object-oriented design. Chapter 1 explores object modeling strategies, giving readers the "big picture" in designing with C++.
ANSI C++ Compatibility
If you are a practicing C++ programmer and would like to be current with the ANSI C++ standard, this book will help you. By describing all of the latest features and incorporating them into sample programs, this book makes every effort to be compatible with the current ANSI C++ standard. You'll discover all of the recent additions to the standard here, such as namespaces, the STL library, mutable, export, nothrow versions of operators new and delete, auto_ptr class, and member templates.
Over the years, the standard has added many features to the language. The major extension are templates, exception handling, and run-time type identification. These language extensions are important, because they play crucial roles in how you design programs with C++. For this reason, we believe these features should be integrated throughout the book (as they are in the C++ language) and not presented as an afterthought in an appendix.
The Order of Things
When language features are closely intertwined, teaching them strictly sequentially is sometimes difficult. For example, before learning about inheritance, you must learn about classes. Before learning about classes, you must learn about functions. Operator overloading requires knowledge of typing, functions, and classes. Good examples of polymorphic programming require template container classes with pointer semantics and inheritance. The end result is that inheritance is pushed towards the later chapters. While it hardly seems "right" that such a primary feature of C++ isn't covered sooner, the inheritance examples (Chapter 11) are more detailed and real-world. We show what inheritance is good for by using C++ features covered in earlier chapters. However, you may read any given chapter out of order. We make generous use of forward and backward references which point the reader to related topics covered in other sections of the book.
How to Read This Book
All readers should start with Chapter 1 (Getting Started). This chapter explores the important features of C++, such as inheritance, polymorphism, template container classes, and exception handling. Chapter 1 explains object modeling notations and concepts so that students learn more than just how to use C++ as a better C. It also gives a hint of where the book is going and what we are striving for. This approach shows you why a modeling notation with design concepts such as aggregation, association, inheritance, classes, and responsibilities is important. It explains what objects are and gives you a reason to learn C++.
Beginners who don't know C should proceed to Chapter 2 (C++ Basics). Beginners who know C can start with Chapter 4 (Classes) and use Chapter 2 and Chapter 3 for reference. Experienced C++ programmers should start with Chapter 7 (Class Design). This chapter introduces our class design boilerplate, an important foundation that we'll use throughout the rest of the book.
We recommend a sequential reading of the book, but that's not strictly necessary. As a new C++ user, you may want to tackle only portions of the language. What's essential to get started? Skim Chapter 2 and Chapter 3. Chapter 4 (Classes) is essential, as well as Chapter 7 (Class Design) and Chapter 11 (Inheritance). This, of course, limits you. But with another pass, you can pick up Overloading (Chapter 6) and Template Classes (Chapter 10). A third pass covers Working with Classes (Chapter 5), Template Functions (Chapter 9), Run-Time Type Identification (Chapter 12) and Exception Handling (Chapter 13). Finally, Object Storage Management (Chapter 8) and Multiple Inheritance (Chapter 14) complete the text. Now, come full circle and reread Chapter 1.
A Word About the Standard Template Library
The Standard Template Library (STL) is part of the ANSI C++ standard and, as such, is an important tool in any C++ programmer's toolbox. You will find an informative overview with numerous examples in Appendix B. However, we elected not to use STL containers in our examples throughout the book. Why not? The design of container classes (such as arrays, lists, stacks, and so forth) requires an understanding of C++ and the treatment of objects in the run-time environment. We recognize that container class design requires trade-offs. Our own class designs, therefore, gives us the most flexibility in how we present these issues. Our Array class, for example, explains copy and assignment semantics with operator overloading and helps you understand how the STL vector class works.
Our associative array class, Assoc, shows you how to map generic entries and separate lvalues from rvalues in expressions (a feature that the STL map class does not currently provide). Furthermore, our List class illustrates the importance of separate cooperating container and iterator classes. We cover the difference between value and pointer semantics with container classes and how this difference affects container class design. Readers will be able to better evaluate the STL container classes after understanding the issues from our designs.
Paul and Gail Anderson