Data Abstraction and Structures Using C Headington and
Data Abstraction and Structures Using C++ Headington and Riley Chapter 5 C++ Records (structs) Slides by Sylvia Sorkin, Community College of Baltimore County - Essex 1
Data Abstraction l Separation of a data type’s 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? 2
Abstract Data Type (ADT) l A data type whose properties (domain and operations) are specified independently of any particular implementation. 3
Data from 3 different levels l Application (or user) level: modeling reallife data in a specific context. WHY l Logical (or ADT) level: abstract view of the domain and operations. WHAT l Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW 4
Viewing a library from 3 different levels l Application (or user) level: Library of Congress, or Baltimore County Public Library. l Logical (or ADT) level: domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book. l Implementation level: representation of the structure to hold the “books”, and the coding for operations. 5
Structured Data Type A structured data type (or data structure) is a type which l stores a collection of individual data components under one variable name, l has rules that determine how the individual data components can be accessed. 6
C++ Built-In Data Types Simple Structured Integral Floating array struct union class char short int long enum float double long double Address pointer reference 7
Records A record is a linear, direct-access data structure with heterogeneous components called members or fields. For example. . . my. Car at Base Address 6000. year. maker. price 1999 ‘h’ ‘o’ ‘n’ ‘d’ ‘a’ ‘ ’ . . . 18678. 92 8
C++ 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. 9
struct Car. Type struct { Car. Type int char float year ; maker[10]; price ; } ; Car. Type this. Car; my. Car; //Car. Type variables 10
Accessing 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 my. Car. year this. Car. maker[4] 11
Aggregate struct operations l 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. 12
Pass-by-value sends a copy of the contents of the actual parameter CALLING BLOCK FUNCTION CALLED SO, the actual parameter cannot be changed by the function. 13
Pass-by-reference sends the location (memory address) of the actual parameter CALLING BLOCK FUNCTION CALLED can change value of actual parameter 14
Using struct type Reference Parameter to change a member void Adjust. For. Inflation(Car. Type& car, float per. Cent) // Increases price by the amount specified in per. Cent { car. price = car. price * per. Cent + car. price; }; SAMPLE CALL Adjust. For. Inflation(my. Car, 0. 03); 15
Using struct type Value Parameter to examine a member Boolean Late. Model(Car. Type car, int date) // Returns true if the car’s model year is later than or // equal to date; returns false otherwise. { return ( car. year >= date ) ; }; SAMPLE CALL if ( Late. Model(my. Car, 1995) ) cout << my. Car. price << endl ; 16
- Slides: 16