Chapter 11: Records (structs)

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Structure.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Structures Spring 2013Programming and Data Structure1.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 10: Records ( structs )
Chapter 11: Records (structs)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
EGR 2261 Unit 10 Records (structs)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10: Records (structs)
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
Chapter 10: Records (structs)
Edited from Powerpoint Slides provided by Thomson Learning
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Structures (aka records, structs) Textbook Chapter 11 sections:
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records ( struct s)
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
C++ Programming:. Program Design Including
Chapter 13: Overloading and Templates
Chapter 7: User-Defined Functions II
REVISION CSC 138 – Structured Programming One Dimensional Array
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS1010 Programming Methodology
Computer Programming BCT 1113
About the Presentations
MIS 120 Structures.
Records C++ Structs Chapter 10.1.
Chapter 15: Overloading and Templates
C++ Structs.
Records C++ Structs Chapter 12.
Structures.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Heterogeneous aggregate datatypes
Structs And Arrays.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9: Records (structs)
Review for Final Exam.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Chapter 7: User-Defined Functions II
Review for Final Exam.
Chapter 9: Records (structs)
Presentation transcript:

Chapter 11: Records (structs) C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records (structs) Prepared by: Malak Abdullah

Objectives 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 Discover how arrays are used in a struct Learn how to create an array of struct items Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2

Records (structs) struct: collection of a fixed number of components (members), accessed by name A struct is a definition, not a declaration Members may be of different types Syntax: Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3

Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Accessing struct Members The syntax for accessing a struct member is: The dot (.) is an operator, called the member access operator Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5

Accessing struct Members (continued) To initialize the members of newStudent: newStudent.GPA = 0.0; newStudent.firstName = "John"; newStudent.lastName = "Brown"; Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6

Accessing struct Members (continued) More examples: cin >> newStudent.firstName; cin>>newStudent.testScore>>newStudent.programmingScore; score = (newStudent.testScore + newStudent.programmingScore) / 2; if (score >= 90) newStudent.courseGrade = 'A'; else if (score >= 80) newStudent.courseGrade = 'B'; else if (score >= 70) newStudent.courseGrade = 'C'; else if (score >= 60) newStudent.courseGrade = 'D'; else newStudent.courseGrade = 'F'; Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7

Example: Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition

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 Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 9

Assignment (continued) 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; Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 10

Example: Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Comparison (Relational Operators) Compare struct variables member-wise No aggregate relational operations allowed To compare the values of student and newStudent: Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12

Input/Output No aggregate input/output operations on a struct variable Data in a struct variable must be read one member at a time The contents of a struct variable must be written one member at a time Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13

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 Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14

Arrays versus structs Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15

Arrays in structs Two key items are associated with a list: Values (elements) Length of the list Define a struct containing both items: Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 16

Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Arrays in structs (cont'd.) 18 Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition

structs within a struct versus Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21

Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition

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 Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24

Summary (continued) 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 struct can be passed by value or reference A function can return a value of type struct structs can be members of other structs Prepared by: Malak Abdullah----C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25