0% found this document useful (0 votes)
30 views

13.1 User-Defined Data Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

13.1 User-Defined Data Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

13.

1 User defined data


types

Computing Department
Question

What is a data type?

Computing Department
User-defined data types

This refers to the programmer as the user,


not the person who is using the program.

Computing Department
Non-composite types
• These must be defined explicitly before being used and do not involve
a reference to another type. There are two types: enumerated and
pointer.

Computing Department
Enumerated data type
• An enumeration is a complete, ordered listing of all of the possible
items in a collection. It defines a list of possible values.
• The values are ordinal which means they have a specific order. This
can be useful when programming.
• The values can look like string values but they are not.

Computing Department
Enumerated data type example
TYPE
Days = (Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday, Sunday)

DECLARE Weekend : Boolean


DECLARE Day : Days
Weekend = TRUE IF Day > Friday

Computing Department
Pointer data type
• A pointer data type is used to reference a memory location.
• Arrays normally reserve space in memory before it is needed which
can be wasteful. Pointers try to overcome this by reserving memory
as and when required. This is known as dynamic memory allocation.
• A pointer is a variable which stores the address of another variable.
• The pseudocode is
TYPE
<Identifier> = ^<Data-Type>

Computing Department
Pointer data type example
TYPE
MyPointer = ^String

The declaration of the variable does not require the caret symbol (^):
• DECLARE NameAddress: MyPointer
• DECLARE Name: STRING

Accessing the value stored in the address pointed by the pointer:


• Name ← NameAddress^
Computing Department
Composite types
• A composite user-defined data type has a definition with reference to
at least one other type. There are 3 types: record, set and
object/class.

Computing Department
Record data type
• This is a data type that contains a fixed number of components, which
can be of different types. It combines different built-in data types in a
record like structure.

Computing Department
Record data type example
TYPE
EmployeeRecord
DECLARE EmployeeFirstName : STRING
DECLARE EmployeeFamilyName : STRING
DECLARE DateEmployed : DATE
DECLARE Salary : CURRENCY
ENDTYPE

Computing Department
Set data type
• This is a collection of particular values with no repetitions in any
order. It allows a program to create sets and to apply the following
mathematical operations:
• Union – in either
• difference – in one but not the other
• intersection – in both
• include an element in the set
• exclude an element from the set
• check whether an element is in a set
Computing Department
Set data type example
• DECLARE Title: SET OF (Mr, Mrs, Ms, Miss, Dr)

Computing Department
Object/Class
A data type in which a record and methods that act on it´s properties
are combined in to one. This is used only in object oriented
programming.

Computing Department
Why are user-defined data types
necessary
• Makes the program easier to understand
• Makes the program less error prone

Computing Department
Task
Data is to be stored for nurses who work in a hospital:
• Nurse ID (5 digit code)
• Family name
• First names
• Date the nurse first registered
• Whether or not the nurse has completed the health and safety course

• Design a user defined data type for the nurse data

Computing Department
Answer
TYPE
Nurses
DECLARE NurseID : INTEGER
DECLARE FamilyName : STRING
DECLARE FirstNames : STRING
DECLARE DateRegistered : DATE/TIME
DECLARE HealthAndSafety: BOOLEAN
ENDTYPE

Computing Department
Further Questions

Computing Department

You might also like