CIE AS & A Level Computer Science, section 13.1 (User-defined data types)
Composite types
A composite type is built from other data types. The main examples are records, sets and classes.
Record
A record groups items of different data types under one name. Each item is a field.
TYPE TStudent
DECLARE Name : STRING
DECLARE Age : INTEGER
DECLARE Passed : BOOLEAN
ENDTYPE
DECLARE Pupil : TStudent
Pupil.Name ← "Sam"
Pupil.Age ← 17
Set
A set is an unordered collection of different values with no duplicates. Sets are used to test membership and for union, intersection and difference.
TYPE SetOfInt = SET OF INTEGER
DEFINE A (2, 4, 6) : SetOfInt
DEFINE B (4, 6, 8) : SetOfInt
Class
A class defines the data (attributes) and the methods that act on it, and is the basis of object-oriented programming (see section 20).
Abstract data types
An abstract data type (ADT) is a collection of data and the operations allowed on it, described without saying how it is stored (for example a stack or a queue). Composite types such as records and pointers are used to build ADTs in a program.
Records or arrays
An array holds items of one type. A record can hold fields of different types.
Common mistake: thinking a set keeps order or allows duplicates. It does neither. Also, a record groups different data types, whereas an array needs every element to be the same type.
Exam tip: when defining a record, write the TYPE ... ENDTYPE with each field and its data type. Use dot notation to access a field, for example Pupil.Name.
Check yourself
What is the difference between a record and an array?
A record can hold fields of different data types, whereas an array holds elements of the same type.
What is the union of the sets {1, 2} and {2, 3}?
{1, 2, 3}.
What is an abstract data type?
A collection of data together with the operations allowed on it, described without stating how it is implemented.