Download presentation
Presentation is loading. Please wait.
Published by승수 십 Modified over 5 years ago
1
Structures Structured Data types Data abstraction structs ---
2
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
3
Structured Data Types simple data type
atomic values '%' true structured type (a.k.a. composite) has component parts and organized structure a collection or group of elements
4
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
5
How can we picture this? title author numCopies allTakenOut BookRec
6
struct Declaration: Syntax
struct <Type Name> { <Data Type> <Member Name>; };
7
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; };
8
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;
9
How can we picture this? "Walden" "Henry Thoreau" 2 false book1
10
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
11
Assigning structs book2 = book1; Copies all members
An aggregate operation operates on the data structure as a whole
12
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;
13
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."; }
14
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; }
15
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
16
Example: nested structs
// add date to our library book info struct DateRec // declare a type to represent a date { int month, day, year; };
17
Using DateRec struct NewBookRec {
string title, author; DateRec datePublished; int numCopies; bool allTakenOut; }; … NewBookRec book3;
18
How can we picture this new book?
title author } month book3.datePublished day year numCopies allTakenOut book3
19
Read in Date Published cout << "Enter the date published “ << “ as MM DD YYYY -> "; cin >> book3.datePublished.month >> book3.datePublished.day >> book3.datePublished.year;
20
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…
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.