2. Computer Science 9618 2
13.1 User-defined data types
Candidates should be able to:
• Show understanding of why user-defined types are
necessary
• Define and use non-composite data types
• Define and use composite data types
• Choose and design an appropriate user-defined data
type for a given problem
13.1 User-defined data types
3. Computer Science 9618 3
13.1 User-defined data types
A user-defined data type is a data type for which the programmer has
included the definition in the program. Once the data type has been
defined, variables can be created and associated with the user-defined
data type.
There are two categories of user defined data types.:
1. Non-composite data type
a. Enumerated data type
b. Pointer data type
2. Composite
a. Record data type
b. Set data type
c. Objects and classes
5. Computer Science 9618 5
User-defined datatypes:
Non-composite user-defined data type
A non-composite data type is defined without referencing another data type.
They don’t combine different built-in data types in one data type.
Non-Composite data types are:
• Enumerated data type
• Pointers
6. Computer Science 9618 6
ENUMERATED DATATYPE
User-defined datatypes:
Non-composite user-defined data type
7. Computer Science 9618 7
ENUMERATED DATATYPE
User-defined datatypes:
Non-composite user-defined data type
Enumerated Datatype
An enumerated data type defines a list of possible values.
8. Computer Science 9618 8
ENUMERATED DATATYPE
User-defined datatypes:
Non-composite user-defined data type
Pseudocode of data Type for Working Days can be declared as TWdays:
9. Computer Science 9618 9
ENUMERATED DATATYPE
User-defined datatypes:
Non-composite user-defined data type
Variables can then be declared and assigned values, for example:
10. Computer Science 9618 10
ENUMERATED DATATYPE
User-defined datatypes:
Non-composite user-defined data type
It is important to note that the values of the enumerated type look like string values but they are
not. They must not be enclosed in quote marks. This makes the second example much more
useful because the ordering can be put to many uses in a program.
For example, a comparison statement can be used with the values and variables of the
enumerated data type:
11. Computer Science 9618 11
POINTER DATATYPE
User-defined datatypes:
Non-composite user-defined data type
12. Computer Science 9618 12
POINTER DATATYPE
User-defined datatypes:
Non-composite user-defined data type
• A pointer data type is used to reference a memory location. This data type needs to have
information about the type of data that will be stored in the memory location.
• In pseudocode the type definition has the following structure, in which ^ shows that the type
being declared is a pointer and <TYPENAME> is the type of data to be found in the memory
location:
• for example INTEGER or REAL: or any user-defined data type.
TYPE <pointer> = ^<Typename>
13. Computer Science 9618 13
POINTER DATATYPE
User-defined datatypes:
Non-composite user-defined data type
It could then be used as follows:
monthPointer ← ^ thisMonth
If the contents of the memory location are required rather than the address of the memory
location, then the pointer can be dereferenced. For example, myMonth can be set to the
value stored at the address monthPointer is pointing to:
DECLARE myMonth : Tmonth
myMonth ← monthPointer^
14. Computer Science 9618 14
• An example of the definition of a pointer type which requires only
the identification of a data type for which the pointer is to be
used.
TYPE TIntegerPointer ← ^Integer
• An example of the declaration of a variable of the pointer data
type which does not require the use of the caret (^) symbol.
DECLARE MyIntegerPointer : TIntegerPointer
• An example of the declaration of two ordinary variables of type
integer and the assignment of a value for one of them.
DECLARE Number1, Number2 : INTEGER
Number1 ← 100
POINTER DATATYPE
User-defined datatypes:
Non-composite user-defined data type
15. Computer Science 9618 15
• An example of an assignment to a pointer variable of a
value which is the address of a different variable.
MyIntegerPointer ← @Number1
• An example of an assignment which uses the
‘dereferenced’ value which has been stored at the
address defined by the pointer variable. This assigns the
value 200 to Number2.
Number2 ← MyIntegerPointer^ * 2
POINTER DATATYPE
User-defined datatypes:
Non-composite user-defined data type
17. Computer Science 9618 17
RECORD DATATYPE
User-defined datatypes:
Composite user-defined data type
18. Computer Science 9618 18
RECORD DATATYPE
User-defined datatypes:
Composite user-defined data type
A record data type is the most useful and therefore most widely used. It allows the
programmer to collect together values with different data types when these form a
coherent whole.
As an example, a record could be used for a program using employee data. Pseudocode
for defining the type could be:
19. Computer Science 9618 19
RECORD DATATYPE
User-defined datatypes:
Composite user-defined data type
A variable can then be declared as follows:
individual data item can then be accessed using a dot notation:
A particular use of a record is for the implementation of a data structure
where one or possibly two of the variables defined are pointer variables.
20. Computer Science 9618 20
Record Data Type example
A Car Manufacturer and seller want to store details of cars. Details can be
stored in Records
RECORD DATATYPE
User-defined datatypes:
Composite user-defined data type
21. Computer Science 9618 21
SET DATATYPE
User-defined datatypes:
Composite user-defined data type
22. Computer Science 9618 22
A set is a given list of unordered elements that can use set theory operations such as
intersection and union. A set data type includes the type of data in the set. In
pseudocode, the type definition has this structure:
SET DATATYPE
User-defined datatypes:
Composite user-defined data type
TYPE <set-identifier>= SET OF <Base Type>
The variable definition for a set includes the elements of the set.
DEFINE <identifier> (value1, value2, value3, ... ) : <set-identifier>
23. Computer Science 9618 23
SET DATATYPE
User-defined datatypes:
Composite user-defined data type
A set of vowels could be declared as follows:
TYPE Sletter = SET OF CHAR
DEFINE vowel ('a', 'e', 'i', 'o', 'u') : Sletter
Or we can declare sets in Pseudocodes this way
Here is another example
TYPE Sizes = SET OF STRING
DEFINE SweaterSize (“XXL”,”XL”,”L”,”M”,”S”,”XS”): Sizes
OR
DEFINE Title : SET OF (“Mr”, “Mrs”,”Ms”,”Miss”,”Dr”)
24. Computer Science 9618 24
Set data type:
A set data type allows a program to create sets and to apply the mathematical
operations defined in set theory. The following is a representative list of the
operations to be expected:
• Union
• Difference
• Intersection
• include an element in the set
• exclude an element from the set
• Check whether an element is in a set.
SET DATATYPE
User-defined datatypes:
Composite user-defined data type
25. Computer Science 9618 25
CLASS DATATYPE
User-defined datatypes:
Composite user-defined data type
26. Computer Science 9618 26
CLASS DATATYPE
User-defined datatypes:
Composite user-defined data type
A class is a composite data type that includes variables of given data
types and methods (code routines that can be run by an object in that
class). An object is defined from a given class; several objects can be
defined from the same class. Classes and objects will be considered in
more depth in Paper 4 (Chapter 20)