Chapter 12
Chapter 12
Chapter 14
records Accessing the field of a record What is a union? Can records be in arrays?
Records
Recall that elements of arrays must all
Records
RECORDS are used to group related
Records
C++ struct structured data type fixed number of components elements accessed by name, not by index components may be of different types
struct part_struct { char descrip [31], part_num [11]; float unit_price; int qty; };
Declare :
Accessing Components
Use the name of the record
no arithmetic operations
old_part = new_part + old_part;
no comparisons
10
member-wise. To compare the values of student and newStudent, you must compare them member-wise, as follows:
if(student.firstName == newStudent.firstName && student.lastName == newStudent.lastName) ...
11
Input/Output
There are no aggregate input/output operations on struct. Data in a struct variable must be
read one member at a time. Contents of a struct must be written one member at a time.
12
parameter either by value or by reference. A function can return a value of the type struct
Note example program fragment
13
14
Arrays of Records
First declare a struct (such as
part_struct)
Then specify an array of that type
part_struct part_list [50];
of the struct
How do we print all the descrip fields? for (x = 0; x <50; x++) part_list[x].descrip cout << _______________________;
15
struct listType { int elements[arraySize]; //array containing the list int listLength; //length of the list }
16
Hierarchical Records
Defn => records where at least one of
17
Unions
defn => a struct that holds only one of
how_many;
a long
...
18
structure together
calls for hierarchical structures
levels of the structure Data Abstraction <=> separation of logical peoperties of a data type from its implementation
19
with a semicolon ; Be sure to specify the full member selector when referencing a component of a struct variable
dont leave out the struct name
20
21
the only aggregate operations will be Assignment = Parameter passing void do_it (part_struct part);
Function return
part_struct blanked_part ( );
22
them