5.1. Introduction To Structures

Time: 00:03:04 | Download: Large, Large (CC) Small | Streaming, Streaming (CC) | Slides (PDF)

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

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
An array represented as a large rectangle subdivided into eight smaller rectangles. The smaller rectangles represent the eight array elements. The illustration includes the array index values, 0, 1, 2, ..., 7 adjacent to the smaller element rectangles. A structure represented as a large rectangle with five smaller rectangles inside. The small rectangles represent the structure's data items: Name, ID, birth date, address, and phone number.
Aggregate data examples. Arrays are a simple aggregate data structure provided directly by many programming languages as an extension to fundamental or built-in data types. They consist of a sequence of same-size elements accessed by their position or index in the sequence. Programmers create more complex data structures with variously sized fields with the struct keyword. Imagine a structure named Person that contains a person's name, identification number, birth date, address, and phone number.

Type Specifiers

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;
Fundamental type specifier examples. Type specifiers (highlighted in yellow) are a part of every variable definition or declaration.

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;

Programmer-created data types. Structures and classes are the most common ways for programmers to create a new data type. Once created, the structure and class names become type specifiers that programmers use to define new variables. The specifications must appear in every source code file using the name, so programmers typically put the specifications in header files and #include the headers in the source code files.