Title: Data Abstraction and Structures Using C
1Data Abstraction and Structures Using
C Headington and Riley Chapter 5 C Records
(structs) Slides by Sylvia Sorkin, Community
College of Baltimore County - Essex
2Data Abstraction
- Separation of a data types logical properties
from its implementation.
LOGICAL PROPERTIES IMPLEMENTATION
What are the possible values? How can
this be done in C? What operations will be
needed? How can data types be used?
3Abstract Data Type (ADT)
- A data type whose properties (domain and
operations) are specified independently of any
particular implementation.
4Data from 3 different levels
- Application (or user) level modeling real-life
data in a specific context. WHY - Logical (or ADT) level abstract view of the
domain and operations. WHAT - Implementation level specific representation of
the structure to hold the data items, and the
coding for operations. HOW
5Viewing a library from 3 different levels
- Application (or user) level Library of Congress,
or Baltimore County Public Library. - Logical (or ADT) level domain is a collection of
books operations include check book out, check
book in, pay fine, reserve a book. - Implementation level representation of the
structure to hold the books, and the coding for
operations.
6Structured Data Type
- A structured data type (or data structure) is a
type which - stores a collection of individual data components
under one variable name, - has rules that determine how the individual data
components can be accessed.
7C Built-In Data Types
Simple
Structured
Integral
Floating
array struct union class
char short int long enum
float double long double
8Records
- A record is a linear, direct-access data
structure with heterogeneous components called
members or fields. For example . . .
9C structs
- C defines a struct to be a class whose members
are all, by default, public. -
- All operations that apply to C classes also
apply to structs.
10struct CarType
- struct CarType
-
- int year
- char maker10
- float price
-
-
- CarType thisCar //CarType variables
- CarType myCar
11Accessing struct members
- The member selection operator (period . ) is used
between the variable name and the member
identifier to access individual members of a
record (struct or class) type variable. - EXAMPLES
- myCar.year
- thisCar.maker4
12Aggregate struct operations
- Operations valid on an entire struct type
variable - assignment to another struct variable of
same type, -
- pass as a parameter to a function
- (either by value or by reference),
- return as the value of a function.
13Pass-by-value
sends a copy of the contents of the actual
parameter
SO, the actual parameter cannot be changed by
the function.
13
14Pass-by-reference
can change value of actual parameter
14
15Using struct type Reference Parameter to change
a member
- void AdjustForInflation(CarType car, float
perCent) - // Increases price by the amount specified in
perCent -
- car.price car.price perCent car.price
-
- SAMPLE CALL
- AdjustForInflation(myCar, 0.03)
16Using struct type Value Parameter to examine a
member
- Boolean LateModel(CarType car, int date)
- // Returns true if the cars model year is later
than or - // equal to date returns false otherwise.
-
- return ( car.year gt date )
-
- SAMPLE CALL
- if ( LateModel(myCar, 1995) )
- cout ltlt myCar.price ltlt endl