|
|
|
|
|
|
|
We can, of course, have an array of hierarchical records; inventory is such an array. We can access the year that the first machine was purchased using the following expression: |
|
|
|
|
|
|
|
|
inventory[0].purchaseDate.year |
|
|
|
|
|
|
|
|
And here is a code segment that prints out the ID number and the year of purchase of each machine that has a failure rate of more than 8 percent: |
|
|
|
|
|
|
|
|
for (counter = 0; counter < NUM_MACHINES; counter++)
if (inventory[counter].history.failRate > 0.08)
cout << inventory[counter].idNumber <<
<< inventory[counter].purchaseDate.year << endl; |
|
|
|
|
|
|
|
|
In Chapter 11, we presented a diagram (Figure 11-2) showing the structured types available in C++. We repeat this diagram in Figure 14-5. |
|
|
|
|
|
|
|
|
So far we have discussed two of the four structured types: arrays and structs. We now look briefly at union types. The fourth structured type the classis the topic of Chapter 15. |
|
|
|
|
|
|
|
|
In C++, a union is defined to be a struct that holds only one of its members at a time during program execution. Here is a declaration of a union type and a union variable: |
|
|
|
|
|
|
|
|
union WeightType
{
long wtInOunces;
int wtInPounds;
float wtInTons;
}; |
|
|
|
|
|
|
|
|
Figure 14-5
C++ Structured Types |
|
|
|
|
|