Chapter 9
Records (structs)
C++ Programming: Program Design Including Data Structures, Eighth Edition
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
1
product or service or otherwise on a password-protected website for classroom
Objectives (1 of 2)
• In this chapter, you will:
• Learn about records (structs)
• Examine various operations on a struct
• Explore ways to manipulate data using a struct
• Learn about the relationship between a struct and functions
• Examine the difference between arrays and structs
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 2
product or service or otherwise on a password-protected website for classroom use.
Objectives (2 of 2)
• Discover how arrays are used in a struct
• Learn how to create an array of struct items
• Learn how to create structs within a structs
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 3
product or service or otherwise on a password-protected website for classroom use.
Records (structs) (1 of 3)
• struct: a collection of a fixed number of components in which the
components are accessed by name
• The components may be of different types and are called the members of the
struct
• Syntax
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 4
product or service or otherwise on a password-protected website for classroom use.
Records (structs) (2 of 3)
• A struct is a definition, not a declaration
• Must declare a variable of that type to use it
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 5
product or service or otherwise on a password-protected website for classroom use.
Records (structs) (3 of 3)
FIGURE 9-1 struct newHouse
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 6
product or service or otherwise on a password-protected website for classroom use.
Accessing struct Members (1 of 2)
• Syntax to access a struct member:
• The dot (.) is called the member access operator
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 7
product or service or otherwise on a password-protected website for classroom use.
Accessing struct Members (2 of 2)
• To initialize the members of newStudent:
newStudent.GPA = 0.0;
newStudent.firstName = "John";
newStudent.lastName = "Brown";
FIGURE 9-2 struct newStudent
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 8
product or service or otherwise on a password-protected website for classroom use.
Assignment (1 of 2)
• 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
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 9
product or service or otherwise on a password-protected website for classroom use.
Assignment (2 of 2)
• 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;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 10
product or service or otherwise on a password-protected website for classroom use.
Comparison (Relational Operators)
• Compare struct variables member-wise
• No aggregate relational operations are allowed
• To compare the values of student and newStudent:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 11
product or service or otherwise on a password-protected website for classroom use.
Input/Output
• No aggregate input/output operations are allowed on a struct variable
• Data in a struct variable must be read or written one member at a time
• The following code would output newStudent contents:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 12
product or service or otherwise on a password-protected website for classroom use.
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
• The following function displays the contents a struct variable of type
studentType:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 13
product or service or otherwise on a password-protected website for classroom use.
Arrays versus structs
TABLE 9-1 Arrays vs. structs
Data Type Array struct
Arithmetic No No
Assignment No Yes
Input/output No (except strings) No
Comparison No No
Parameter passing By reference only By value or by reference
Function returning a value No Yes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 14
product or service or otherwise on a password-protected website for classroom use.
Arrays in structs (1 of 3)
• Two items are associated with a list:
• Values (elements)
• Length of the list
• Define a struct containing both items:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 15
product or service or otherwise on a password-protected website for classroom use.
Arrays in structs (2 of 3)
FIGURE 9-5 struct variable intList
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 16
product or service or otherwise on a password-protected website for classroom use.
Arrays in structs (3 of 3)
• Consider these statements and refer to the figure below showing the results
following execution of the statements:
FIGURE 9-6 intList after the statements in Lines 1 through 5 execute
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 17
product or service or otherwise on a password-protected website for classroom use.
structs in Arrays (1 of 2)
• Example
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 18
product or service or otherwise on a password-protected website for classroom use.
structs in Arrays (2 of 2)
employeeType employees[50]
• Declares the array employees of 50 components of type employeeType
FIGURE 9-7 Array of employees
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 19
product or service or otherwise on a password-protected website for classroom use.
structs within a struct
FIGURE 9-8 struct variable newEmployee
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 20
product or service or otherwise on a password-protected website for classroom use.
Quick Review (1 of 2)
• A struct is a collection of a fixed number of components
• Components of a struct can be of different types
• Called members
• Accessed by name
• struct is a reserved word
• No memory is allocated for a struct
• Memory is allocated only when variables are declared
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 21
product or service or otherwise on a password-protected website for classroom use.
Quick Review (2 of 2)
• In C++, the dot (.) operator is called the member access operator
• Used to access members of a struct
• The only built-in operations on a struct are the assignment and member
access operations
• 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
• A struct can be a member of another struct
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 22
product or service or otherwise on a password-protected website for classroom use.