Class Crib Notes for CPSC 110                                Brent Dingle

                                    Monday, March 25, 2002

 

Reminders:

Quiz Wednesday.

Lab 5 due today and Tuesday

 

For Today:

Abstract Data Types (still chapter 8)

 

Data Types so far:

            Integer, Real, Char, Boolean, TEXT, etc.

 

Definition: A data type is called an abstract data type if the programmer who is using the data type does NOT have access to the details of how the operations are implemented.

 

In Pascal there exists a way to declare new types using the keyword: TYPE

The format for this goes like:

 

new_type_name = [new_type_def]        the square brackets are not actually used

 

The format of a program now becomes:

Program LA;

CONST

    { constants declared }

TYPE

   { types declared }

VAR

   { global variables declared }

 

{ procedures and functions declared }

 

Begin { main body begins }

   { whatever }

End.

 

Example – Subrange Types:

Subrange types are intervals of predefined types whose members can be counted.

 

Example:

TYPE

   SHORT_RANGE = 4..10;     { notice the host type is integer }

 

Notice:

Subrange types CANNOT be of type real (try counting all real numbers).

 

Example:

TYPE

   ODD_RANGE = -5..15;   { so negative numbers can be used }

VAR

   odd_score : ODD_RANGE;

   score : integer;

 

Subrange Compatibility:

odd_score = 425;   { this will NOT compile }

odd_score = score;  { this will compile but may not work if score < -5 or score > 15 }

 

Example:

TYPE

   CAP_LETTERS = ‘A’..’Z’;

VAR

   letter : CAP_LETTERS;

   any_char : char;

 

Subrange types lead us to ordinal types:

 

ORDINAL TYPES:

Definition:

Types whose values can be specified by a list are ordinal types.

(also called scalar types and types with a finite number of elements)

 

Examples:

integer

char

boolean

PRIME_COLORS = (red, green, blue)

 

PRIME_COLORS would be defined using TYPE, e.g.

TYPE

   PRIME_COLORS = (red, green, blue);

VAR

   some_color : PRIME_COLORS;

 

ENUMERATED TYPES:

Definition:

Enumerated types are user (programmer) defined ordinal types.

 

So PRIME_COLORS above would be an enumerated type.

 

Enumerated Types have TWO properties:

  1. The order of their members
  2. The names of their members