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.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Structures Spring 2013Programming and Data Structure1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
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
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.
Pointer Data Type and Pointer Variables
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
Chapter 6: User-Defined Functions
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.
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:
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 13: Overloading and Templates
Chapter 7: User-Defined Functions II
REVISION CSC 138 – Structured Programming One Dimensional Array
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
Computer Programming BCT 1113
Review: Chapter 5: Syntax directed translation
About the Presentations
MIS 120 Structures.
Records C++ Structs Chapter 10.1.
C++ Structs.
Records C++ Structs Chapter 12.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Heterogeneous aggregate datatypes
Structs And Arrays.
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.
Structure.
Chapter 11: Records (structs)
Objectives In this chapter, you will:
Chapter 9: Records (structs)
Chapter 11 Structure and Union Types.
Presentation transcript:

Chapter 9: Records (structs)

Objectives In this chapter, you will: Learn about records (structs) Examine operations on a struct Manipulate data using a struct Learn about the relationship between a struct and functions Discover how arrays are used in a struct Create an array of struct items C++ Programming: From Problem Analysis to Program Design, Sixth Edition

Records (structs) struct: collection of a fixed number of components (members), accessed by name Members may be of different types Syntax: C++ Programming: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, Sixth Edition

Records (structs) (cont’d.) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

Accessing struct Members Syntax to access a struct member: The dot (.) is called the member access operator C++ Programming: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, Sixth Edition

Arrays versus structs Refer to Table 9-1 C++ Programming: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, Sixth Edition

Arrays in structs (cont’d.) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

Arrays in structs (cont’d.) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

structs in Arrays Example: C++ Programming: From Problem Analysis to Program Design, Sixth Edition

structs in Arrays (cont’d.) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

structs within a struct C++ Programming: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, 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: From Problem Analysis to Program Design, Sixth Edition