Embrace object-oriented programming and explore language complexities, design patterns, and smart programming techniques using this hands-on guide with C++ 20 compliant examples
Key Features
Apply object-oriented design concepts in C++ using direct language features and refined programming techniques
Discover sophisticated programming solutions with nuances to become an efficient programmer
Explore design patterns as proven solutions for writing scalable and maintainable C++ software
Book Description
Even though object-oriented software design enables more easily maintainable code, companies choose C++ as an OO language for its speed. Object-oriented programming in C++ is not automatic – it is crucial to understand OO concepts and how they map to both C++ language features and OOP techniques. Distinguishing your code by utilizing well-tested, creative solutions, which can be found in popular design patterns, is crucial in today's marketplace. This book will help you to harness OOP in C++ to write better code.
Starting with the essential C++ features, which serve as building blocks for the key chapters, this book focuses on explaining fundamental object-oriented concepts and shows you how to implement them in C++. With the help of practical code examples and diagrams, you'll learn how and why things work. The book's coverage furthers your C++ repertoire by including templates, exceptions, operator overloading, STL, and OO component testing. You'll discover popular design patterns with in-depth examples and understand how to use them as effective programming solutions to solve recurring OOP problems.
By the end of this book, you'll be able to employ essential and advanced OOP concepts to create enduring and robust software.
What you will learn
Quickly learn core C++ programming skills to develop a base for essential OOP features in C++
Implement OO designs using C++ language features and proven programming techniques
Understand how well-designed, encapsulated code helps make more easily maintainable software
Write robust C++ code that can handle programming exceptions
Design extensible and generic code using templates
Apply operator overloading, utilize STL, and perform OO component testing
Examine popular design patterns to provide creative solutions for typical OO problems
Who this book is for
Programmers wanting to utilize C++ for OOP will find this book essential to understand how to implement OO designs in C++ through both language features and refined programming techniques while creating robust and easily maintainable code. This OOP book assumes prior programming experience; however, if you have limited or no prior C++ experience, the early chapters will help you learn essential C++ skills to serve as the basis for the many OOP sections, advanced features, and design patterns.
Table of Contents
Understanding Basic C++ Assumptions
Adding Language Necessities
Indirect Addressing: Pointers
Indirect Addressing: References
Exploring Classes in Detail
Implementing Hierarchies with Single Inheritance
Utilizing Dynamic Binding through Polymorphism
Mastering Abstract Classes
Exploring Multiple Inheritance
Implementing Association, Aggregation, and Composition
Handling Exceptions
Friends and Operator Overloading
Working with Templates
Understanding STL Basics
Testing Classes and Components
Using the Observer Pattern
Applying the Factory Pattern
Applying the Adapter Pattern
Using the Singleton Pattern
Removing Implementation Details Using the pImpl Pattern
Making C++ Safer
Author(s): Dorothy R. Kirk
Edition: 1
Publisher: Packt
Year: 2022
Language: English
Tags: c++ c++14 c++17 c++20 c++23 embedded systems low-level games programming compilers iot operating systems linux unix windows
Cover
Title Page
Copyright and Credits
Dedication
Contributors
Table of Contents
Preface
Part 1: C++ Building Block Essentials
Chapter 1: Understanding Basic C++ Assumptions
Technical requirements
Reviewing basic C++ language syntax
Comment styles
Variable declarations and standard data types
Variables and array basics
Recapping basic I/O
The iostream library
Basic iostream manipulators
Revisiting control structures, statements, and looping
Control structures – if, else if, and else
Looping constructs – while, do while, and for loops
Reviewing C++ operators
Revisiting function basics
Reviewing user defined type basics
struct
typedef and “using” alias declaration
class
enum and strongly-typed enum
Recapping namespace basics
Summary
Questions
Chapter 2: Adding Language Necessities
Technical requirements
Using the const and constexpr qualifiers
const and constexpr variables
const qualification with functions
Working with function prototypes
Defining function prototypes
Naming arguments in function prototypes
Adding default values to function prototypes
Prototyping with different default values in different scopes
Understanding function overloading
Learning the basics of function overloading
Eliminating excessive overloading with standard type conversion
Ambiguities arising from function overloading and type conversion
Summary
Questions
Chapter 3: Indirect Addressing – Pointers
Technical requirements
Understanding pointer basics and memory allocation
Revisiting pointer basics
Using the address-of and dereference operators
Using operators new() and delete()
Creating and using pointers to user defined types
Allocating and deallocating arrays at runtime
Dynamically allocating single dimension arrays
Dynamically allocating 2-D arrays – an array of pointers
Dynamically allocating 2-D arrays – pointers to pointers
Dynamically allocating N-D arrays – pointers to pointers to pointers
Using pointers with functions
Passing pointers as arguments to functions
Using pointers as return values from functions
Using the const qualifier with pointers
Using pointers to constant objects
Using constant pointers to objects
Using constant pointers to constant objects
Using pointers to constant objects as function arguments and as return types from functions
Using pointers to objects of unspecified types
Creating void pointers
Looking ahead to smart pointers for safety
Summary
Questions
Chapter 4: Indirect Addressing – References
Technical requirements
Understanding reference basics
Declaring, initializing, and accessing references
Referencing existing objects of user defined types
Using references with functions
Passing references as arguments to functions
Using references as return values from functions
Using the const qualifier with references
Using references to constant objects
Using pointers to constant objects as function arguments and as return types from functions
Realizing underlying implementation and restrictions
Understanding when we must use pointers instead of references
Summary
Questions
Part 2: Implementing Object-Oriented Concepts in C++
Chapter 5: Exploring Classes in Detail
Technical requirements
Introducing object-oriented terminology and concepts
Understanding object-oriented terminology
Understanding object-oriented concepts
Understanding class and member function basics
Examining member function internals; the “this” pointer
Using access labels and access regions
Understanding constructors
Applying constructor basics and overloading constructors
Constructors and in-class initializers
Creating copy constructors
Creating conversion constructors
Understanding destructors
Applying destructor basics and proper usage
Applying qualifiers to data members and member functions
Adding inline functions for potential efficiency
Adding const data members and the member initialization list
Using const member functions
Utilizing static data members and static member functions
Summary
Questions
Chapter 6: Implementing Hierarchies with Single Inheritance
Technical requirements
Expanding object-oriented concepts and terminology
Deciphering generalization and specialization
Understanding single inheritance basics
Defining base and derived classes and accessing inherited members
Examining inherited access regions
Specifying a class as final
Understanding inherited constructors and destructors
Implicit constructor and destructor invocations
Usage of member initialization list to select a base class constructor
Putting all the pieces together
Implementation inheritance – changing the purpose of inheritance
Modifying access labels in the base class list by using protected or private base classes
Creating a base class to illustrate implementation inheritance
Using a private base class to implement one class in terms of another
Using a protected base class to implement one class in terms of another
Summary
Questions
Chapter 7: Utilizing Dynamic Binding through Polymorphism
Technical requirements
Understanding the OO concept of polymorphism
Implementing polymorphism with virtual functions
Defining virtual functions and overriding base class methods
Generalizing derived class objects
Utilizing virtual destructors
Putting all the pieces together
Considering function hiding
Understanding dynamic binding
Comprehending runtime binding of methods to operations
Interpreting the v-table in detail
Summary
Questions
Chapter 8: Mastering Abstract Classes
Technical requirements
Understanding the OO concept of an abstract class
Implementing abstract classes with pure virtual functions
Creating interfaces
Generalizing derived class objects as abstract types
Putting all the pieces together
Summary
Questions
Chapter 9: Exploring Multiple Inheritance
Technical requirements
Understanding multiple inheritance mechanics
Examining reasonable uses for multiple inheritance
Supporting Is-A and mix-in relationships
Supporting Has-A relationships
Creating a diamond-shaped hierarchy
Utilizing virtual base classes to eliminate duplication
Considering discriminators and alternate designs
Summary
Questions
Chapter 10: Implementing Association, Aggregation, and Composition
Technical requirements
Understanding aggregation and composition
Defining and implementing composition
Defining and implementing a generalized aggregation
Understanding association
Implementing association
Utilizing backlink maintenance and reference counting
Summary
Questions
Part 3: Expanding Your C++ Programming Repertoire
Chapter 11: Handling Exceptions
Technical requirements
Understanding exception handling
Utilizing exception handling with try, throw, and catch
Exploring exception handling mechanics with typical variations
Utilizing exception hierarchies
Using standard exception objects
Creating customized exception classes
Creating hierarchies of user defined exception types
Summary
Questions
Chapter 12: Friends and Operator Overloading
Technical requirements
Understanding friend classes and friend functions
Using friend functions and friend classes
Making access safer when using friends
Deciphering operator overloading essentials
Implementing operator functions and knowing when friends might be necessary
Summary
Questions
Chapter 13: Working with Templates
Technical requirements
Exploring template basics to genericize code
Examining the motivation for templates
Understanding template functions and classes
Creating and using template functions
Creating and using template classes
Examining a full program example
Making templates more flexible and extensible
Adding operator overloading to further genericize template code
Summary
Questions
Chapter 14: Understanding STL Basics
Technical requirements
Surveying the contents and purpose of the STL
Understanding how to use essential STL containers
Using STL list
Using STL iterator
Using STL vector
Using STL deque
Using STL stack
Using STL queue
Using STL priority queue
Examining STL map
Examining STL map using a functor
Customizing STL containers
Summary
Questions
Chapter 15: Testing Classes and Components
Technical requirements
Contemplating OO testing
Understanding the canonical class form
Default constructor
Copy constructor
Overloaded assignment operator
Virtual destructor
Move copy constructor
Move assignment operator
Bringing the components of canonical class form together
Ensuring a class is robust
Creating drivers to test classes
Testing related classes
Testing classes related through inheritance, association, or aggregation
Testing exception handling mechanisms
Embedding exception handling in constructors to create robust classes
Summary
Questions
Part 4: Design Patterns and Idioms in C++
Chapter 16: Using the Observer Pattern
Technical requirements
Utilizing design patterns
Understanding the Observer pattern
Implementing the Observer pattern
Creating an Observer, Subject, and domain-specific derived classes
Summary
Questions
Chapter 17: Applying the Factory Pattern
Technical requirements
Understanding the Factory Method pattern
Implementing the Factory Method pattern
Including the Factory Method in the Product class
Creating an Object Factory class to encapsulate the Factory Method
Summary
Questions
Chapter 18: Applying the Adapter Pattern
Technical requirements
Understanding the Adapter pattern
Implementing the Adapter pattern
Using an Adapter to provide a necessary interface to an existing class
Using an Adapter as a wrapper
Summary
Questions
Chapter 19: Using the Singleton Pattern
Technical requirements
Understanding the Singleton pattern
Implementing the Singleton pattern
Using a simple implementation
An alternate simple implementation
Using a more robust paired-class implementation
Using a registry to allow many classes to utilize Singleton
Summary
Questions
Chapter 20: Removing Implementation Details Using the pImpl Pattern
Technical requirements
Understanding the pImpl pattern
Implementing the pImpl pattern
Organizing file and class contents to apply the pattern basics
Improving the pattern with a unique pointer
Understanding pImpl pattern trade-offs
Summary
Questions
Part 5: Considerations for Safer Programming in C++
Chapter 21: Making C++ Safer
Technical requirements
Revisiting smart pointers
Using smart pointers – unique
Using smart pointers – shared
Using smart pointers – weak
Exploring a complementary idea – RAII
Embracing additional C++ features promoting safety
Revisiting range for loops
Using auto for type safety
Preferring STL for simple containers
Applying const as needed
Considering thread safety
Multithreaded programming overview
Multithreaded programming scenarios
Utilizing core programming guidelines
Examining guideline essentials
Adopting a programming guideline
Understanding resources for programming safely in C++
Summary
Assessments
Chapter 1, Understanding Basic C++ Assumptions
Chapter 2, Adding Language Necessities
Chapter 3, Indirect Addressing: Pointers
Chapter 4, Indirect Addressing: References
Chapter 5, Exploring Classes in Detail
Chapter 6, Implementing Hierarchies with Single Inheritance
Chapter 7, Utilizing Dynamic Binding through Polymorphism
Chapter 8, Mastering Abstract Classes
Chapter 9, Exploring Multiple Inheritance
Chapter 10, Implementing Association, Aggregation, and Composition
Chapter 11, Handling Exceptions
Chapter 12, Friends and Operator Overloading
Chapter 13, Working with Templates
Chapter 14, Understanding STL Basics
Chapter 15, Testing Classes and Components
Chapter 16, Using the Observer Pattern
Chapter 17, Applying the Factory Pattern
Chapter 18, Applying the Adapter Pattern
Chapter 19, Using the Singleton Pattern
Chapter 20, Removing Implementation Details Using the pImpl Pattern
Index
About Packt
Other Books You May Enjoy