Structures Structured Data types Data abstraction structs ---

Slides:



Advertisements
Similar presentations
1 Chapter 11 Introducing the Class Pages ( )
Advertisements

1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Structures Spring 2013Programming and Data Structure1.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Dale/Weems/Headington
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,
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 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 11 – Structured Data
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Structured Data Types array array union union struct struct class class.
Chapter 10: Records (structs)
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Module 4: Structures ITEI222 Advanced Programming.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
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:
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
LESSON 06.
CO1401 Program Design and Implementation
Chapter 10-1: Structure.
Structured Data (Lecture 07)
Structures and Unions.
Programming fundamentals 2 Chapter 1:Array
Programming Structures.
Student Book An Introduction
Structures - Part II aggregate operations arrays of type struct
Records C++ Structs Chapter 12.
C++ Data Types Simple Structured Address Integral Floating
DATA HANDLING.
Buy book Online -
Enumeration used to make a program more readable
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Structs And Arrays.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Structures.
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Data type List Definition:
Review for Final Exam.
CS148 Introduction to Programming II
Structured Data Types array union struct class.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Review for Final Exam.
Arrays Arrays A few types Structures of related data items
Structures In C Programming By Rajanikanth B.
Programming Structures.
CPS120: Introduction to Computer Science
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Programming Structures.
Pointers, Dynamic Data, and Reference Types
Programming Fundamental
Presentation transcript:

Structures Structured Data types Data abstraction structs ---

Data Types in C++ simple address structured integral floating int char short long [ unsigned ] bool enum float double long double pointer reference array class struct union

Structured Data Types simple data type atomic values '%' 18 -1.0 true structured type (a.k.a. composite) has component parts and organized structure a collection or group of elements

Why Structured Types? Group together multiple simple items in a single object. Examples: Entries in the campus directory Cards: rank and suit Library book entry … more

How can we picture this? title author numCopies allTakenOut BookRec

struct Declaration: Syntax struct <Type Name> { <Data Type> <Member Name>; . . . };

Example: Book Record Mini-Lib // store information about books in library struct BookRec // BookRec is the // type name { string title; // members string author; // must be distinct int numCopies; // within struct bool allTakenOut; };

MINI-Lib Program // declare two book record variables BookRec book1, book2; // assign values to book1 book1.title = "Walden"; book1.author = "Henry Thoreau"; book1.numCopies = 2; book1.allTakenOut = false;

How can we picture this? "Walden" "Henry Thoreau" 2 false book1

Aggregate Operations on structs I/O NO Assignment YES Arithmetic NO Comparison NO Pass as Argument YES by value or by reference Return as function result YES

Assigning structs book2 = book1; Copies all members An aggregate operation operates on the data structure as a whole

Using struct Members Just like any other variable of their type // add one to book1's copies book1.numCopies++; // print message based on bool member if (book1.allTakenOut) cout << "Sorry, none available."; // print a formatted title cout << setw(50) << book1.title;

Passing a struct by Value // example call from main // PrintOneBook (book1); void PrintOneBook (BookRec inbook) { cout << "Title: " << inbook.title << endl << "Author: " << inbook.author << endl << "Number of Copies: " << inbook.numCopies << endl; if (inbook.allTakenOut) cout << "All copies are out."; else cout << "The book is available."; }

Passing a struct by Reference // example call from main // AddBooks (5, book1); void AddBooks (int numPurchased,BookRec& thisbook) { thisbook.numCopies = thisbook.numCopies + numPurchased; cout << "The library now has " << thisbook.numCopies << " copies of " << thisbook.title; }

Nested (hierarchical) structs Members of a struct may be structs When to nest structs if nesting improves readability if a structure is used repeatedly inside other structs Beware: too many levels of nesting may impair readabilty

Example: nested structs // add date to our library book info struct DateRec // declare a type to represent a date { int month, day, year; };

Using DateRec struct NewBookRec { string title, author; DateRec datePublished; int numCopies; bool allTakenOut; }; … NewBookRec book3;

How can we picture this new book? title author } month book3.datePublished day year numCopies allTakenOut book3

Read in Date Published cout << "Enter the date published “ << “ as MM DD YYYY -> "; cin >> book3.datePublished.month >> book3.datePublished.day >> book3.datePublished.year;

Data Structures What is a data structure? Layout of information in a program to hold whatever data it needs Homogeneous data layout (arrays) Heterogeneous data layout (structures) Combinations of the two Choosing a data structure is an important step in writing a complex program…

Pick a Data Structure to hold… …the names of all the states in the USA …the season statistical totals for a baseball player …the service information for a car …the daily price of a stock for the last 6 months …the ID numbers of passengers on a plane …the quantity of stars in each square degree of the sky …the directory information for a University student …the directory information for all University students