EGR 2261 Unit 12 structs Read Malik, Chapter 9.

Slides:



Advertisements
Similar presentations
Structures Spring 2013Programming and Data Structure1.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 11: Records (structs)
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 8 Arrays and Strings
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 Structured Data.
EGR 2261 Unit 10 Records (structs)
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Chapter Structured Data 11. Combining Data into Structures 11.2.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Structures and Classes
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
EGR 2261 Unit 10 Two-dimensional Arrays
EGR 2261 Unit 9 One-dimensional Arrays
11 Chapter Structured Data
Objectives In this chapter, you will:
CS1010 Programming Methodology
Pointers, Enum, and Structures
Chapter 7: Introduction to Classes and Objects
Chapter 10: Void Functions
EGR 2261 Unit 4 Control Structures I: Selection
MIS 120 Structures.
Records C++ Structs Chapter 10.1.
Student Book An Introduction
C++ Structs.
Records C++ Structs Chapter 12.
CS 1430: Programming in C++.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
Pointers, Dynamic Data, and Reference Types
Review for Final Exam.
Chapter 11: Structured Data.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Review for Final Exam.
Chapter 11: Records (structs)
Standard Version of Starting Out with C++, 4th Edition
C++ Array 1.
Pointers, Dynamic Data, and Reference Types
Chapter 9: Records (structs)
Structures, Unions, and Enumerations
Week 9 - Monday CS222.
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

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.

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.

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

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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