Chapter 9: 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.
Structures Spring 2013Programming and Data Structure1.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
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
EGR 2261 Unit 10 Records (structs)
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 10: Records (structs)
Chapter 8 Arrays and Strings
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
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.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
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:
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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)
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Chapter 16: Linked Lists.
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.
Chapter 13: Overloading and Templates
REVISION CSC 138 – Structured Programming One Dimensional Array
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS1010 Programming Methodology
Computer Programming BCT 1113
Review: Chapter 5: Syntax directed translation
About the Presentations
MIS 120 Structures.
Records C++ Structs Chapter 10.1.
Data Structures and Abstract Data Types
C++ Structs.
Records C++ Structs Chapter 12.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Learning Objectives Structures Structure types
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.
Review for Final Exam.
Structure.
Chapter 11: Records (structs)
Objectives In this chapter, you will:
Chapter 9: Pointers and String
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Structures Chapter 4.
Presentation transcript:

Chapter 9: Records (structs)

Records (structs) struct: collection of a fixed number of components (members), accessed by name Members may be of different types Syntax: C++ Programming: Program Design Including Data Structures, Sixth Edition

Records (structs) (cont’d.) A struct is a definition, not a declaration Must declare a variable of that type to use it C++ Programming: Program Design Including Data Structures, Sixth Edition

Records (structs) (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

Accessing struct Members Syntax to access a struct member: The dot (.) is called the member access operator C++ Programming: Program Design Including Data Structures, Sixth Edition

Accessing struct Members (cont’d.) To initialize the members of newStudent: newStudent.GPA = 0.0; newStudent.firstName = "John"; newStudent.lastName = "Brown"; C++ Programming: Program Design Including Data Structures, Sixth 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 C++ Programming: Program Design Including Data Structures, Sixth Edition

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; C++ Programming: Program Design Including Data Structures, Sixth Edition

Comparison (Relational Operators) Compare struct variables member-wise No aggregate relational operations allowed To compare the values of student and newStudent: C++ Programming: Program Design Including Data Structures, Sixth Edition

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 C++ Programming: Program Design Including Data Structures, Sixth Edition

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 C++ Programming: Program Design Including Data Structures, Sixth Edition

Arrays versus structs Refer to Table 9-1 C++ Programming: Program Design Including Data Structures, Sixth Edition

Arrays in structs Two items are associated with a list: Values (elements) Length of the list Define a struct containing both items: C++ Programming: Program Design Including Data Structures, Sixth Edition

Arrays in structs (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

Arrays in structs (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

structs in Arrays Example: C++ Programming: Program Design Including Data Structures, Sixth Edition

structs in Arrays (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

structs within a struct C++ Programming: Program Design Including Data Structures, Sixth 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 C++ Programming: Program Design Including Data Structures, Sixth Edition

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 C++ Programming: Program Design Including Data Structures, Sixth Edition