C++ is an extensible programming language, meaning programmers can extend it by creating new data types with structures, enumerations, unions, bit fields, and classes. Chapter 5 covers most of these constructs (saving classes for Chapter 9) but focuses on and takes its name from structures. Structures are similar to classes but are inherited from the older, non-object-oriented C programming language. Programmers create structures with the struct
keyword, creating a pattern grouping related data into easily managed units and forming an aggregate data type. The other topics covered in the chapter are either extensions to structures or different ways of creating simple data types.
Aggregate data consists of a set or group of related data items. Think of aggregate data as a basket. You can place many items in a basket and carry them or move them about by holding the basket's handle. In the same way, you can store many data items in aggregate data and move them around in a program as a unit by "holding" the aggregate's "handle." In the case of aggregate data, the "handle" that the program "holds" is the name of the aggregate variable or object. ZIP, RAR, TAR, and JAR files are familiar computer-related examples of aggregate data. Each file is a container holding other files, allowing us to move (e.g., copy or email) the contained files as a single entity. The zip or archive file's name is the "handle" the computer "holds" while moving and saving the contained files.
C++ programmers create aggregate data in three ways: structures, classes (Chapter 9), and arrays (Chapter 7). Just as users access the individual files in zip or archive files by their name, programmers access the data contained in structures and classes by their names. Throughout this chapter, we'll see how programmers name and access the individual data items in structures. Although arrays are structurally simpler than structures or classes, we delay their detailed exploration until Chapter 7.
Array | struct |
---|---|
struct
keyword. Imagine a structure named Person that contains a person's name, identification number, birth date, address, and phone number.
A type specifier is a name that uniquely identifies a data type sufficiently that the compiler can generate code to process it. The data type determines its size, how to interpret its bits, and what operations it supports. For example, Chapter 1 presented seven (eight if you count void) fundamental or primitive data types. The compiler inherently "knows" about the fundamental data types - their sizes, the meaning of their bit patterns, and the arithmetic operations they support. The names of these fundamental data types are type specifiers.
int counter; |
double average; |
Just as the compiler uses the fundamental data types to generate processing code, it also uses the names of programmer-created data types to determine how much memory to allocate for variables of those types and how to interpret the bits saved in them. Therefore, the names of programmer-created data types are also specifiers. Programmers create a new data type with a specification consisting of three parts: one of the keywords struct
, class, enum
, or typedef
, a unique name, and a description of the new type's subcomponents. Unions and bit-fields are structure extensions. Structures and classes create aggregate data types.
Specification | Variable Definitions |
---|---|
struct Person { string name; short id; string address; int phone; }; |
Person chair; Person president; Person vp; |