Chapter12 Malik
description
Transcript of Chapter12 Malik
![Page 1: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/1.jpg)
1
RecordsC++ Structs
Chapter 14
![Page 2: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/2.jpg)
2
What to do with records?
Declaring recordsAccessing
recordsAccessing the
field of a recordWhat is a union?Can records be in
arrays?
![Page 3: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/3.jpg)
3
Records
Recall that elements of arrays must all be of the same type
In some situations, we wish to group elements of different types
scores : 85 79 92 57 68 80 . . . scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99
employee R. Jones 123 Elm 6/12/55 $14.75 employee R. Jones 123 Elm 6/12/55 $14.75
![Page 4: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/4.jpg)
4
Records
RECORDS are used to group related components of different types
Components of the record are called fields
In C++– record called a struct (structure)– fields called members
employee R. Jones 123 Elm 6/12/55 $14.75 employee R. Jones 123 Elm 6/12/55 $14.75
![Page 5: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/5.jpg)
5
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; };
struct part_struct { char descrip [31], part_num [11]; float unit_price; int qty; };
![Page 6: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/6.jpg)
6
Declaring struct Variables
Given
Declare :
struct part_struct { char descrip [31], part_num [11]; float unit_price; int qty; };
struct part_struct { char descrip [31], part_num [11]; float unit_price; int qty; };
part_struct new_part, old_part;part_struct new_part, old_part;
Use struct name as a type.Use struct name as a type.
![Page 7: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/7.jpg)
7
Accessing Components
Use the name of the record the name of the member separated by a dot .
The dot is called the member selector
old_part.qty = 5;
cout << new_part.descrip;
![Page 8: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/8.jpg)
8
Aggregate Operations with Structures
Recall that arrays had none (except reference parameter)
Structures DO have aggregate operators– assignment statement =– parameter (value or reference)– return a structure as a function type
![Page 9: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/9.jpg)
9
Aggregate Operations with Structures
Limitations on aggregate operations– no I/O
– no arithmetic operations
– no comparisons
cout << old_part;cin >> new_part;
cout << old_part;cin >> new_part;
old_part = new_part + old_part;old_part = new_part + old_part;
if (old_part < new_part) cout << ...;
if (old_part < new_part) cout << ...;
![Page 10: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/10.jpg)
10
Aggregate Operations with Structures
struct variables must be compared 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) ...
![Page 11: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/11.jpg)
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.
![Page 12: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/12.jpg)
12
struct Variables and Functions
A struct variable can be passed as a parameter either by value or by reference.
A function can return a value of the type struct
Note example program fragment
![Page 13: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/13.jpg)
13
Contrast Arrays and structs
![Page 14: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/14.jpg)
14
Arrays of Records
First declare a struct (such as part_struct)
Then specify an array of that type
Access elements of the array, elements of the struct
part_struct part_list [50];part_struct part_list [50];
for (x = 0; x <50; x++) cout << _______________________;
for (x = 0; x <50; x++) cout << _______________________;
How do weprint all the
descrip fields?part_list[x].descrip
![Page 15: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/15.jpg)
15
Records with Arrays
Exampleconst arraySize = 1000;
struct listType
{
int elements[arraySize]; //array containing the list
int listLength; //length of the list
} See sample program
![Page 16: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/16.jpg)
16
Hierarchical Records
Defn => records where at least one of the components is, itself, a record
Example:
struct inventory_struct { part_struct part; int qty_sold, re_order_qty; vendor_struct vendor; };
struct inventory_struct { part_struct part; int qty_sold, re_order_qty; vendor_struct vendor; };
![Page 17: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/17.jpg)
17
Unions
defn => a struct that holds only one of its members at a time during program executionunion Qty_type { int count; float linear_ft; long cu_in; };
Qty_type how_many;
union Qty_type { int count; float linear_ft; long cu_in; };
Qty_type how_many;
At run time, how_manycontains either
•an int
•a float
•a long . . .
But … never all three
![Page 18: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/18.jpg)
18
Choosing Data Structures
Strive to group logical elements of a structure together– calls for hierarchical structures
Push details of entities down to lower levels of the structure
Data Abstraction <=> separation of logical peoperties of a data type from its implementation
![Page 19: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/19.jpg)
19
Testing and Debugging Hints
Declaration of a struct type must end with a semicolon ;
Be sure to specify the full member selector when referencing a component of a struct variable– don’t leave out the struct name
![Page 20: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/20.jpg)
20
Testing and Debugging
When using an array in a struct, the index goes at the end student_rec.scores[x]
When using an array of struct, the index goes after the struct name parts_list[x].qty
![Page 21: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/21.jpg)
21
Testing and Debugging
Process struct members separately … the only aggregate operations will be
Assignment =Parameter passing void do_it (part_struct part);
Function returnpart_struct blanked_part ( );
![Page 22: Chapter12 Malik](https://reader035.fdocuments.in/reader035/viewer/2022062217/5695cef01a28ab9b028be110/html5/thumbnails/22.jpg)
22
Testing and Debugging
Be careful using same member names in different struct types
Compiler keeps them separate OKHuman readers can easily confuse them
struct parts { int qty; . . . } ;
struct test_scores { int qty; . . . } ;