Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Encapsulation by Subprograms and Type Definitions
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 10 Introduction to Classes
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Structures (aka records, structs) Textbook Chapter 11 sections:
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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?
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Arrays Chapter 7.
Chapter 12 Classes and Abstraction
User-Written Functions
CS1010 Programming Methodology
Pointers, Enum, and Structures
Chapter 7: Introduction to Classes and Objects
Records C++ Structs Chapter 10.1.
Multiple Dimension Arrays
EKT150 : Computer Programming
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Mr. Dave Clausen La Cañada High School
Chapter 9 Introduction To Classes
Presentation transcript:

Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School

Mr. Dave Clausen2 The need for Structs  So far we have structured the data associated with a program in files, vectors or matrices.  Files and “arrays” have helped us organize data that are of the same type.  We could use “arrays” to organize data of different types using “parallel arrays” and multidimensional arrays, but there is a better way…Structs.

Mr. Dave Clausen3 Data Bases and Structs  Before we look at structs, let’s remind you how data is organized in a typical data base program.  We organize the data into records, and then organize the records into fields or categories of information that contain the same type of data in each field.  Data may be displayed in a “table” format or a “label” or “form” format.

Mr. Dave Clausen4 Form or Single Record Layout This format displays all the fields and data elements for one record only. This is similar to a struct.

Mr. Dave Clausen5 Table or Multiple Record Format In this format: The Rows are Records, and The Columns are the Categories or Fields. This is similar to a vector of records or structs. Ski Resorts in California

Mr. Dave Clausen6 What is a Struct?  A struct is a data structure that can have components or elements of different data types associated by and accessible by the same name.  The components or fields are called members of the struct.  When using structs it is often convenient to define a new data type, and declare variables to be of this type.

Mr. Dave Clausen7 Defining a Struct Type  The general form for defining a struct type is: struct //Include the word type here { ; }; //Yes, a semicolon must follow the right curly bracket. //A structure declaration is a statement, and therefore, //needs a semicolon to end the statement.

Mr. Dave Clausen8 A Struct Example struct employee_type //We included the word type. { apstring name, street, city, state, zip_code; int age; double salary; };//Don’t forget the semicolon! //Variable Declarations based upon the new data type. employee_type employee1, employee2;

Mr. Dave Clausen9 Another struct example Structs don’t have to use different data types.  Windows was originally written using structs in C (classes in C++ weren’t available yet), and still has some of this “legacy” code. Structs were used to keep track of the coordinates of rectangular windows… struct Rect { int left;//Top left point coordinate point int top; int right;//Bottom right point coordinate point. int bottom; };

Mr. Dave Clausen10 Remember where we define types...  Comments  Preprocessor Directives  Constant Definition Section  Type Definition Section (struct, class, typedef)  Function Declaration Section  Main Program Heading: int main( ); Declaration Section (Variables, etc…) Statement Section  Function Implementations

Mr. Dave Clausen11 Structs: General Information  When the struct variables are declared, that is when the computer allocates the memory for all the elements of the structs, not when the struct is defined.  The members (fields) of a struct are accessed by using a member selector: A selector is a period placed between the struct variable name and the name of a struct member (or field in our database terminology).

Mr. Dave Clausen12 Using Struct Member Selectors employee1.name = “John Doe”; employee1.street = “102 Maple Lane”; employee1.city = “York”; employee1.state = “PA”; employee1.zip_code = “12309”; employee1.age = 21; employee1.salary = ;

Mr. Dave Clausen13 Visualizing the Struct as a Data Base Form name street city state zip_code age salary John Doe 102 Maple Lane York PA employee1 Members of the struct Data Items

Mr. Dave Clausen14 Advantages of Using Structs  If you define your struct as a data “type”, and declare two variables of that same type, you can: Perform aggregate assignments employee2 = employee1;  This will copy every member of one struct into the other. (We don’t have to copy every member…) employee2.name = employee1.name; employee2.street = employee1.street; etc… (A great shortcut!)

Mr. Dave Clausen15 Nested Structs & Hierarchical Records  A component of a record can be another record. When this occurs we call them hierarchical records.  Using struct vocabulary, a struct member can be any valid data type. When a struct is a member of another struct, we call this nested structs.  In this case, more than one member selector may be necessary to access any data member.

Mr. Dave Clausen16 An Example of Nested Structs struct address_type { apstring street, city, zip_code; }; struct employee_type { apstring name; address_type address; int age; double salary; }; //address_type needs to be declared before employee_type

Mr. Dave Clausen17 Let’s Try Defining A Struct Struct Date_Type { }; //Represent: Month (int or enum), Day, & Year

Mr. Dave Clausen18 Let’s Try Defining Another Struct Struct Inventory_Type { }; //Part #, Description, Quantity, Price, & Ship Date

Mr. Dave Clausen19 Using Multiple Member Selectors If we declare variables based on these nested structs such as: employee_type employee1, employee2; To access any part of the address requires 2 member selectors: cout<<“Street: “<<employee1.address.street<<endl; cout<<“City: “<<employee1.address.city<<endl; cout<<“Zip Code: “<<employee1.address.zip_code<<endl;

Mr. Dave Clausen20 Other Struct Advantages  If you define your struct as a data “type” statement, you can define more complex structures, like a vector of structs. apvector list(vector_size); Each component of this vector is a struct containing the members previously defined in employee_type  list[0] //accesses the entire struct for the first employee  list[0].name //accesses the name member of the struct for the first employee

Mr. Dave Clausen21 Using Structs with Functions  Functions can return data of type struct. Consider the following function declarations: employee_type init_employee( ); employee_type read_employee( ); employee_type print_employee( );  The implementations of these functions will initialize, read, or print each member of the struct

Mr. Dave Clausen22 Sample Function Implementation employee_type init_employee( ) { employee_type employee; //A local variable is declared employee.name = "John Doe"; employee.street = "123 Anywhere Lane"; employee.city = "SomeCity"; employee.state = ”SomeState"; employee.zip_code = "00000"; employee.age = -1; employee.salary = -1.00; //Use initialization values that are obviously not real data items. return employee; }

Mr. Dave Clausen23 A Vector of Structs  To declare a vector of structs: Ask the user how many elements they would like in the vector. Then declare the vector using: apvector list(vector_size);  You can now declare and call functions using the vector of structs: void read_list(apvector &list); Remember, you don’t have to pass the length of the vector as a parameter to each function. You can use the length member function built into the apvector class: list.length( )

Mr. Dave Clausen24 Data Abstraction In Chapter 4, we said that a function is an example of abstraction, because it simplifies a complex task for its users. Simplification of complex data requires two components:  A data structure such as an vector or struct, labeled with a type name.  And a set of functions that perform operations on the data structure. Therefore, data abstraction is the separation of the conceptual definition of a data structure and its implementation.

Mr. Dave Clausen25 Abstract Data Types (ADT’s) An ADT is a class of objects (values), a defined set of properties of those objects (values), and a set of operations for processing the objects(values). The benefits of ADT’s are simplicity and ease of use. An ADT doesn’t mention how it is implemented, or how it can be used in a program. Many C++ ADT’s are already defined in libraries. A library header file contains the type definitions of the data structures and the function declarations

Mr. Dave Clausen26 Sample ADT Library Header Files  The employee type ADT contains definitions for employee_type, and function declarations for init_ employee, read_ employee, and print_ employee. employee.hemployee.h employeh.htxtemployeh.htxt  The list type ADT contains definitions for MAX_LIST_SIZE, list_type, and function declarations for init_list, read_list, and print_list. This is based upon an array, not a vector…listvctr.h listvctrh.htxtlistvctr.hlistvctrh.htxt

Mr. Dave Clausen27 Let’s look at the assigned program  We can use the employee type ADT in our program, as is…  We will have to modify the list type ADT to use vectors instead of arrays…  We will not separate the header “.h” files from the implementation “.cpp” files in this program.  Refer to pages for this program. P9avectr.exe