CSCI
152 Hierarchical (Nested) Structures
// GLOBAL TYPE
DECLARATIONS
1 struct Date
2 {
3 int month;
4 int day;
5 int year;
6 };
7 struct Machine
8 {
9 int id;
10
bool
serviceContract;
11
float failRate;
12
Date lastServiced;
13
int downDays;
14 Date purchaseDate;
15
float cost;
16
float deprValue;
17
};
// LOCAL VARIABLE DECLARATIONS
18
Date yesterday,
19
today;
20
Machine press;
21
Machine station [100];
The following statements demonstrate references to fields (fully qualified names must be used for all field identifiers):
22 cout <<
"Date: " << today.month
<< "/" << today.day
<< "/" << today.year
<< endl;
23 cout <<
"Machine # " << press.id << endl;
24 cout <<
"Purchase date: " << press.purchaseDate.month
<< "/"
25
<< press.purchaseDate.day
<< "/"
26
<< press.purchaseDate.year
<< endl;
27 cout <<
"Original cost = $" << setprecision(2) << press.cost << endl;
28 cout <<
"Depreciation value = $" << press.deprValue << endl;
29 cout <<
"Total down days since purchase = " << press.downDays << endl;
30 cout
<< "Last serviced: "
<< press.lastServiced.day << " "
31
<< press.lastServiced.month
<< " "
32
<< press.lastServiced.year
<< endl;
33 if (press.failRate > 0.10)
34
cout << "Excessive downtime\n";
35 if (! press.serviceContract) // or if (press.serviceContract == false)
36
cout << "Service contract has expired\n";
The only
defined operations for structure variables (the entire structure) of the same
type are
37 press.lastServiced = today;
is equivalent to:
38 // press.lastServiced.month
= today.month;
39 // press.lastServiced.day
= today.day;
40 // press.lastServiced.year
= today.year;
1 station[5]
= press;
is equivalent to:
2 station[5].id
= press.id;
3 station[5].serviceContract
= press.serviceContract;
4 station [5].failRate =
press.failRate;
5 station[5].lastServiced.month
= press.lastServiced.month;
6 station[5].lastServiced.day
= press.lastServiced.day;
7 station[5].lastServiced.year
= press.lastServiced.year;
8 station [5].downDays =
press.downDays;
9 station[5].purchaseDate.month
= press.purchaseDate.month;
10
station[5].purchaseDate.day = press.purchaseDate.day;
11 station[5].purchaseDate.year
= press.purchaseDate.year;
12
station [5].cost = press.cost;
13
station [5].deprValue = press.deprValue;
// Type declarations:
struct COURSE
{
string dept;
int
courseNum;
int
section;
char
letterGrade;
};
struct ADDRESS
{
string street;
string cityState;
string zipCode;
};
struct STUDENT
{
string name;
ADDRESS localAddress;
ADDRESS permanentAddress;
COURSE grades[10];
};
// Variable declarations:
COURSE myCourse;
ADDRESS myAddress;
STUDENT aStudent;
STUDENT students[100];
// To
refer to: Use
this name:
The number of my
course: myCourse.courseNum
My zip code: myAddress.zipCode
The name of the first
student: students[0].name
The initial of the
second student: students[1].name[0]
The fourth grade of the
tenth student: students[9].grades[3].letterGrade
The local address (a
record) of the last student: students[99].localAddress
The zip code of the local address of the first student: students[0].localAddress.zipCode