Abstract
In this chapter, we’ll introduce one of the most fundamental tools in the C++ programmer’s toolbox: classes. We’ll also present some ideas that are implicit in object-oriented programming and show how they are applied.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
In the context of object-oriented programming, you’ll find that the term encapsulation may refer to two related yet distinct notions. Some authors define encapsulation like we do, namely, as the bundling of data with the functions that operate on that data, while others define it as a language mechanism for restricting direct access to an object’s members. The latter is what we refer to as data hiding in the next subsection. Enough ink has been spilled debating which definition is right, so we will not go there (although obviously it’s ours!). When reading other texts or when discussing with your peers, just keep in mind that encapsulation is often used as a synonym for data hiding.
- 2.
Technically speaking, programming language theory distinguishes several different forms of polymorphism. What we refer to here as “polymorphism” is formally known as subtyping, subtype polymorphism, or inclusion polymorphism. Other forms of polymorphism that are also supported by C++ include parametric polymorphism (C++ function and class templates; see Chapters 10 and 17) and ad hoc polymorphism (function and operator overloading; see Chapters 8 and 13). In the world of object-oriented programming, however, the term “polymorphism” usually refers solely to subtyping, so that is also the terminology we adopt in this book.
- 3.
And to union types as well, technically, but we won’t cover those in this book. You’re better off using std::variant<> anyway in C++, a vocabulary type template similar to the std::optional<> and std::result<> types we discuss in Chapter 9.
- 4.
Some also use statements such as this->length = length; to circumvent such name clashes (the this pointer is discussed later in this chapter), but we prefer the more compact m_ convention.
- 5.
For a class that is defined in a header, however, in-class member function definitions are always (implicitly) inline. For classes defined in a module this no longer holds; a member function in a module is only inline if you explicitly mark it as such. Refer to Appendix A for more information on the differences between headers and modules.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2023 The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature
About this chapter
Cite this chapter
Horton, I., Van Weert, P. (2023). Defining Your Own Data Types. In: Beginning C++23. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-9343-0_12
Download citation
DOI: https://doi.org/10.1007/978-1-4842-9343-0_12
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-9342-3
Online ISBN: 978-1-4842-9343-0
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)