Download presentation
Presentation is loading. Please wait.
1
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Homework #12 and Lab #12 due next week. Quiz next week. Handouts: Quiz 11, Unit 12 practice sheets.
2
Review: Partial Hierarchy of Data Types
Simple Data Types Integral (int, bool, char, …) Floating-Point (double, …) Enumeration Structured Data Types Arrays structs Classes Pointers structs and classes are programmer-defined data types.
3
What is a struct? A struct is a collection of a fixed number of components (called members). These members: May be of different types. Are accessed by name. In contrast, recall that an array is also a collection of a fixed number of components (called elements). But these elements: Must be of the same type. Are accessed by index number, not by name. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
4
This struct has eight members (style, numOfBedrooms, etc.).
Defining a struct Syntax for defining a struct: Example: -Do practice questions 1 and 2. This struct has eight members (style, numOfBedrooms, etc.). Ends with a semicolon. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
5
A struct is a Programmer-Defined Data Type
When you define a struct, you are creating a new data type that you can use just like any of C++'s built-in data types. In this sense they are similar to the enumeration types that you define using the keyword enum. But they’re much more powerful. Examples: Declaring a variable of type int: int myInt; Declaring a variable of type houseType: houseType myHouse;
6
Declaring a struct Variable
After you have defined a struct, you can declare variables of that type. This statement defines a new datatype, but it does not create a variable of that data type, and therefore does not allocate any memory. This statement creates a variable of the data type defined above, and therefore allocates memory. See next slide… C++ Programming: From Problem Analysis to Program Design, Seventh Edition
7
Declaring a struct Variable (cont’d.)
Typo in this figure (in 7th edition only): yearBuilt should not be listed twice. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
8
Accessing struct Members
Syntax to access a struct member: The dot (.) is called the member access operator. In contrast, recall that to access an array element you use the [] operator, as in myArray[2]. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
9
Accessing struct Members (cont’d.)
Example: assign values to some members of newHouse: newHouse.style = "Ranch"; newHouse.numOfBedrooms = 3; newHouse.yearBuilt = 1972; Ranch 3 Demo by single-stepping week12Struct.cpp. (Note that we define the struct outside of main().) -Then do practice question 3. 1972 C++ Programming: From Problem Analysis to Program Design, Seventh Edition
10
Input/Output You cannot do aggregate input/output operations on a struct variable cout << newHouse; //Error!! Instead, read or write the data in a struct one member at a time: cout << newHouse.style << " " << newHouse.numOfBedrooms << " " << newHouse.numOfBathrooms << " " << newHouse.numOfCarsGarage << " " << newHouse.yearBuilt; Demo with week12Struct.cpp. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
11
Assignment Aggregate assignment is allowed on structs. That is, you can use = to copy the value of one struct variable to another struct variable of the same type. Example: if house and newHouse have been previously declared as houseType variables, then house = newHouse; copies the contents of newHouse into house. Demo with week12StructAssignment.cpp. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
12
Assignment (cont’d.) The assignment statement on the previous slide:
house = newHouse; is equivalent to the following statements: house.style = newHouse.style; house.numOfBedrooms = newHouse.numOfBedrooms; house.numOfBathrooms = newHouse.numOfBathrooms; house.numOfCarsGarage = newHouse.numOfCarsGarage; house.yearBuilt = newHouse.yearBuilt; house.finishedSquareFootage = newHouse.finishedSquareFootage; house.price = newHouse.price; house.tax = newHouse.tax; C++ Programming: From Problem Analysis to Program Design, Seventh Edition
13
Comparison (Relational Operators)
You cannot do aggregate comparisons on struct variables. if (myHouse > yourHouse) //Error!! Instead, compare struct variables member-by-member: if (myHouse.style == yourHouse.style && myHouse.yearBuilt > yourHouse.yearBuilt) Demo with week12StructCompare.cpp. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
14
struct Variables and Functions
A struct variable can be passed as a function parameter by value or by reference. Example: void printHouse(houseType house) { cout << house.style << " " << house.numOfBedrooms << " " << house.numOfBathrooms << " " << house.numOfCarsGarage << " " << house.yearBuilt; } No &, so we’re passing by value, not by reference. Demo with week12StructFunctionParameter.cpp. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
15
struct Variables and Functions (cont'd.)
A function can return a struct-type value. Example: houseType createHouse(string style, int year) { houseType newHouse; newHouse.style = style; newHouse.yearBuilt = year; return newHouse; } Demo with week12FunctionReturn.cpp. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
16
Arrays and structs: A Comparison
On this slide I’ve erased the comment that says “(except strings)” in the Array column of the Input/output row because we skipped C-strings. Do practice question 4. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
17
Nesting Arrays and structs
As shown on the following slides, you can have: An array of structs A struct that contains an array A struct that contains another struct The key thing to remember is that you use the [] operator to access elements within an array, and you use the . operator to access members within a struct. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
18
An Array of structs Example. First, define a struct:
Then declare an array whose elements are employeeType variables…. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
19
An Array of structs (cont’d.)
employeeType employees[50]; //Create an array of 50 elements // of data type employeeType. employees[2].lastName = "Jones"; //Assign a value to this. Do practice question 5. This is similar to Lab12Scores. See example using this struct on pages 621 – 623. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
20
Nesting Arrays and structs
The previous example demonstrated an array of structs. As shown in the next example, a struct can also contain an array. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
21
A struct That Contains an Array
Suppose you’ve got an array of 1000 elements, but at any given time the number of these 1000 elements that you’re actually using will vary. You might want to define a struct containing the array together with an int that tells you how many elements are currently in use: C++ Programming: From Problem Analysis to Program Design, Seventh Edition
22
A struct That Contains an Array (cont’d.)
Given the definition of listType on the previous slide, we can declare a variable of type listType with this statement: listType intList; C++ Programming: From Problem Analysis to Program Design, Seventh Edition
23
A struct That Contains an Array (cont’d.)
Note combined use of [] and . operators. Do practice question 6. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
24
Nesting structs within a struct
The previous examples demonstrated an array of structs and a struct that contains an array. As shown in the next example, a struct can also contain other structs. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
25
structs within a struct
The figure on the next slide shows the result of the following steps: Step 1: Define several structs: Step 2: Define a struct that contains structs of the type defined in Step 1: Step 3: Declare a variable of the type defined in Step 2: Note that this employeeType struct is different from the employeeType struct defined several slides earlier. C++ Programming: From Problem Analysis to Program Design, Seventh Edition
26
structs within a struct
To set newEmployee’s last name: newEmployee.name.last = "Jones"; To display newEmployee’s salary: cout << newEmployee.salary; To display newEmployee’s zip code: cout << newEmployee.address.zip; Do practice question 7. Take a quick look at Lab12Scores (Programming Exercise 2 in Chapter 9). C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.