Built-in types such as INTEGER and STRING cannot describe everything a program needs. A user-defined type lets the programmer create exactly the type the problem calls for, from a list of named values to a record, a set or a class.
A user-defined data type is one the programmer defines, derived from the built-in types, because the built-in types alone do not model the data in the problem. A non-composite type is defined without reference to other user types: an enumerated type lists every value it can take, in order, and a pointer type holds the memory address of a value of a stated type. A composite type groups other data types under one identifier: a record (fields of different types), a set (unordered, no duplicates, supports set operations such as membership) and a class, whose objects combine attributes with methods.
User-defined types are necessary because a language cannot predict every kind of data. Defining your own type makes code easier to read, lets the translator reject invalid values and keeps the program close to the problem. The two non-composite types in the syllabus are enumerated and pointer.
TYPE Season = (Spring, Summer, Autumn, Winter) DECLARE ThisSeason : Season DECLARE NextSeason : Season ThisSeason ← Summer TYPE TIntPointer = ^INTEGER DECLARE Score : INTEGER DECLARE MyPointer : TIntPointer Score ← 40 MyPointer ← ^Score // MyPointer now holds the address of Score MyPointer^ ← MyPointer^ + 2 // the value at that address becomes 42
Summer > Spring is TRUE and ThisSeason + 1 gives the next value. The values are identifiers, not strings: no quotation marks.TYPE TIntPointer = ^INTEGER says the variable will hold the address of an INTEGER. The variable itself is declared without a caret.^Score means "the address of Score".MyPointer^ means "the value stored at the address held in MyPointer".Score ← 40 stores 40 in the memory location called Score.MyPointer ← ^Score stores the address of Score in MyPointer, not the value 40.MyPointer^ ← MyPointer^ + 2 reads the value at that address (40), adds 2 and writes 42 back to the same location, which is Score.Grade for the values Distinction, Merit, Pass and Fail, then declare a variable Result of that type and assign it Merit.TYPE Grade = (Distinction, Merit, Pass, Fail), DECLARE Result : Grade, Result ← Merit. No quotation marks round Merit: it is a value of the type, not a string.Writing TYPE Season = ("Spring", "Summer", ...) loses the mark. Enumerated values are not strings, so they have no quotation marks.
A composite type is a collection of data that can consist of one or more data types, grouped under one identifier. Records were met at AS; A Level adds sets and classes.
TYPE StudentRecord
DECLARE LastName : STRING
DECLARE YearGroup : INTEGER
DECLARE FormGroup : CHAR
ENDTYPE
TYPE LetterSet = SET OF CHAR
DEFINE Vowels ('A', 'E', 'I', 'O', 'U') : LetterSet
CLASS Ticket
PRIVATE Seat : STRING
PRIVATE Price : REAL
PUBLIC PROCEDURE NEW(GivenSeat : STRING, GivenPrice : REAL)
Seat ← GivenSeat
Price ← GivenPrice
ENDPROCEDURE
PUBLIC FUNCTION GetPrice() RETURNS REAL
RETURN Price
ENDFUNCTION
ENDCLASS
DECLARE MyTicket : Ticket
MyTicket ← NEW Ticket("F12", 18.50)
OUTPUT MyTicket.GetPrice()
| Type | What it holds | Key property |
|---|---|---|
| Record | fields, which may be of different data types | fields reached by dot notation, e.g. Pupil1.YearGroup |
| Set | a collection of values of one base type | unordered, no duplicates; supports membership tests, union, intersection and difference |
| Class | attributes (data) and methods (procedures and functions) | a template: each object is an instance created with NEW; PRIVATE attributes are reached only through methods |
A set is not an array: it has no index and no order, and adding a value that is already present changes nothing. Do not describe a set as "a list of values with positions".
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which of these is a non-composite user-defined data type?
MyPointer is of type TIntPointer = ^INTEGER and holds the address of Total. What does MyPointer^ give?
Which statement correctly defines an enumerated type?
Which property distinguishes a set from a 1D array?
Exam questions describe a situation and ask you to choose and design a suitable user-defined type. Match the shape of the data to the type, then write the definition in exact 9618 pseudocode.
TYPE RoomType = (Single, Double, Twin, Suite).TYPE RoomType = (Single, Double, Twin, Suite)
TYPE RoomRecord
DECLARE RoomNumber : INTEGER
DECLARE Kind : RoomType
DECLARE Rate : REAL
DECLARE Occupied : BOOLEAN
ENDTYPETYPE SymbolSet = SET OF CHAR and DEFINE Symbols ('!', '?', '#', '$') : SymbolSet.Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.
Why are user-defined data types needed?
TYPE Day = (Mon, Tue, Wed, Thu, Fri). Today holds Tue. What is Today + 2?
Which line creates a new object of class Ticket?
A program must store, for each loan, a book ID, the date due back and whether a fine is owed. Which type is most suitable?
This skill is now recorded on your profile. Keep the chain going.