Title: Chapter 11
1Chapter 11 Structured Data
- Abstract data types (ADTs) are data types created
by the programmer. - ADTs have their own range (or domain) of data and
their own set of operations that may be performed
on them.
2Abstraction
- An abstraction is a general model of something.
3Data Types
- C has several primitive data types
bool int unsigned long int
char long int float
unsigned char unsigned short int double
short int unsigned int long double
4What groups/types were there in high school?
- How did you recognize them?
- What they looked like
- What they did
- This is the concept of a data type!
5Abstract Data Types
- A data type created by the programmer
- The programmer decides what values are acceptable
for the data type. - The programmer decides what operations may be
performed on the data type.
6Combining Data into Structures
- C allows you to group several variables
together into a single item known as a structure.
7Payroll System
Variable Declaration Information Held
int empNumber Employee number
char name 25 Employee name
float hours Hours worked
float payRate Hourly pay rate
float grossPay Gross pay
8Payroll as a C struct
- struct PayRoll // NOTE capitalization of type
names -
- int EmpNumber
- char Name25
- float Hours
- float PayRate
- float GrossPay
- // PayRoll
9PayRoll deptHead
Structure Variable Name
Members
10PayRoll deptHead, foreman, associate
deptHead
foreman
associate
11At seats
- Discuss with your neighbor why you would want to
group data into a structure.
12Two Steps to Create a Structures
- Define the type Create structure declaration.
- This establishes the tag (or name) of the
structure and a list of items that are members. - Declare variables of that type
13Accessing Structure Members
- The dot operator (.) allows you to access
structure members in a program.
14Using Structures Program
- struct PayRoll
-
- int empNumber // Employee number
- char name25 // Employee's name
- float hours // Hours worked
- float payRate // Hourly Payrate
- float grossPay // Gross Pay
- // PayRoll
15Using Structures Program (cont)
- void main ( void )
- PayRoll employee // Employee is a PayRoll
structure - cout ltlt "Enter the employee's number "
- cin gtgt employee.empNumber
- cout ltlt "Enter the employee's name "
- cin.ignore() // Skip the remaining '\n'
character - cin.getline(employee.name, 25)
- cout ltlt "How many hours did the employee work?
" - cin gtgt employee.hours
- cout ltlt "What is the employee's hourly payrate?
" - cin gtgt employee.payRate
- employee.grossPay employee.hours
employee.payRate - cout ltlt "Here is the employee's payroll
data\n" - cout ltlt "Name " ltlt employee.name ltlt endl
16Displaying a Structure
- The contents of a structure variable cannot be
displayed by passing the entire variable to cout.
- For example, assuming employee is a PayRoll
structure variable, cout ltlt employee will not
work (We will learn later how to extend the
system to do this.
17Circle Structure Program
- struct Circle
-
- float radius
- float diameter
- float area
- // Circle
- const float PI 3.14159
- At seats use structs and method circleArea
(which you write) to return area of give circle
18Strings as Structure Members
- When a character array is a structure member, use
the same string manipulation techniques with it
as you would with any other character array. - In other words, after qualification, it is just a
simple type (which you know how to use).
19Arrays of Structures
- Arrays of structures can simplify some
programming tasks. - struct BookInfo
-
- char title50
- char author30
- char publisher25
- float price
- // BookInfo
- BookInfo bookList20
- How would you refer to the first letter of the
author in the third book? - At seats given an array of books, print out the
title of all books costing more than 100.
20Initializing a Structure Array
- struct PayInfo
- int jobId
- float rate
-
- PayInfo workers5 10, 9.75,
- 15, 8.62,
- 20, 10.50,
- 40, 18.75,
- 50, 15.65
21Nested Structures
- struct Costs
- float wholesale
- float retail
- // Costs
- struct Item
- string partNum
- string description
- Costs pricing
- // Item
22Questions
- Can we have arrays of structures?
- Can we have structures of arrays?
- Can we have structures of structures of
structures of arrays of arrays of structures. - YES
23Nested Structures Program
- struct Date
-
- int month
- int day
- int year
- // Date
- struct Place
-
- string address
- string city
- string state
- string zip
- // Place
24Nested Structures Program (cont)
- struct EmpInfo
- string name
- int empNumber
- Date birthDate
- Place residence
- // EmpInfo
- void main ( void )
- EmpInfo manager
- cout ltlt "Enter the manager's name "
- getline(cin,manager.name)
- cout ltlt "Enter the manager's employee number "
- cin gtgt manager.empNumber
25Nested Structures Program (cont)
- cout ltlt "Now enter the manager's
date-of-birth.\n" - cout ltlt "Month (up to 2 digits) "
- cin gtgt manager.birthDate.month
- cout ltlt "Day (up to 2 digits) "
- cin gtgt manager.birthDate.day
- cout ltlt "Year (2 digits) "
- cin gtgt manager.birthDate.year
- cin.get() // Eat the remaining newline
character - cout ltlt "Enter the manager's street address "
- getline(cin,manager.residence.address)
-
-
26Structures as Arguments Program
- struct InvItem
-
- int partNum // Part number
- string description // Item description
- int onHand // Units on hand
- float price // Unit price
- // InvItem
- // Function Prototypes
- void GetItem ( InvItem )
- // Important or nothing will be changed
- void ShowItem ( InvItem )
27Structures as Arguments Program (cont)
- void main ( void )
- InvItem part
- GetItem(part)
- ShowItem(part)
- // main
- void GetItem ( InvItem piece )
-
-
- // GetItem
28Constant Reference Parameters
- Sometimes structures can be quite large.
- Therefore, passing by value can decrease a
programs performance. But passing by reference
can cause problems. - Instead, pass by constant reference
- void ShowItem ( const InvItem piece )
- cout.setf(iosprecision(2) iosfixed
iosshowpoint) - cout ltlt "Part Number " ltlt piece.partNum ltlt
endl - cout ltlt "Price " ltlt piece.price ltlt endl
- // ShowItem
29Returning a Structure Program
- struct Circle
- float radius
- float diameter
- float area
- // Circle
- Circle getInfo ( void )
- const float PI 3.14159
30Returning a Structure Program (cont)
- void main ( void )
- Circle c
- c getInfo()
- c.area PI pow(c.radius, 2.0)
- cout ltlt "The radius and area of the circle
are\n" - cout.precision(2)
- cout.setf(iosfixed iosshowpoint)
- cout ltlt "Radius " ltlt c.radius ltlt endl
- cout ltlt "Area " ltlt c.area ltlt endl
- // main
- Circle getInfo ( void )
- Circle Round
- cout ltlt "Enter the diameter of a circle "
- cin gtgt Round.Diameter
- Round.Radius Round.Diameter / 2
- return Round
- // getInfo
31Pointers to Structures
- You may take the address of a structure variable
and create variables that are pointers to
structures. - Circle cirPtr
- CirPtr piePlate
- cirPtr.Radius 10 // incorrect
- (cirPtr).Radius 10 // correct
- cirPtr-gtRadius 10 // easier notation
32Pointers and Structures Program (cont)
- void main ( void )
- PayRoll employee
- employee new PayRoll
- assert( employee NULL )// Always good to do
- cout ltlt "Enter the employee's number "
- cin gtgt employee-gtempNumber
- cout ltlt "Enter the employee's name "
- cin.ignore()
- getline(cin,employee-gtname)
-
33The Use of ., -gt, and
Expression Expression Description
s-gtm s is a structure pointer and m is a member. This expression accesses the m member of the structure pointer to by s. s is a structure pointer and m is a member. This expression accesses the m member of the structure pointer to by s.
a.p a is a structure variable and p, a pointer, is a member. This expression dereferences the value pointed to by p. a is a structure variable and p, a pointer, is a member. This expression dereferences the value pointed to by p.
(s).m s is a structure pointer and m is a member. The operator dereferences s, causing the expression to access the m member of the structure pointed to by s. This expression is the same as s-gtm. s is a structure pointer and m is a member. The operator dereferences s, causing the expression to access the m member of the structure pointed to by s. This expression is the same as s-gtm.
s-gtp s is a structure pointer and p, a pointer, is a member of the structure pointed to by s. This expression accesses the value pointed to by p. s is a structure pointer and p, a pointer, is a member of the structure pointed to by s. This expression accesses the value pointed to by p.
(s).p s is a structure pointer and p, a pointer, is a member of the structure pointed to by s. This expression accesses the value pointed to by p. s is a structure pointer and p, a pointer, is a member of the structure pointed to by s. This expression accesses the value pointed to by p.
34Unions
- A union is like a structure, except all the
members occupy the same memory area (to save
space). - union PaySource
- short hours
- float sales
- // PaySource
- PaySource employee
- Fallen from favor as unsafe cannot do type
checking as can store as one type and use as
another.
35Unions Program
- union PaySource
- short hours
- float sales
- // PaySource
- void main ( void )
- PaySource employee1
- char payType
- float payRate, grossPay
36Unions Program (cont)
- if ( toupper(payType) 'H' )
- cout ltlt "What is the hourly pay rate? "
- cin gtgt payRate
- cout ltlt "How many hours were worked? "
- cin gtgt employee1.hours
- GrossPay employee1.hours payRate
- cout ltlt "Gross pay " ltlt grossPay ltlt endl
- // if
else if (toupper(payType) 'C') cin gtgt
employee1.sales grossPay employee1.sales
0.10 // else if else cout ltlt payType ltlt
" is not a valid selection.\n"