Enums and Structs Flashcards
Program defined type / User defined type
give definition, examples and rules
A custom type we can create for use in our own programs.
The enumerated types and class types (including structs, classes and unions) allow for creation of program defined-types.
Program defined types must be defined before they are used
Type definition
Exempt from?
The definition of a program defined type. They are exempt from the one-definition rule
Enumeration / Enumerated Type / Enum
a compound data type where every possible value is defined as a symbolic constant
enumerations are distinct types, meaning the compiler can differentiate them from other types (unlike type aliases)
Name for the symbolic constants inside an enum
enumerator
Unscoped enumerations
they put their enumerator names in the same scope as the enumeration definition itself (as opposed to creating a new scope region like a namespace does).
Will implicitly convert to integral values
scoped enumerations
similar to unscoped enumerations but
won’t implicitly convert to integers
enumerators are only placed into the scope region of the enumeration (not into the scope region where the enumeration is defined)
struct
program defined data type that allows us to bundle multiple variables together in a single type.
data members / member variables
variables that are part of a struct or class
member selection operator
operator.
allows you to access a specific member variable for structs or refs to structs
member selection from pointer operator
operator->
allows you to access a specific member variable for pointers to structs
aggregate data type / aggregate
any type that can contain multiple data members
arrays and structs with only data members
aggregate initialisation
directly initialises members of aggregates with list of comma seperated values
e.g.
Triad t { 1, 2, 3 };
Initialises each member of the struct in order of declaration
memberwise initialization
each member in the struct is initialized in the order of declaration.
Designated initializers
Add a code example here
C++20
allow you to explicitly define which initialization values map to which members. The members must be initialized in the order in which they are declared in the struct, otherwise an error will result
Car myCar = { .make “Toyota”, .year = 2020 };
non-static member initialization
When we define a struct (or class) type, we can provide a default initialization value for each member as part of the type definition