User-Defined Types (Cambridge (CIE) A Level Computer Science): Revision Note
Exam code: 9618
What are user-defined data types?
A user-defined data type is a custom data type created by the programmer, built from existing types to match the specific needs of a program
It is built using primitive data types such as
INTEGER,STRING,BOOLEAN, etc
Why are user-defined types necessary?
Primitive types alone cannot model many real-world entities (e.g. a customer, a date, a linked list)
User-defined types let you:
Group related data under a single identifier (record)
Restrict a variable to a fixed set of valid values (enumerated)
Build dynamic data structures such as linked lists (pointer)
Make code more readable, reusable and easier to validate
Category | Definition | Examples |
|---|---|---|
Non-composite | A type defined without referencing another data type | Enumerated, pointer |
Composite | A type that references other data types in its definition, grouped under a single identifier | Record, set, class |
Non-composite
A non-composite data type is defined without referencing any other data type
It can be a primitive type available in the language, or a user-defined type (enumerated or pointer)
What is an enumerated data type?
An enumerated data type is a non-composite, user-defined type consisting of a list of all possible values the variable can take
The values are ordered: they have an implied sequence based on the order they are declared, so comparisons work (e.g.
Mondaycomes beforeTuesday)Use an enumerated type to restrict a variable to a known, fixed list, improving readability and reducing invalid values
Pseudocode | |
|---|---|
|
Example code
TYPE DayType = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
DECLARE Today : DayType
Today ← Monday
IF Today = Saturday OR Today = Sunday THEN
OUTPUT "It's the weekend!"
ELSE
OUTPUT "It's a weekday."
ENDIF
Todaycan only hold one of the seven ordered values inDayType
What is a pointer data type?
A pointer is a non-composite, user-defined type that stores a memory address only, rather than the data itself
Its definition also indicates the type of data stored at that memory location
Pointers are used to build dynamic data structures such as linked lists
Pseudocode | |
|---|---|
|
The
^shows the type is a pointer<data type>is the type of data stored at the address
Example code
TYPE Node
DECLARE Data : INTEGER
DECLARE Next : ^Node
ENDTYPE
DECLARE StartPtr : ^Node
DECLARE NewNode : ^Node
NEW(NewNode) // allocate memory for a new node
NewNode^.Data ← 10 // ^ accesses the contents at the address
NewNode^.Next ← NULL // NULL marks the end of the list
StartPtr ← NewNode
Composite
A composite data type is a collection of data made up of multiple elements, of the same or different data types, grouped under a single identifier
It references other data types in its definition (primitive or other user-defined types)
The three composite user-defined types you need are record, set and class
What is a record?
A record is a composite type that groups together a fixed number of related items (fields), which may be of different data types, to form a single new type
Access a field using dot notation:
RecordName.FieldName
Pseudocode | |
|---|---|
|
Example code
TYPE Order
DECLARE AccountNumber : INTEGER
DECLARE OrderNumber : INTEGER
DECLARE OrderPrice : REAL
DECLARE OrderDate : DATE
ENDTYPE
DECLARE ThisOrder : Order
ThisOrder.AccountNumber ← 4521
ThisOrder.OrderPrice ← 19.99
What is a set?
A set is a composite type holding a list of unordered elements
All elements must be of the same data type, and the set definition includes the data type it uses
Set theory operations such as union and intersection can be applied to the elements
Pseudocode | |
|---|---|
|
Example code
TYPE SymbolSet = SET OF CHAR
DEFINE Operators ('+', '-', '*', '/', '^') : SymbolSet
IF '+' ISIN Operators THEN
OUTPUT "Plus is in the set"
ENDIF
What is a class?
A class is a composite type that groups together both data (attributes) and the methods that operate on that data, under a single identifier
It is used as a template to create objects in object-oriented programming
Full detail on defining and using classes is covered in Object-Oriented Programming
Choosing a user-defined data type
Exam questions may ask you to choose and design an appropriate type for a problem
Match the type to what the data looks like:
If the data... | Use a... |
|---|---|
can only be one of a fixed, named list of values | Enumerated type |
needs to link to another item / build a dynamic structure | Pointer |
groups several related fields of different types | Record |
is an unordered collection with no duplicates | Set |
combines data with the operations that act on it | Class |
Examiner Tips and Tricks
Composite vs non-composite" is about whether the type references other types, not whether it holds one value or many. State this precisely
For enumerated types, remember to say the values are ordered, that is a mark point
For a "describe the set/record data type" question, include that it is composite, that it references/uses other data types, and the specific detail (set = unordered, same type, supports union/intersection; record = fixed number of related items)
Unlock more, it's free!
Was this revision note helpful?
Build on this topic