Records C++ Structs Chapter 10.1.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

1 Records C++ Structs Chapter 14 2 What to do with records?  Declaring records  Accessing records  Accessing the field of a record  What is a union?
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Structures Spring 2013Programming and Data Structure1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 10: Records ( structs )
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 11: Records (structs)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
Chapter 8 Arrays and Strings
EGR 2261 Unit 10 Records (structs)
Chapter 10: Records (structs)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)
Chapter 10: Records (structs)
Edited from Powerpoint Slides provided by Thomson Learning
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Arrays & Structures II Chapter 9. 2 Organizing Heterogeneous Data  Arrays allow a programmer to organize values of the same type  Homogeneous data 
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Structures (aka records, structs) Textbook Chapter 11 sections:
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records ( struct s)
1 CS161 Introduction to Computer Science Topic #15.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Chapter VII: Arrays.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CO1401 Program Design and Implementation
Chapter 7: Introduction to Classes and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
MIS 120 Structures.
Data Structures and Abstract Data Types
The dirty secrets of objects
C++ Structs.
Data Types – Structures
Records C++ Structs Chapter 12.
Arrays … The Sequel Applications and Extensions
Data Types – Structures
Heterogeneous aggregate datatypes
Structs And Arrays.
Arrays and Strings Chapter 9.
User Defined Functions
Classes and Data Abstraction
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
Review for Final Exam.
CS148 Introduction to Programming II
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.
Chapter 11: Records (structs)
Arrays Arrays A few types Structures of related data items
Lec17 Structs.
Structures Structured Data types Data abstraction structs ---
Chapter 9: Records (structs)
Programming Fundamental
Presentation transcript:

Records C++ Structs Chapter 10.1

Records Recall that elements of arrays must all be of the same type In some situations, we wish to group elements of different types scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99 employee R. Jones 123 Elm 6/12/55 $14.75

Structs C++ Structs are used to group related components of different types Components of the record are called fields In C++ record called a struct (structure) fields called members employee R. Jones 123 Elm 6/12/55 $14.75

Records C++ struct structured data type fixed number of components elements accessed by name, not by index components may be of different types struct part_struct { char descrip [31], part_num [11]; float unit_price; int qty; };

Declaring struct Variables Given Declare : struct part_struct { char descrip [31], part_num [11]; float unit_price; int qty; }; Use struct name as a type. part_struct new_part, old_part;

Accessing Components Use the name of the record the name of the member separated by a dot . The dot is called the member selector old_part.qty = 5; cout << new_part.descrip;

Aggregate Operations with Structures Aggregate operation – apply to entire element. Recall that arrays had none (except reference parameter) Structures DO have aggregate operators assignment statement = parameter (value or reference) return a structure as a function type

Aggregate Operations with Structures Limitations on aggregate operations no I/O no arithmetic operations no comparisons cout << old_part; cin >> new_part; old_part = new_part + old_part; if (old_part < new_part) cout << ...;

Aggregate Operations with Structures struct variables must be compared member-wise. To compare the values of student and newStudent, you must compare them member-wise, as follows: if(student.firstName == newStudent.firstName && student.lastName == newStudent.lastName) ...

Input/Output There are no aggregate input/output operations on struct. Data in a struct variable must be read one member at a time. Contents of a struct must be written one member at a time.

struct Variables and Functions A struct variable can be passed as a parameter either by value or by reference. A function can return a value of the type struct http://ideone.com/wIkLCp 10

Contrast Arrays and structs

Arrays of Records First declare a struct (such as part_struct) Then specify an array of that type Access elements of the array, elements of the struct part_struct part_list [50]; for (x = 0; x <50; x++) cout << _______________________; How do we print all the descrip fields? part_list[x].descrip

Records with Arrays Example See sample program const arraySize = 1000; struct listType { int elements[arraySize]; //array containing the list int listLength; //length of the list } See sample program

Hierarchical Records Defn => records where at least one of the components is, itself, a record Example: struct inventory_struct { part_struct part; int qty_sold, re_order_qty; vendor_struct vendor; };

Choosing Data Structures Strive to group logical elements of a structure together calls for hierarchical structures Push details of entities down to lower levels of the structure Data Abstraction <=> separation of logical peoperties of a data type from its implementation

Testing and Debugging Hints Declaration of a struct type must end with a semicolon ; Be sure to specify the full member selector when referencing a component of a struct variable don’t leave out the struct name

Testing and Debugging When using an array in a struct, the index goes at the end student_rec.scores[x] When using an array of struct, the index goes after the struct name parts_list[x].qty

Testing and Debugging Process struct members separately … the only aggregate operations will be Assignment = Parameter passing void do_it (part_struct part); Function return part_struct blanked_part ( );

Testing and Debugging Be careful using same member names in different struct types Compiler keeps them separate OK Human readers can easily confuse them struct parts { int qty; . . . } ; struct test_scores { int qty; . . . } ;