User-defined data types
| English | Chinese | Pinyin |
|---|---|---|
| user-defined types | 用户定义类型 | yòng hù dìng yì lèi xíng |
| non-composite | 非组合 | fēi zǔ hé |
| composite | 组合 | zǔ hé |
| enumerated type | 枚举类型 | méi jǔ lèi xíng |
| pointer | 指针 | zhǐ zhēn |
| dereference | 解引用 | jiě yǐn yòng |
| record | 记录 | jì lù |
| set | 集合 | jí hé |
| class | 类 | lèi |
| attributes | 属性 | shǔ xìng |
| methods | 方法 | fāng fǎ |
A type that makes the mistake impossible
- A hospital system stored a patient's blood group as a
STRING. Someone typed "0+" instead of "O+": a zero for a letter. The field accepted it, because a string accepts anything. - Nothing in the program could tell that "0+" was not a blood group, so the error travelled untouched to the point where it mattered.
- Give the field a type whose values are exactly the eight blood groups, and the assignment is rejected the moment it is written, by the compiler, before the program ever runs.
- That is what user-defined types 用户定义类型 buy you. This lesson is the ones the syllabus names, and how to choose between them.
Why user-defined types are needed
- A built-in
STRINGlets you store nonsense in a field that should hold one of a few legal values; a user-defined type restricts it, so the compiler catches more mistakes. - Real entities are usually a collection of values of different types, and a built-in type can hold only one.
DECLARE MyTaxi : Vehicleis self-documenting: it says what the variable is, whereDECLARE MyTaxi : STRINGsays only how it is stored.
Why are user-defined types needed? Select all that apply.
Restriction, grouping and self-documentation. Speed is not the reason; correctness and clarity are.
Non-composite types: enumerated
- A non-composite 非组合 type holds a single value; a composite 组合 type groups several values together.
- An enumerated type 枚举类型 has values that are a fixed list of named constants. The names are values of the new type, stored internally as small integers.
TYPE Vehicle = (M100, M230, T101, T102, T120, T150)
DECLARE MyTaxi : Vehicle
MyTaxi ← T102 // anything outside the list is rejected
- Uses: days of the week, colours, status codes, blood groups: any field whose legal values are a short known list.

Six named values, and nothing else is assignable
An enumerated type holds:
An enumerated type restricts a variable to one of a fixed set of named values (e.g. days of the week).
Non-composite types: pointer
- A pointer 指针 holds the memory address of another variable, or
NULLmeaning "no target". - To dereference 解引用 a pointer, written
p^, is to reach the variable it points at.
TYPE PNode = ^TNode // a pointer to a TNode
DECLARE p : PNode
p ← NEW TNode
p^.Value ← 42 // dereference, then reach the field
- Pointers are what make dynamic structures possible: a linked list is nodes joined by pointers, and a tree is nodes with two. They also pass a large record to a procedure without copying it.

The variable holds where the data is, not the data
A pointer stores the memory address of another variable, and dereferencing it (e.g. p^) reaches the variable it points to.
Pointers (with NULL marking "points to nothing") are how dynamic structures like linked lists and trees are built.
Reaching the variable a pointer points at, written p^, is called ____.
The pointer holds an address; dereferencing follows it to the data. That is how linked lists and trees are built.
Composite types
- A record 记录 groups fields of different types under one name, in a
TYPE … ENDTYPEblock. You met it in topic 10. - A set 集合 is an unordered collection of unique values, with the operations add, remove, membership test, union and intersection.
DECLARE Available : SET OF Colour
Available ← {Red, Blue}
IF Green IN Available THEN …
- A class 类 and its instances, objects, combine data fields, the attributes 属性, with the operations on them, the methods 方法.

Fields of different types, one identifier
Match each user-defined type to what it holds.
Enumerated = fixed named values; pointer = an address; class = data + behaviour; set = unique unordered values.
A set is an ordered collection in which a value may appear more than once.
A set is unordered and its values are unique. An array is the ordered structure that allows repeats.
The class
CLASS Taxi
PRIVATE Capacity : INTEGER
PUBLIC FUNCTION GetCapacity() RETURNS INTEGER
RETURN Capacity
ENDFUNCTION
ENDCLASS
- A class is the definition; an object is an instance of it, one actual taxi with its own capacity.
- What distinguishes it from a record is the methods: a record is data alone, a class is data and the behaviour that belongs with it.
A class combines:
A class bundles attributes and methods; an object is an instance of a class.
What distinguishes a class from a record?
Both group fields; only the class carries behaviour. An object is one instance of the class.
Worked example: choose the type and justify it
- A booking system stores, for each booking, a status that must be one of Requested, Confirmed or Cancelled. Which user-defined type, and why?
- An enumerated type: the legal values are a short fixed list, so the type prevents any other value being assigned and the code reads as the domain does.
- And for the booking itself: a reference, a date, a passenger count and a price? A record, because the values are of different types and always describe one booking together.
- And for the set of seats still free? A set: unordered, no duplicates, and membership is the operation you need.
Choosing between them
| Need | Type |
|---|---|
| a value from a short fixed list | enumerated |
| indirection, or a structure that grows | pointer |
| several fields of different types describing one thing | record |
| an unordered collection of unique values | set |
| state and the behaviour that acts on it | class |
Programming concept lab
Connect examples to the programming idea they show.
For a variable that must be one of {Red, Green, Blue}, the best type is:
A fixed list of allowed values is exactly what an enumerated type is for.
Marks that slip away
- An enumerated type's values are named constants, not strings.
MyTaxi ← "T102"is a different thing and is wrong. - A pointer holds an address, not the value. Dereferencing is the step that reaches the value.
- A set is unordered with no duplicates; an array is ordered and may repeat. The question's wording tells you which.
- What makes a class more than a record is its methods. Say that when asked for the difference.
You've got it
- user-defined types restrict what can be stored, group values that belong together, and make code self-documenting
- non-composite: enumerated (a fixed list of named constants) and pointer (an address, reached by dereferencing)
- composite: record (fields of different types), set (unordered, unique), class/object (attributes plus methods)
- choose from the need, and justify with the property that fits: a fixed list, indirection, mixed fields, uniqueness, or state with behaviour