登录

User-defined data types

Syllabus requirements

The AS & A Level (9618) syllabus requires candidates to understand that data structures that are not available in a particular programming language need to be constructed from the data structures that are built-in within the language. User-defined data types need to be defined. The syllabus requires candidates to use and define non-composite data types such as enumerated and pointer and composite data types such as record, set, class/object. Abstract Data Types (ADTs) stack, queue, linked list, dictionary and binary tree are also defined as composite data types.

Defining user-defined data types

Non-composite data type – Enumerated

A user-defined non-composite data type with a list of possible values is called an enumerated data type. The enumerated type should be declared as follows:

TYPE <identifier> = (value1, value2, value3, ...)
//Example – declaration of enumerated type
//This enumerated type holds data about seasons of the year.
TYPE Season = (Spring, Summer, Autumn, Winter)

Non-composite data type – Pointer

A user-defined non-composite data type referencing a memory location is called a pointer. The pointer should be declared as follows:

TYPE <identifier> = ^<data type>

The ^ shows that the variable is a pointer and the data type indicates the type of the data stored in the memory location.

//Example – declarations of pointer type
TYPE TIntPointer = ^INTEGER
TYPE TCharPointer = ^CHAR

Declaration of a variable of pointer type does not require the ^ (caret) symbol to be used.

//Example – declaration of a pointer variable
DECLARE MyPointer : TIntPointer

Composite data type

A composite data type is a collection of data that can consist of different data types, grouped under one identifier. The composite type should be declared as follows:

TYPE <identifier1>
    DECLARE <identifier2> : <data type>
    DECLARE <identifier3> : <data type>
    ...
ENDTYPE
//Example – declaration of composite type
//This user-defined data type holds data about a student.
TYPE Student
    DECLARE LastName : STRING
    DECLARE FirstName : STRING
    DECLARE DateOfBirth : DATE
    DECLARE YearGroup : INTEGER
    DECLARE FormGroup : CHAR
ENDTYPE

Using user-defined data types

When a user-defined data type has been defined it can be used in the same way as any other data type in

declarations.

Variables of a user-defined data type can be assigned to each other. Individual data items are accessed using dot notation.

//Example – using user-defined data types
//This pseudocode uses the user-defined types Student, Season and TIntPointer defined in the previous section.
DECLARE Pupil1 : Student
DECLARE Pupil2 : Student
DECLARE Form : ARRAY[1:30] OF Student
DECLARE ThisSeason : Season
DECLARE NextSeason : Season
DECLARE MyPointer : TIntPointer

Pupil1.LastName ← "Johnson"
Pupil1.Firstname ← "Leroy"
Pupil1.DateOfBirth ← 02/01/2005
Pupil1.YearGroup ← 6
Pupil1.FormGroup ← ꞌAꞌ
Pupil2 ← Pupil1

FOR Index ← 1 TO 30
    Form[Index].YearGroup ← Form[Index].YearGroup + 1
NEXT Index

ThisSeason ← Spring
MyPointer ← ^ThisSeason
NextSeason ← MyPointer^ + 1
// access the value stored at the memory address

登录