Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.

Similar presentations


Presentation on theme: "Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative."— Presentation transcript:

1 Data Types Chapter 6: Data Types Lectures # 12

2 Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record Types Union Types Pointer and Reference Types

3 Record types A record is a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names. Records were introduced in COBOL and are implemented in most modern programming languages. Chapter 6: Data Types 3

4 Definition of Records :COBOL COBOL uses level numbers to show nested records; others use recursive definition. 01 EMP-REC. 02 EMP-NAME. 05 FIRST PIC X(20). 05 MID PIC X(10). 05 LAST PIC X(20). 02 HOURLY-RATE PIC 99V99. Chapter 6: Data Types 4

5 Definition of Records : Ada type Emp_Rec_Type is record First: String (1..20); Mid: String (1..10); Last: String (1..20); Hourly_Rate: Float; end record; Emp_Rec: Emp_Rec_Type; Chapter 6: Data Types 5

6 References to Records Fully qualified references must include all record names. Elliptical references allow leaving out record names as long as the reference is unambiguous, Example in COBOL: FIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the employee’s first name. Pascal and Modula 02 provide a with clause to abbreviate references. Chapter 6: Data Types 6

7 Records vs. Arrays Array elements all have the same type and are processed in the same way. Record elements may have different types and are processed using operations unique to that type. Access to array elements is slower than access to record fields, because:  Subscripts are dynamically calculated.  Field offsets are statically calculated. Chapter 6: Data Types 7

8 Unions Types A union is a type whose variables are allowed to store different type values at different times during execution. A free union is a union for which there is no language support for type checking. Programmers are allowed complete freedom from type checking in their use. FORTRAN, C, and C++ have this kind of union. A discriminated union has a field that indicates the type of the data in the union. This field is called a discriminant. Pascal and Ada have discriminated unions.  Algol 68 was the first language provide discriminated union. Chapter 6: Data Types 8

9 Pascal variant records Example of a Pascal record with a variant part: type shape = (circle, triangle, rectangle); colors = (red, green, blue); figure = record filled : boolean; color : colors; case form : shape of circle : (diameter : real); triangle : (leftside : integer; rightside : integer; angle : real); rectangle: (side1, side2 : integer); end; var myfigure : figure; Variant part Chapter 6: Data Types 9

10 Pascal does no runtime type checking of variant records, so inconsistent unions can be created: myfigure.filled := true; myfigure.color := red; myfigure.form := circle; myfigure.side1 := 5; It is the programmer’s responsibility to ensure that the data remains consistent. Furthermore, the tag field is optional! Ada’s discriminated records are type-safe because:  Tag field must be present.  All assignments to the union must include the tag field. Pascal variant records (cont.) Chapter 6: Data Types 10

11 Ada discriminated unions type shape is (circle, triangle, rectangle); type colors is (red, green, blue); type figure (form : shape) is record filled : boolean; color : colors; case form is when circle => diameter : float; when triangle => leftside : integer; rightside : integer; angle : float; rectangle: side1, side2 : integer; end case; end record; Chapter 6: Data Types 11

12 Ada discriminated union (cont.) Figure_1 : figure; Figure_2 : Figure(form => triangle); Figure_1 := (filled => true,color => blue,form => rectangle, side_1 => 12,side_2 => 3); Figure_1 is declared to be unconstrained variant record that has no initial value. Figure_2 is constrained to be a triangle and cannot be changed to another variant. Chapter 6: Data Types 12

13 Evaluation of Unions Potentially unsafe construct:  Do not allow type checking. Java and C# do not support unions.  Reflective of growing concerns for safety in programming language. Chapter 6: Data Types 13


Download ppt "Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative."

Similar presentations


Ads by Google