|
|
|
|
|
|
|
In the last three chapters, we looked in depth at a homogeneous structured data type called an array. We discussed common algorithms that are applied to arrays: sorting, linear searching, and binary searching. We added a data structures section to our top-down design. Clearly, how we choose to represent our data is an important aspect of the programming process. |
|
|
|
|
|
|
|
|
Although the array is an extremely useful data structure, it can be used only when the components are all the same data type. In this chapter, we examine a heterogeneous (nonhomogeneous) structured data type called a record. The components of a record do not have to be of the same data type, and they are accessed by name rather than by relative position. |
|
|
|
|
|
|
|
|
The last chapter closed with a discussion of how to choose a data structure. We continue this discussion at the end of this chapter, adding the record data type to our list of possible choices. |
|
|
|
|
|
|
|
|
Records allow us to group related components together, regardless of their data types. Each component in a record is called a field of the record, and each field is given a name called the field name. C++ uses its own terminology with records. A record is called a structure, the fields of a record are called members of the structure, and each member has a member name. |
|
|
|
 |
|
 |
|
|
Record (Structure, in C++) A structured data type with a fixed number of components that are accessed by name, not by index. The components may be heterogeneous (of different types). |
|
|
|
 |
|
 |
|
|
Field (Member, in C++) A component of a record. |
|
|
|
|
|
|
|
|
In C++, record data types are most commonly declared according to the following syntax: |
|
|
|
|
|
|
|
|
where TypeName is an identifier giving a name to the data type, and MemberList is defined as |
|
|
|
|
|