![Page 1: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/1.jpg)
EGR 2261 Unit 10Records (structs)
Read Malik, Chapter 9. Homework #10 and Lab #10 due next
week. Quiz next week.
![Page 2: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/2.jpg)
Chapter 9:Records (structs)
![Page 3: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/3.jpg)
Objectives
• In this chapter, you will:– Learn about records (structs)– Examine various operations on a struct– Manipulate data using a struct– Learn about the relationship between a struct
and functions– Examine the difference between arrays and structs
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 4: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/4.jpg)
Objectives (cont’d.)
– Discover how arrays are used in a struct– Learn how to create an array of struct items– Learn how to create structs within structs
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 5: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/5.jpg)
Records (structs)
• struct: collection of a fixed number of components (members), accessed by name– Members may be of different types
• Syntax:
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 6: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/6.jpg)
Records (structs) (cont’d.)
• A struct is a definition, not a declaration– Must declare a variable of that type to use it
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 7: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/7.jpg)
Records (structs) (cont’d.)
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 8: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/8.jpg)
Accessing struct Members
• Syntax to access a struct member:
• The dot (.) is called the member access operator
8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 9: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/9.jpg)
Accessing struct Members (cont’d.)
• To initialize the members of newStudent:newStudent.GPA = 0.0;newStudent.firstName = "John";newStudent.lastName = "Brown";
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 10: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/10.jpg)
Assignment
• Value of one struct variable can be assigned to another struct variable of the same type using an assignment statement
• The statement:student = newStudent;
copies the contents of newStudent into student
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 11: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/11.jpg)
Assignment (cont’d.)
• The assignment statement:student = newStudent;
is equivalent to the following statements:student.firstName = newStudent.firstName;student.lastName = newStudent.lastName;student.courseGrade = newStudent.courseGrade;student.testScore = newStudent.testScore;student.programmingScore = newStudent.programmingScore;student.GPA = newStudent.GPA;
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 12: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/12.jpg)
Comparison (Relational Operators)
• Compare struct variables member-wise– No aggregate relational operations allowed
• To compare the values of student and newStudent:
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 13: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/13.jpg)
Input/Output
• No aggregate input/output operations on a struct variable
• Data in a struct variable must be read or written one member at a time
• Example: output newStudent contents
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 14: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/14.jpg)
struct Variables and Functions
• A struct variable can be passed as a parameter by value or by reference
• A function can return a value of type struct
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 15: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/15.jpg)
Arrays versus structs
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 16: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/16.jpg)
Arrays in structs
• Two items are associated with a list: – Values (elements)– Length of the list
• Define a struct containing both items:
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 17: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/17.jpg)
Arrays in structs (cont’d.)
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 18: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/18.jpg)
Arrays in structs (cont’d.)
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 19: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/19.jpg)
structs in Arrays
• Example:
19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 20: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/20.jpg)
structs in Arrays (cont’d.)
20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 21: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/21.jpg)
structs within a struct
21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 22: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/22.jpg)
Summary
• struct: collection of a fixed number of components
• Components can be of different types– Called members– Accessed by name
• struct is a reserved word• No memory is allocated for a struct
– Memory when variables are declared
22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
![Page 23: EGR 2261 Unit 10 Records ( struct s) Read Malik, Chapter 9. Homework #10 and Lab #10 due next week. Quiz next week](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649e055503460f94af1dad/html5/thumbnails/23.jpg)
Summary (cont’d.)
• Dot (.) operator: member access operator– Used to access members of a struct
• The only built-in operations on a struct are the assignment and member access
• Neither arithmetic nor relational operations are allowed on structs
• A struct can be passed by value or reference• A function can return a value of type struct• structs can be members of other structs
23C++ Programming: From Problem Analysis to Program Design, Seventh Edition