Chapter 2: User-Defined Types
2.1
We call the types that can be built from the fundamental types, the const modifier, and the declarator operators built-in types. C++ augments the built-in types and operations with a sophisticated set of abstraction mechanisms out of which programmers can build such high-level facilities.
Types built out of other types using C++'s abstraction mechanisms are called user-defined types. Tyhey are referred to as classes and enumerations. User defined types can be built out of both built-in types and other user-defined types.
2.2
The first step in building a new type is often to organize the elements it needs into a data structured, a struct.
The new operator allocates memory from an area called the free store (a.k.a. dynamic memory or heap). Objects allocated on the free store are indepenent of the scope from which they are created and "live" until they are destroyed using the delete operator.
2.3
Having the data specified separately from the operations on it has advantages, such as the ability to use the data in arbitrary ways. However, a tighter connection between the representation and the operations is needed for a user-defined type to have all the properties expected of a "real type". In particular, we often want to keep the representation inaccessible to users so as to ease use, guarantee consistent use of the data, and allow us to later improve the representation. We have to distinguish between the interface to a type(to be used by all) and its implementation(which has access to the otherwise inaccessible data). The interface is defined by the public members of a class, and private members are accessible only through that interface.
Using vector as an example: the Vector object is a "handle" containing a pointer to the elements (elem) and the number of elements (sz), and a Vector object can have a different number of elements at different times. However, the Vector object itself is always the same size. This is the basic technique ofor handling varying amounts of information in C++: a fixed-size handle referring to a variable amount of data "elsewhere".
A member function with the same name as its class is called a constructor. A constructor is guaranteed to be used to initialize objects of its class.
There is no fundamental differece between a struct and a class; a struct is simply a class with members public by default.
2.4
A union is a struct in which all members are allocated at the same address so that the union occupies only as much space as its largest members. The language doesn't keep track of which kind of value is held by a union, so the programmer must do that.
Maintaining the correspondence between a type field and the type held in a union is error-prone. At the application level, abstractions relying on such tagged unions are common and useful. The use of "naked" unions is best minimized.
The standard library type, variant, can be used to eliminate most direct uses of unions. For many uses, a variant is simpler and safer to use than a union.
2.5
Note that enum class enumerators are in the scope of their enum class, so that they can be used repeatedly in different enum classes without confusion. Similarly, we cannot implicitly mix Color enum with integer values.
By default, an enum class has only assignment, initialization, and comparison defined. However, an enumeration is a user-defined type, so we can define operators for it.
If you don't want to explicitly qualify enumerator names and want enumerator values to be ints, you can remove the class from enum class to get "plain" enum. The enumerators from a "plain" enum are entered into the same scope as the name of their enum and implicitly converts to their integer value.