|
|
|
|
|
|
|
In the last chapter, we examined the concept of a data type and looked at how to define simple data types. In this chapter, we expand the definition of a data type to include structured types, which are collections of components given a single name. |
|
|
|
|
|
|
|
|
Sometimes it is necessary to show relationships among different variables or to store and reference variables as a group. This is difficult to do if each variable is named individually. For example, if a set of individually named values must be printed in reverse order, all the values must be read and saved before the last one can be printed. If there were 1000 values, we would have to declare 1000 individual variables to hold the values, and write 1000 different I/O statements to input and output the values-an incredibly tedious task! A one-dimensional array is a structured data type that allows us to program operations of this kind with ease. |
|
|
|
|
|
|
|
|
In this chapter, we discuss structured data types in general and examine the one-dimensional array data type in detail. |
|
|
|
|
|
|
|
|
Simple Versus Structured Data Types |
|
|
|
|
|
|
|
|
In Chapter 10, we examined simple, or atomic, data types. A value in a simple type is a single data item; it cannot be broken down into component parts. For example, each int value is a single integer number and cannot be further decomposed. In contrast, a structured data type is one in which each value is a collection of component data items. The entire collection is given a single name, yet each component can still be accessed individually. An example of a structured data type in C++ is the ifstream type, used for creating and manipulating input file streams. When you declare a variable inFile to be of type ifstream, inFile does not represent just one data value; it represents an entire collection of data items in an input file. But each of the components in the file can be accessed individually (by performing an input operation). |
|
|
|
 |
|
 |
|
|
Structured Data Type A collection of components whose organization is characterized by the method used to access individual components. The allowable operations on a structured data type include the storage and retrieval of individual components. |
|
|
|
|
|
|
|
|
Simple data types, both built-in and user-defined, are the building blocks for structured types. A structured type gathers together a set of component |
|
|
|
|
|