Chap 2 2 Intro To Data Structures ADTs

  • Slides: 59
Download presentation
Chap. 2 2. Intro. To Data Structures & ADTs – C-Style Types • Goal:

Chap. 2 2. Intro. To Data Structures & ADTs – C-Style Types • Goal: to organize data • Criteria: to facilitate efficient – storage of data – retrieval of data – manipulation of data • Design Issue: – select and design appropriate data types. (This is the real essence of OOP. ) 1

Examples – Factors to Consider Lab 2 A Airline Reservations Trans-Fryslan Airlines (pp. 30

Examples – Factors to Consider Lab 2 A Airline Reservations Trans-Fryslan Airlines (pp. 30 -31) Attempt 1: enum Seat. Status {OCCUPIED, UNOCCUPIED}; Seat. Status seat 1, seat 2, . . . , seat 10; Horrible algorithms for the basic operations! Attempt 2: const int MAX_SEATS = 10; // upper limit on number of seats enum Seat. Status {OCCUPIED, UNOCCUPIED}; typedef Seat. Status Seat. List[MAX_SEATS]; Seat. List seat; Nice algorithms for the basic operations! Tradeoff: simplicity of data organization « simplicity/elegance of algorithms Simple (unsophisticated data structure) may require much work for processing data. More complex data organization may yield nicer algorithms for the basic operations 2

Examples - cont. or interpolation search 3

Examples - cont. or interpolation search 3

Abstract Data Type (ADT) Def. a collection of related data items together with an

Abstract Data Type (ADT) Def. a collection of related data items together with an associated set of operations e. g. whole numbers (integers) and arithmetic operators for addition, subtraction, multiplication and division. e. g. Seats for TFA Basic operations: find empty seat, reserve a seat, cancel a seat assignment Why "abstract? " Data, operations, and relations are studied independent of implementation. What not how is the focus. 4

Implementation of an ADT Def. Consists of storage structures (aka data structures) to store

Implementation of an ADT Def. Consists of storage structures (aka data structures) to store the data items and algorithms for the basic operations. The storage structures/data structures used in implementations are provided in a language (primitive or built-in) or are built from the language constructs (user-defined). In either case, successful software design uses data abstraction: Separating the definition of a data type from its implementation. 5

istringstream ostringstream 6

istringstream ostringstream 6

Simple Data Types (§ 2. 2) Memory: 2 -state devices « bits 0 and

Simple Data Types (§ 2. 2) Memory: 2 -state devices « bits 0 and 1 Organized into bytes (8 bits) and words (machine dependent — e. g. , 4 bytes). Each byte (or word) has an address making it possible to store and retrieve contents of any given memory location. Project 1 (See App. B re base 2, 8, 16) Therefore: the most basic form of data: sequences of bits simple data types (values are atomic — can't be subdivided) are ADTs. Implementations have: characters integers » Storage structures: memory locations reals logical » Algorithms: system hardware/software to do basic operations. 7

Boolean data Data values: {false, true} In C/C++: false = 0, true = 1

Boolean data Data values: {false, true} In C/C++: false = 0, true = 1 (or nonzero) Could store 1 value per bit, but usually use a byte (or word) Operations: and or not && || (See bit tables on p. 34) ! x !x 0 1 1 0 8

Character Data App. A Java 9

Character Data App. A Java 9

Integer Data Nonegative (unsigned) integer: type unsigned (and variations) in C++ Store its base-two

Integer Data Nonegative (unsigned) integer: type unsigned (and variations) in C++ Store its base-two representation in a fixed number w of bits (e. g. , w = 16 or w = 32) App. B 88 = 0000010110002 Signed integer: type int (and variations) in C++ Store in a fixed number w of bits using one of the following representations: 10

Sign-magnitude representation Save one bit (usually most significant) for sign (0 = +, 1

Sign-magnitude representation Save one bit (usually most significant) for sign (0 = +, 1 = – ) Use base-two representation in the other bits. 88 ® _00001011000 0 sign bit – 88 ® 1_00001011000 Cumbersome for arithmetic computations 11

Two's complement representation For nonnegative n: Use ordinary base-two representation with leading (sign) bit

Two's complement representation For nonnegative n: Use ordinary base-two representation with leading (sign) bit 0 For negative n (–n): (1) Find w-bit base-2 representation of n WHY? (2) Complement each bit. (3) Add 1 (Flip all bits from rightmost 0 to the end) Example: – 88 1. 88 as a 16 -bit base-two number 2. Complement this bit string 3. Add 1 Same as sign mag. WHY? 000001011000 111110100111 111110101000 12

Good for arithmetic computations (see p. 38) These work for both + and –

Good for arithmetic computations (see p. 38) These work for both + and – integers 5 + 7: 111¬¾¾ carry bits 0000000101 +0000000111 0000001100 5 + – 6: 0000000101 +1111111010 11111111 13

Biased representation Add a constant bias to the number (typically, 2 w – 1)

Biased representation Add a constant bias to the number (typically, 2 w – 1) ; then find its base-two representation. Examples: 88 using w = 16 bits and bias of 215 = 32768 1. Add the bias to 88, giving 32856 2. Represent the result in base-two notation: 100001011000 Note: For n > 0, just change leftmost bit of binary representation of n to 1 – 88: 1. Add the bias to -88, giving 32680 2. Represent the result in base-two notation: 011110101000 ® Good for comparisons; so, it is commonly used for exponents in floating-point representation of reals. 14

Problems with Integer Representation Limited Capacity — a finite number of bits An operation

Problems with Integer Representation Limited Capacity — a finite number of bits An operation can produce a value that requires more bits than maximum number allowed. This is called What's INT_MAX + 1? overflow. See climits (App. C) So none of these is a perfect representation of (mathematical) integers — can only store a finite (sub)range of them. 15

Real Data p. 756 2. Store: — sign of mantissa in leftmost bit (0

Real Data p. 756 2. Store: — sign of mantissa in leftmost bit (0 = +, 1 = – ) — biased binary rep. of exponent in next 8 bits (bias = 127) — bits b 2 b 3. . . in rightmost 23 bits. (Need not store b 1 — know it's 1) Example: 22. 625 = 10110. 1012 Floating point form: 1. 01101012 ´ 24 double: Exp: 11 bits, bias 1023 Mant: 52 bits (see p. 41) + 127 16

Problems with Real Representation What's 10*DBL_MAX? See cfloat (App. C) Roundoff error (pp. 41

Problems with Real Representation What's 10*DBL_MAX? See cfloat (App. C) Roundoff error (pp. 41 -42) Most reals do not have terminating binary representations. Example: 0. 7 = (0. 10110011001100110. . . )2 p. 756 e. g. , 44. 7 Roundoff error may be compounded in a sequence of operations. Be careful in comparing reals with == and !=. 17

C-Style Data Structures: Arrays (§ 2. 3) Defn of an array as an ADT:

C-Style Data Structures: Arrays (§ 2. 3) Defn of an array as an ADT: An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation is direct access to each element in the array so values can be retrieved from or stored in this element. Properties: • Ordered so there is a first element, a second one, etc. • Fixed number of elements — fixed capacity • Elements must be the same type (and size); use arrays only for homogeneous data sets. • Direct access: Access an element by giving its location — the time to access each element is the same for all elements, regardless of position. — in contrast to sequential access (where to access an element, one must first access all those that precede it. ) 18

Declaring arrays in C++ element_type array_name[CAPACITY]; where element_type array_name CAPACITY is any type is

Declaring arrays in C++ element_type array_name[CAPACITY]; where element_type array_name CAPACITY is any type is the name of the array — any valid identifier (a positive integer constant) is the number of elements in the array Can't input the capacity The compiler reserves a block of consecutive memory locations, enough to hold CAPACITY values of type element_type. The elements (or positions) of the array are indexed 0, 1, 2, . . . , CAPACITY - 1. e. g. , double score[100]; Better to use a named constant to specify the array capacity: const int CAPACITY = 100; double score[CAPACITY]; score[0] score[1] score[2] score[3]. . . Can use typedef with array declarations; e. g. , const int CAPACITY = 100; typedef double Scores. Array[CAPACITY]; score[99] Scores. Array score; . . . 19

How well does C/C++ implement an array ADT? As an ADT In C++ ordered

How well does C/C++ implement an array ADT? As an ADT In C++ ordered indices numbered 0, 1, 2, . . . , CAPACITY - 1 fixed size CAPACITY specifies the capacity of the array same type elements element_type is the type of elements direct access subscript operator [] 20

Subscript operator [] is an actual operator and not simply a notation/punctuation as in

Subscript operator [] is an actual operator and not simply a notation/punctuation as in some other languages. Its two operands are an array variable and an integer index (or subscript) and is written array_name[i] Here i is an integer expression with 0 < i < CAPACITY – 1. [] returns the address of the element in location i in array_name; so array_name[i]is a variable, called an indexed (or subscripted) variable, whose type is the specified element_type of the array. Also, it can have an index This means that it can be used on the left side of an assignment, in input statements, etc. to store a value in a specified location in the array. For example: // Zero out all the for (int i = 0; i < score[i] = 0. 0; // Read values into for (int i = 0; i < cin >> score[i]; elements of score CAPACITY; i++) the first num. Scores elements of score num. Scores; i++) // Display values stored in the first num. Scores elements for (int i = 0; i < num. Scores; i++) cout << score[i] << endl; 21

Array Initialization In C++, arrays can be initialized when they are declared. an array

Array Initialization In C++, arrays can be initialized when they are declared. an array literal Numeric arrays: element_type num_array[CAPACITY] = {list_of_initial_values}; Example: double rate[5] = {0. 11, 0. 13, 0. 16, 0. 18, 0. 21}; Note 1: If fewer values supplied than array's capacity, remaining elements assigned 0. double rate[5] = {0. 11, 0. 13, 0. 16}; What’s an easy way to initialize an array to all zeros? Note 2: It is an error if more values are supplied than the declared size of the array. How this error is handled, however, will vary from one compiler to another. Note 3: If no values supplied, array elements are undefined (i. e. , garbage values). 22

Character arrays: Character arrays may be initialized in the same manner as numeric arrays.

Character arrays: Character arrays may be initialized in the same manner as numeric arrays. char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; declares vowel to be an array of 5 characters and initializes it as follows: Note 1: If fewer values are supplied than the declared size of the array, the zeroes used to fill uninitialized elements are interpreted as the null character '' whose ASCII code is 0. const int NAME_LENGTH = 10; char college. Name[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'}; 23

Note 2: Character arrays may be initialized using string constants. For example, the following

Note 2: Character arrays may be initialized using string constants. For example, the following declaration is equivalent to the preceding: char college. Name[NAME_LENGTH] = "Calvin"; Note 3: The null character '' (ASCII code is 0) is used as an end-of-string mark. Thus, character arrays used to store strings should be declared large enough to store the null character. If it is not, one cannot expect some of the string functions and operations to work correctly. See cstring in App. D; also >> and << If a character array is initialized with a string constant, the end-of-string mark is added automatically, provided there is room for it. char college. Name[7] = {'C', 'a', 'l', 'v', 'i', 'n', ''}; char college. Name[7] = "Calvin"; 24

Initializations with no array size specified The array capacity may be omitted in an

Initializations with no array size specified The array capacity may be omitted in an array declaration with an initializer list. In this case, the number of elements in the array will be the number of values in the initializer list. Example: double rate[] = {0. 11, 0. 13, 0. 16}; Note: This explains the brackets in constant declarations such as const char IN_FILE[] = "employee. dat"; 25

Addresses When an array is declared, the address of the first byte (or word)

Addresses When an array is declared, the address of the first byte (or word) in the block of memory associated with the array is called the base address of the array. Each array reference must be translated into an offset from this base address. For example, if each element of array score will be stored in 8 bytes and the base address of score is 0 x 1396. A statement such as cout << score[3] << endl; requires that array reference score[3] be translated into a memory address: score 0 x 1396 score[3] ® 0 x 1396 + 3 * (sizeof double) = 0 x 1396 + 3 * 8 = 0 x 13 ae [0] [1] [2] [3]. . . [99] The contents of the memory word with this address 0 x 13 ae can then be retrieved and displayed. An address translation like this is carried out each time an array element is accessed. 26

The value of array_name is actually the base address of array_name + index is

The value of array_name is actually the base address of array_name + index is the address of array_name[index]. An array reference is equivalent to Lab 2 B array_name[index] *(array_name + index) * is the dereferencing operator *ref returns the contents of the memory location with address ref For example, the following statements are equivalent: cout << score[3] << endl; cout << *(score + 3) << endl; Note: No bounds checking of indices is done! (See pp. 50 -51) 27

C-Style Multidimensional Arrays Example: A table of test scores for several different students on

C-Style Multidimensional Arrays Example: A table of test scores for several different students on several different tests. p. 52 For storage and processing, use a two-dimensional array. 28

Declaring Two-Dimensional Arrays Standard form of declaration: element_type array_name[NUM_ROWS][NUM_COLUMNS]; [0] [[1] [2] [3] [0]

Declaring Two-Dimensional Arrays Standard form of declaration: element_type array_name[NUM_ROWS][NUM_COLUMNS]; [0] [[1] [2] [3] [0] Example: [1] [2] const int NUM_ROWS = 30, [3]. . NUM_COLUMNS = 4; . double scores. Table[NUM_ROWS][NUM_COLUMNS]; [29] or typedef double Two. Dim. Array [NUM_ROWS][NUM_COLUMNS]; Two. Dim. Array scores. Table; Initialization List the initial values in braces, row by row; May use internal braces for each row to improve readability. Example: double rates[2][3] = {{0. 50, 0. 55, 0. 53}, // first row {0. 63, 0. 58, 0. 55}}; // second row 29

Processing Two-Dimensional Arrays Remember: Use Rows (and) columns are numbered from zero!! doubly-indexed variables:

Processing Two-Dimensional Arrays Remember: Use Rows (and) columns are numbered from zero!! doubly-indexed variables: scores. Table[2][3] is the entry in row 2 and column 3 row index column index Counting from 0 nested loops to vary the two indices, most often in a rowwise manner. int num. Students, num. Tests, i, j; cout << "# students and # of tests? "; cin >> num. Students >> num. Tests; cout << "Enter test scores for studentsn"; for (i = 0; i < num. Students; i++) { cout << '#' << i + 1 << ': '; for (j = 0; j < num. Tests; j++) cin >> scores. Table[i][j]; } 30

Higher-Dimensional Arrays The methods for two-dimensional arrays extend in the obvious way. Example: To

Higher-Dimensional Arrays The methods for two-dimensional arrays extend in the obvious way. Example: To store and process a table of test scores for several different students on several different tests for several different semesters const int RANKS = 10, ROWS = 30, COLUMNS = 4; typedef double Three. Dim. Array[RANKS][ROWS][COLUMNS]; Three. Dim. Array grade. Book; grade. Book[4][2][3] is the score on page 4 for student 2 on test 3 // number of pages, students and tests all counted from zero!! 31

b. Still higher dimensions Example like the automobile-inventory example on pp. 54 -5 Errors

b. Still higher dimensions Example like the automobile-inventory example on pp. 54 -5 Errors in text enum Brand. Type {Levi, Wrangler, Calvin. Klein, Lee, Big. Yank, NUM_BRANDS}; enum Style. Type {baggy, tapered, straightleg, designer, NUM_STYLES}; enum Waist. Type {w 28, w 29, w 30, w 31, w 32, w 33, w 34, w 35, w 36, w 37, w 38, w 39, w 40, w 41, w 42, w 43, w 44, w 45, w 46, w 47, w 48, NUM_WAIST_SIZES}; enum Inseam. Type {i 26, i 27, i 28, i 29, i 30, i 31, i 32, i 33, i 34, i 36, NUM_INSEAM_SIZES}; typdef int Jeans. Array[NUM_BRANDS][NUM_STYLES] [NUM_WAIST_SIZES][NUM_INSEAM_SIZES]; Jeans. Array jeans. In. Stock; jeans. In. Stock[b][s][w][i]--; // sale of 1 brand-b, style-s, waist-w, inseam-i jeans 32

Arrays of Arrays [0] [[1] [2] [3] [0] [1] [2] [3] double scores. Table[30][4];

Arrays of Arrays [0] [[1] [2] [3] [0] [1] [2] [3] double scores. Table[30][4]; . . . [29] Declares scores. Table to be a one-dimensional array containing 30 elements, each of which is a one-dimensional array of 4 real numbers; that is, scores. Table is a one-dimensional array of rows , each of which has 4 real values. We could declare it as typedef double Row. Of. Table[4]; Row. Of. Table scores. Table[30]; [0] [[1] [2] [3] or, since typedef is used once, why not use it twice: . . . [29] typedef double Row. Of. Table[4]; typedef Row. Of. Table Two. Dim. Array[30]; Two. Dim. Array scores. Table; 33

In any case: scores. Table[i] is the i-th row of the table scores. Table[i][j]

In any case: scores. Table[i] is the i-th row of the table scores. Table[i][j] should be thought of as (scores. Table[i])[j] that is, as finding the j-th element of scores. Table[i]. Address Translation: The array-of-arrays structure of multidimensional arrays explains address translation. Suppose the base address of scores. Table is 0 x 12348: scores. Table[10][3 ] scores. Table[10] 0 x 12348 + 10*(sizeof Row. Of. Table) = 0 x 12348 + 10 * (4 * 8) scores. Table[10][3 base(scores. Table[10]) + 3*(sizeof double) ] = 0 x 12348 + 10 * (4 * 8) + 3 * 8 = 0 x 124 a 0 In general, an n-dimensional array can be viewed (recursively) as a one-dimensional array whose elements are (n - 1)-dimensional arrays. [0] [1] [9] [10] . . . [3] 34

Arrays as Parameters Passing an array to a function actually passes the base address

Arrays as Parameters Passing an array to a function actually passes the base address of the array. This means: 1. The parameter has the same address as the argument. So modifying the void f(Array. Type parameter will modify f(array); {. . . } the corresponding array argument. param) 2. Array capacity is not available to a function unless passed as a separate parameter. The following function prototypes are all equivalent void Print(int A[100], int the. Size); void Print(int A[], int the. Size); void Print(int * A, int the. Size); 35

Arrays as Parameters …Continued Now, what about multidimensional arrays? void Print(double table[][], int rows,

Arrays as Parameters …Continued Now, what about multidimensional arrays? void Print(double table[][], int rows, int cols) doesn't work Use a typedef to declare a global type identifier and use it to declare the types of the parameters. For example: By #includes (so global) typedef double Two. Dim. Array[30][4]; . . . Two. Dim. Array scores. Table; . . . void Print(Two. Dim. Array table, int rows, int col). . . 36

Problems with C-Style Arrays • Capacity cannot change. Later Solution 1 (non-OOP) Use a

Problems with C-Style Arrays • Capacity cannot change. Later Solution 1 (non-OOP) Use a run-time array — Construct B to have required capacity — Copy elements of A into B — Deallocate A Solution 2 (OOP) Use vector 37

 • Virtually no predefined operations for non-char arrays. ® Basic reason: No character

• Virtually no predefined operations for non-char arrays. ® Basic reason: No character to mark the end of a numeric sequence — no numeric equivalent of the NUL character. Start processing here J o Stop processing here h n Start processing here 6 2 D o e Stop processing where? ? ? 0 1 5 0 2 0 0 0 Solution 1(non-OOP): In addition to the array, pass its size (and perhaps its capacity) to functions. The Deeper Problem: C-style arrays aren't self-contained. Basic principle of OOP: An object should be autonomous (self-contained); it should carry within itself all of the information needed to describe and operate upon itself. Solution (OOP): Encapsulate array, capacity, size, and operations in a class. Array in Java vector 38

Aggregate Data Types Why Needed? Current OCD: 1. Identify the objects in the problem.

Aggregate Data Types Why Needed? Current OCD: 1. Identify the objects in the problem. 1 a. . 2. Identify the operations in the problem. 2 a. If the operation is not predefined, write a function to perform it. 2 b. If the function is useful for other problems, store it in a library. 3. Organize the objects and operations into an algorithm. 4. Code the algorithm as a program. 5. Test, execute, and debug the program. 6. Maintain the program But, predefined types may not be adequate; so we add: 1 a. If necessary, create a new data type to model it. 39

Especially true if object being modeled has multiple attributes. Examples: A temperature has: a

Especially true if object being modeled has multiple attributes. Examples: A temperature has: a degrees attribute a scale attribute (Fahrenheit, Celsius, Kelvin) 32 degrees F scale A date has: a month attribute a day attribute a year attribute February 14 2001 month day year 40

C++ provides structs and classes to create new types with multiple attributes. So we

C++ provides structs and classes to create new types with multiple attributes. So we add to our OCD methodology: 1 a. If necessary, create a new data type to model it. 1 b. If the object has multiple attributes, create a struct or class to represent objects of that type. 41

As an ADT: A structure (usually abbreviated to struct and sometimes called a record)

As an ADT: A structure (usually abbreviated to struct and sometimes called a record) has a fixed size is ordered elements may be of different types Only difference from an array The basic operation is direct access to each element so that values can be stored in / retrieved from that element. Declaration (C-Style) struct Type. Name { Type. A data 1; Type. B data 2; . . . //member data of any type }; 42

Examples: 32 degrees F scale struct Temperature { double degrees; // number of degrees

Examples: 32 degrees F scale struct Temperature { double degrees; // number of degrees char scale; // temp. scale (F, C, or K) }; Temperature temp; February 14 2001 month day year char month[10]; struct Date { string month; // name of month int day, // day number year; // year number }; Date birthday, current. Date; 43

Phone Listing: John Q. Doe 12345 Calvin Rd. Grand Rapids, MI name street struct

Phone Listing: John Q. Doe 12345 Calvin Rd. Grand Rapids, MI name street struct Directory. Listing { string char name[20], name, street[20], street, city. And. State[20]; city. And. State; unsigned phone. Number; }; Directory. Listing entry, group[20]; city & state // // 9571234 phone # name of person street address city, state (no zip) 7 -digit phone number // entry in phone book // array of directory listings 44

Coordinates of a point: (Members need not have different types. ) 3. 73 –

Coordinates of a point: (Members need not have different types. ) 3. 73 – 2. 51 x coord. y coord. struct Point { double x. Coord, y. Coord; }; Point p, q; Test scores: (Members may be structured types — e. g. , arrays. ) 012345 83 79 92 85 id-number list of scores struct { Test. Record unsigned id. Number, score[4]; }; Test. Record student. Record, grade. Book[30]; 45

Hierarchical (or nested) structs Since the type of a member may be any type,

Hierarchical (or nested) structs Since the type of a member may be any type, it may be another struct. John Q. Doe name 123 Calvin Rd. Detroit, MI street city & state Directory. Listing 95714 zip May 17 1975 3. 95 92. 5 month day year gpa credits - Date real struct Personal. Info { Directory. Listing ident; Date birth; double cum. GPA, credits; }; Personal. Info student; 46

Some Properties: The scope of a member identifier is the struct in which it

Some Properties: The scope of a member identifier is the struct in which it is defined. Consequences: — A member identifier may be used outside the struct for some other purpose. e. g. int month; // legal declaration alongside Date — A member cannot be accessed outside the struct just by giving its name. e. g. year will not yield anything unless there is a year declared outside the Date struct. Direct access to members of a struct (or class) is implemented using member access operators: one of these is the dot operator (. ) struct_var. member_name 47

Examples: Input a value into the month member of birthday cin >> birth. Day.

Examples: Input a value into the month member of birthday cin >> birth. Day. month; Calculate y coordinate of a point on y = 1/x if (p. x. Coord != 0) p. y. Coord = 1. 0 / p. x. Coord; Sum the scores in student. Record double sum = 0; for (int i = 0; i < 4; i++) sum += student. Record. score[i]; Output the name stored in student cout << student. ident. name << endl 48

A Quick Look at Unions (p. 68) Declaration: Like a struct, but replace "struct"

A Quick Look at Unions (p. 68) Declaration: Like a struct, but replace "struct" with "union": union Type. Name ¬¾¾¾¾¾¾ Type. Name is optional { declarations of members //of any types }; A union differs from a struct in that the members share memory. Memory is (typically) allocated for the largest member, and all the other members share this memory 49

Unions can be used to define structs that have some common members — a

Unions can be used to define structs that have some common members — a fixed part — and a variant part that makes it possible for the fields of a struct to differ from one data value to the next. For example to process a file of information about various categories of people: John Doe 40 M January 30 1980 Mary Smith Doe 8 Fred Jones 17 S T Jane Vander. Van 24 D February 21 1998 N Peter Vander. Van 25 W February 22 1998 Y : : <——— name, age, marital status (married) <——— wedding date <——— spouse, # dependents <——— name, age, marital status (single) <——— available <——— name, age, marital status (divorced) <——— divorce date, remarried (No)] <——— name, age, marital status (widower) <——— date became a widower, remarried (Yes) 50

struct Date { string month; short day, year; }; struct Married. Info { Date

struct Date { string month; short day, year; }; struct Married. Info { Date wedding; string spouse short dependents; }; struct Single. Info { bool available; }; struct Was. Married. Info { Date divorce. Or. Death; char remarried; }; struct Personal. Info { string name; short age; char mar. Status; // Tag: S = single, M = married, // W = was married union { Married. Info married; Single. Info single; Was. Married. Info was. Married; }; }; Personal. Info person; 51

Structs with variant parts aren't used much anymore. (p. 69) Instead, in OOP languages:

Structs with variant parts aren't used much anymore. (p. 69) Instead, in OOP languages: Encapsulate the common information in a base class. Use inheritance to build derived classes for the variants (Derived classes inherit all of the members of the base class. ) 52

Address translation for structs and unions is similar to that for arrays, except that

Address translation for structs and unions is similar to that for arrays, except that different field types require using a summation to calculate the offsets. (p. 70) If a struct s has fields f 1, . . . , fn, requiring w 1, . . . , wn cells of storage, respectively: Address of s. fk = base address of s + offset = base address of s + For structs that contain unions: Allocate space for the largest variant, and then overlay variants in this space. 53

A commercial for OOP: Two programming paradigms • Procedural: ( C, FORTRAN, and Pascal

A commercial for OOP: Two programming paradigms • Procedural: ( C, FORTRAN, and Pascal ) – Action-oriented — concentrates on the verbs of a problem's specification – Programmers: • Identify basic tasks to be performed to solve problem • Implement the actions required to do these tasks as subprograms (procedures/functions/ subroutines) • Group these subprograms into programs/modules/libraries, which together make up a complete system for solving the problem • Object-oriented: ( C++, Java, and Smalltalk) – Focuses on the nouns of a problem's specification – Programmers: • Determine what objects are needed for a problem and how they should work together to solve the problem. • Create types called classes made up of data members and function members to operate on the data. Instances of a type (class) are called objects. 54

Creating a Data Type in a procedural (C-type) language (See pp. 74 -78) Problem:

Creating a Data Type in a procedural (C-type) language (See pp. 74 -78) Problem: Create a type Time for processing times in standard hh: mm AM/PM form and in military-time form. Data Members: Hours (1, . . . , 12) Minutes (0, 1, 2, . . . , 59) AM or PM indicator ('A' or 'P') Mil. Time (military time equivalent) Some Operations : 1. Set the time 2. Display the time 3. Advance the time 4. Determine if one time is less than another time. Implementation: 1. Need storage for the data members — use a struct 2. Need functions for the operations. 3. "Package" declarations of these together in a header file. 55

/** Time. h -----------------------------This header file defines the data type Time for processing time.

/** Time. h -----------------------------This header file defines the data type Time for processing time. Basic operations are: Set: To set the time Display: To display the time Advance: To advance the time by a certain amount Less. Than: To determine if one time is less than another ----------------------------------*/ #include <iostream> using namespace std; struct Time { unsigned hour, minute; char AMor. PM; unsigned mil. Time; }; Notice the documentation! // 'A' or 'P' // military time equivalent 56

/* Set sets the time to a specified values. Notice the * docu* Receive:

/* Set sets the time to a specified values. Notice the * docu* Receive: Time object t mentation! * hours, the number of hours in standard time * minutes, the number of minutes in standard time * AMPM ('A' if AM, 'P' if PM * Pass back: The modified Time t with data members set to * the specified values *********************************/ void Set(Time & t, unsigned hours, unsigned minutes, char AMPM); /* Display displays time t in standard and military format using * output stream out. * Receive: Time t and ostream out * Output: The time T to out * Pass back: The modified ostream out *********************************/ void Display(const Time & t, ostream & out); /* Advance increments a time by a specified value. * * Receive: Time object t * hours, the number of hours to add * minutes, the number of minutes to add * Pass back: The modified Time t with data members incremented * by the specified values *********************************/ void Advance(Time & t, unsigned hours, unsigned minutes); 57

/* Determine if one time is less than another time. * * Receive: Times

/* Determine if one time is less than another time. * * Receive: Times t 1 and t 2 * Return: True if t 1 < t 2, false otherwise. *********************************/ bool Less. Than(const Time & t 1, const Time & t 2); //===== Time. cpp -- implements the functions in Time. h ===== #include "Time. h" /*** Utility functions -- might be added as basic operations later ***/ int To. Military(unsigned hours, unsigned minutes, char AMPM); void To. Standard(unsigned military, unsigned & hours, unsigned & minutes, char& AMPM); //. . . Definitions of Set, Display, Advance, Less. Than, To. Military, // and To. Standard go here --- see the text. 58

istringstream ostringstream 59

istringstream ostringstream 59