Records C++ Structs Chapter 12.

Slides:



Advertisements
Similar presentations
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?
Advertisements

Structure.
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.
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)
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Chapter 10: Records (structs)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)
1 STRUCTURES AND POINTERS. 2 A VARIABLE OF THIS COMPOSITE TYPE CAN HAVE MORE THAN ONE VALUE, GROUPED TOGETHER TO DESCRIBE AN ENTITY. THE COMPONENTS OF.
Chapter 10: Records (structs)
Edited from Powerpoint Slides provided by Thomson Learning
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
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.
 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.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
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 :
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.
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?
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records ( struct s)
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.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
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.
Records C++ Structs Chapter 10.1.
Data Structures and Abstract Data Types
C++ Structs.
Data Types – Structures
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Arrays … The Sequel Applications and Extensions
Arrays, For loop While loop Do while loop
Data Types – Structures
Structs And Arrays.
Arrays and Strings Chapter 9.
User Defined Functions
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes and Data Abstraction
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
Review for Final Exam.
Chapter 15 Pointers, Dynamic Data, and Reference Types
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)
Structures Structured Data types Data abstraction structs ---
Pointers, Dynamic Data, and Reference Types
Chapter 9: Records (structs)
Programming Fundamental
Presentation transcript:

Records C++ Structs Chapter 12

What to do with records? Declaring records Accessing records Accessing the field of a record What is a union? Can records be in arrays?

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

Records RECORDS 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 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 Note example program fragment

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]; How do we print all the descrip fields? for (x = 0; x <50; x++) cout << _______________________; 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; };

Unions defn => a struct that holds only one of its members at a time during program execution At run time, how_many contains either an int a float a long . . . But … never all three union Qty_type { int count; float linear_ft; long cu_in; }; Qty_type how_many;

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; . . . } ;