Cover....1
Copyright....3
Contributors....5
Table of Contents....8
Preface....18
Section 1: C++ Building Block Essentials....24
Chapter 1: Understanding Basic C++ Assumptions....26
Technical requirements....27
Reviewing basic C++ language syntax....27
Variable declarations and standard data types....27
Variables and array basics....28
Comment styles....30
Recapping basic I/O....30
The iostream library....30
Basic iostream manipulators....32
Revisiting control structures, statements, and looping....33
Control structures: if, else if, and else....33
Looping constructs: while, do while, and for loops....34
Reviewing C++ operators....37
Revisiting function basics....38
Reviewing user-defined type basics....40
struct....40
typedef....41
class....42
enum....43
Recapping namespace basics....44
Summary....46
Questions....47
Chapter 2: Adding Language Necessities....48
Technical requirements....49
Using the const qualifier....49
Const variables....49
Const qualification with functions....52
Working with function prototypes....52
Defining function prototypes....53
Naming arguments in function prototypes....54
Adding default values to function prototypes....55
Prototyping with different default values in different scopes....57
Understanding function overloading....59
Learning the basics of function overloading....59
Eliminating excess overloading with standard type conversion....62
Ambiguities arising from function overloading and type conversion....64
Summary....66
Questions....67
Chapter 3: Indirect Addressing: Pointers....68
Technical requirements....69
Understanding pointer basics and memory allocation....69
Revisiting pointer basics....70
Using the address-of and dereference operators....70
Using the operators new() and delete() ....71
Creating and using pointers to user defined types....72
Allocating and deallocating arrays at run time....74
Dynamically allocating single-dimension arrays....74
Dynamically allocating 2-D arrays: arrays of pointers....78
Dynamically allocating 2-D arrays: pointers to pointers....81
Dynamically allocating N-D arrays: pointers to pointers to pointers....83
Using pointers with functions....86
Passing pointers as arguments to functions....86
Using pointers as return values from functions....89
Using the const qualifier with pointers....91
Using pointers to constant objects....92
Using constant pointers to objects....93
Using constant pointers to constant objects....94
Using pointers to constant objects as function arguments and as return types from functions....95
Using pointers to objects of unspecified types....98
Creating void pointers....99
Summary....101
Questions....101
Chapter 4: Indirect Addressing: References....104
Technical requirements....105
Understanding reference basics....106
Declaring, initializing, and accessing references....106
Referencing existing objects of user defined types ....109
Using references with functions....110
Passing references as arguments to functions....111
Using references as return values from functions....114
Using the const qualifier with references....116
Using references to constant objects....117
Using pointers to constant objects as function arguments and as return types from functions....118
Realizing underlying implementation and restrictions....121
Understanding when we must use pointers instead of references....122
Summary....123
Questions....124
Section 2: Implementing Object-Oriented Concepts in C++....126
Chapter 5: Exploring Classes in Detail....128
Technical requirements....130
Introducing object-oriented terminology and concepts....130
Understanding object-oriented terminology....130
Understanding object-oriented concepts....132
Understanding class and member function basics....132
Examining member function internals; the "this" pointer....137
Using access labels and access regions....142
Understanding constructors....147
Applying constructor basics and overloading constructors....147
Creating copy constructors....150
Creating conversion constructors....156
Understanding destructors....162
Applying destructor basics and proper usage....163
Applying qualifiers to data members and member functions....167
Adding inline functions for potential efficiency....167
Adding const data members and the member initialization list....171
Using const member functions....175
Utilizing static data members and static member functions....179
Summary....186
Questions....187
Chapter 6: Implementing Hierarchies with Single Inheritance....190
Technical requirements....191
Expanding object-oriented concepts and terminology....192
Deciphering generalization and specialization....192
Understanding single inheritance basics....193
Defining base and derived classes, and accessing inherited members....193
Examining inherited access regions....196
Understanding inherited constructors and destructors ....197
Implicit constructor and destructor invocations....197
Usage of the member initialization list to select a base class constructor....198
Putting all the pieces together....200
Implementation inheritance – changing the purpose of inheritance....208
Modifying access labels in the base class list by using protected or private base classes....208
Creating a base class to illustrate implementation inheritance....210
Using a private base class to implement one class in terms of another....215
Using a protected base class to implement one class in terms of another....218
Summary....224
Questions....225
Chapter 7: Utilizing Dynamic Binding through Polymorphism....226
Technical requirements....227
Understanding the OO concept of polymorphism....228
Implementing polymorphism with virtual functions....229
Defining virtual functions and overriding base class methods....229
Generalizing derived class objects....233
Utilizing virtual destructors....234
Putting all the pieces together....236
Considering function hiding....244
Understanding dynamic binding....249
Comprehending runtime binding of methods to operations....250
Interpreting the virtual function table (v-table) in detail....251
Summary....255
Questions....256
Chapter 8: Mastering Abstract Classes....258
Technical requirements....259
Understanding the OO concept of an abstract class....259
Implementing abstract classes with pure virtual functions....260
Creating interfaces ....262
Generalizing derived class objects as abstract types ....265
Putting all the pieces together....266
Summary....276
Questions....277
Chapter 9: Exploring Multiple Inheritance....278
Technical requirements....279
Understanding multiple inheritance mechanics....279
Examining reasonable uses for MI....286
Supporting Is-A and mix-in relationships....287
Supporting Has-A relationships....288
Creating a diamond-shaped hierarchy....288
Utilizing virtual base classes to eliminate duplication....293
Considering discriminators and alternate designs....300
Summary....303
Questions....304
Chapter 10: Implementing Association, Aggregation, and Composition....306
Technical requirements....307
Understanding aggregation and composition....307
Defining and implementing composition....308
Defining and implementing a generalized aggregation....315
Understanding associations....316
Implementing association....318
Utilizing back-link maintenance and reference counting....326
Summary....328
Questions....328
Section 3: Expanding Your C++ Programming Repertoire....332
Chapter 11: Handling Exceptions....334
Technical requirements....335
Understanding exception handling....335
Utilizing exception handling with try, throw, and catch....335
Exploring exception handling mechanics with typical variations....340
Utilizing exception hierarchies....349
Using standard exception objects....349
Creating customized exception classes....350
Creating hierarchies of user-defined exception types....355
Summary....355
Questions....356
Chapter 12: Friends and Operator Overloading....358
Technical requirements....359
Understanding friend classes and friend functions....359
Using friend functions and friend classes....360
Making access safer when using friends....363
Deciphering operator overloading essentials....366
Implementing operator functions and knowing when friends might be necessary....368
Summary....376
Questions....376
Chapter 13: Working with Templates....378
Technical requirements....379
Exploring template basics to genericize code....379
Examining the motivation for templates....380
Understanding template functions and classes....382
Creating and using template functions....382
Creating and using template classes....384
Examining a full program example....387
Making templates more flexible and extensible ....391
Adding operator overloading to further genericize template code....392
Summary....394
Questions....395
Chapter 14: Understanding STL Basics....396
Technical requirements....397
Surveying the contents and purpose of the STL....397
Understanding how to use essential STL containers....399
Using STL list....399
Using STL iterator....401
Using STL vector....404
Using STL deque....406
Using STL stack....409
Using STL queue....411
Using STL priority queue....413
Examining STL map....414
Examining STL map using a functor....418
Customizing STL containers....420
Summary....421
Questions....422
Chapter 15: Testing Classes and Components....424
Technical requirements....425
Contemplating OO testing....425
Understanding canonical class form....426
Default constructor....426
Copy constructor....427
Overloaded assignment operator....427
Virtual destructor....428
Move copy constructor....428
Move assignment operator....429
Bringing the components of canonical class form together....429
Ensuring a class is robust....435
Creating drivers to test classes....436
Testing related classes....439
Testing classes related through inheritance, association, or aggregation....439
Testing exception handling mechanisms....442
Embedding exception handling in constructors to create robust classes....442
Summary....443
Questions....444
Section 4: Design Patterns and Idioms in C++....446
Chapter 16: Using the Observer Pattern....448
Technical requirements....449
Utilizing design patterns....449
Understanding the Observer pattern....450
Implementing the Observer pattern....451
Creating Observer, Subject, and domain-specific derived classes....451
Summary....465
Questions....466
Chapter 17: Applying the Factory Pattern....468
Technical requirements....469
Understanding the Factory Method pattern....469
Implementing the Factory Method pattern....470
Including the Factory Method in the Product class....470
Creating an Object Factory class to encapsulate the Factory Method....478
Summary....483
Questions....483
Chapter 18: Applying the Adapter Pattern....484
Technical requirements....485
Understanding the Adapter pattern....485
Implementing the Adapter pattern....486
Using an Adapter to provide a necessary interface to an existing class....487
Using an Adapter as a wrapper....498
Summary....502
Questions....502
Chapter 19: Using the Singleton Pattern....504
Technical requirements....505
Understanding the Singleton pattern....505
Implementing the Singleton pattern....506
Using the simple implementation....507
Using the more robust paired-class implementation....511
Using a registry to allow many classes to utilize Singleton....522
Summary....523
Questions....524
Chapter 20: Removing Implementation Details Using the pImpl Pattern....526
Technical requirements....527
Understanding the pImpl pattern....527
Implementing the pImpl pattern....528
Organizing file and class contents to apply the pattern basics....529
Improving the pattern with a unique pointer....538
Understanding pImpl pattern trade-offs....542
Summary....544
Questions....545
Assessments....546
Chapter 1 – Understanding Basic C++ Assumptions....546
Chapter 2 – Adding Language Necessities....547
Chapter 3 – Indirect Addressing: Pointers....547
Chapter 4 – Indirect Addressing: References....548
Chapter 5 – Exploring Classes in Detail....548
Chapter 6 – Implementing Hierarchies with Single Inheritance....549
Chapter 7 – Utilizing Dynamic Binding through Polymorphism....549
Chapter 8 – Mastering Abstract Classes....549
Chapter 9 – Exploring Multiple Inheritance....549
Chapter 10 – Implementing Association, Aggregation, and Composition....550
Chapter 11 – Handling Exceptions....550
Chapter 12 – Friends and Operator Overloading....550
Chapter 13 – Working with Templates....551
Chapter 14 – Understanding STL Basics ....551
Chapter 15 – Testing Classes and Components....551
Chapter 16 – Using the Observer Pattern....551
Chapter 17 – Applying the Factory Pattern....552
Chapter 18 – Applying the Adapter Pattern....552
Chapter 19 – Using the Singleton Pattern....553
Chapter 20 – Removing Implementation Details Using the pImpl Pattern....553
Why subscribe?....554
Other Books You May Enjoy....555
Index....558
Companies need to utilize C++ for its speed. However, object-oriented software design leads to code which is easier to modify and maintain. Understanding how to utilize C++ as an object-oriented language is essential. Programming in C++ won't guarantee OOP – one must understand OO concepts and how they map to C++ language features as well as OO programming techniques. Additionally, programmers will want to harness additional skills beyond OOP to make code more generic, more robust, as well as employ well-tested, creative solutions which can be found in popular design patterns.
A programmer who learns how to use C++ as an object-oriented language will become a valuable C++ developer. A C++ programmer without OOP understanding and skills is one whose code will be difficult to maintain, modify or to understand by anyone else. Becoming an OO programmer in C++ is a valuable skill desired by companies needing to utilize this language.
This book has step-by-step explanations of essential OO concepts, paired with practical examples in code and often with diagrams so you can truly understand how and why things work. Self-assessment questions are available to test your skills.
This book first provides the necessary building blocks of skills (which may not be objectoriented), which provide an essential foundation on which the OO essentials will be built. Next, OO concepts will be described and paired with language features as well as coding techniques so that you can understand how to use C++ as an OOP language successfully. Additionally, more advanced skills are added to augment the programmer's repertoire, including friend function/classes, operator overloading, templates (to build more generic code), exception handling (to build robust code), STL basics, as well as design patterns and idioms.
By the end of this book, you will understand both essential and advanced OO concepts and how to implement these concepts in C++. You will learn not just how to use C++, but how to use it as an OOP language. You will additionally understand ways to make more robust and easily maintainable code, as well as understand how to employ well tested design patterns as part of your programming repertoire.