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