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?

Slides:



Advertisements
Similar presentations
Números.
Advertisements

EuroCondens SGB E.
Chapter 7 Completing a Program
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Addition and Subtraction Equations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
1 Arrays … The Sequel Applications and Extensions Chapter 10.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2.
Precedence of all operators: AssociativelyoperatorPrecedence Innermost first()1 Right to left++ -- !2 Left to right* / %3 Left to right+ -4 Left to right.
Before Between After.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
2.10% more children born Die 0.2 years sooner Spend 95.53% less money on health care No class divide 60.84% less electricity 84.40% less oil.
Static Equilibrium; Elasticity and Fracture
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
úkol = A 77 B 72 C 67 D = A 77 B 72 C 67 D 79.
EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Structured Data Types array array union union struct struct class class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Array, Structure and Union
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Struct Data Type in C++ What Are Structures?
1 CS161 Introduction to Computer Science Topic #15.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Chapter 10-1: Structure.
Chapter 7: Introduction to Classes and Objects
MIS 120 Structures.
Records C++ Structs Chapter 10.1.
C++ Structs.
Records C++ Structs Chapter 12.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
CS148 Introduction to Programming II
Structured Data Types array union struct class.
Chapter 1: Introduction to Data Structures(8M)
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.
Structures In C Programming By Rajanikanth B.
Programming Fundamental
Presentation transcript:

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?  Can records be in arrays?

3 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 : employee R. Jones 123 Elm 6/12/55 $14.75

4 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

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

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

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

8 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

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

10 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)...

11 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.

12 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 fragmentexample program

13 Contrast Arrays and struct s

14 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

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

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

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

18 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

19 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

20 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

21 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 ( );

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