Sample Notes: User-Defined Data Types
A2 Level Computer Science – Topic 13.1: User-defined Data Types
1. Purpose of User-defined Data Types
- Built-in types (e.g. Integer, Real, Boolean, Char) are limited for complex real-world modelling.
- User-defined data types allow programmers to define custom formats suited to specific problems.
- Essential for:
- Abstraction: grouping related data together
- Modelling complex systems (e.g. banking system, student record system)
- Readability and maintainability of code
2. Non-composite User-defined Types
A. Enumerated Types (enum)
- Defines a list of named constant values.
- Improves code readability and prevents invalid data.
- Underlying values are integers starting from 0 unless specified.
Example:
TYPE DayOfWeek = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
Usage:
VAR today: DayOfWeek
today := Friday
B. Pointer Types
- A pointer stores the memory address of a variable or object.
- Useful in dynamic data structures (e.g. linked lists, trees).
- In Pascal-like pseudocode:
TYPE NodePointer = ^Node
3. Composite User-defined Types
A. Record
- Groups different types of data under one name.
- Each element in a record is called a field.
- Similar to a struct in C or a class without methods in OOP.
Example:
TYPE Student = RECORD
ID: INTEGER
Name: STRING
Grade: CHAR
END RECORD
Usage:
VAR s: Student
s.ID := 101
s.Name := "Ali"
s.Grade := 'A'
B. Set
- Unordered collection of distinct elements of the same type.
- Supports operations like union, intersection, and membership testing.
Example:
VAR S1, S2: SET OF 1..10
S1 := [1, 2, 3]
S2 := [3, 4, 5]
Operations:
S1 ∪ S2
= [1, 2, 3, 4, 5]S1 ∩ S2
= [3]
C. Class/Object
- Part of Object-Oriented Programming (OOP).
- Combines data (attributes) and functions (methods).
- Supports encapsulation, inheritance, and polymorphism.
Class Example:
CLASS Car
PRIVATE
brand: STRING
speed: INTEGER
PUBLIC
PROCEDURE Accelerate()
BEGIN
speed := speed + 10
END PROCEDURE
END CLASS
Object Example:
VAR myCar: Car
myCar.Accelerate()
4. Choosing Appropriate User-defined Types
Problem Example 1: Student Data
- Requirements: Store student name, age, subjects taken.
- Suggested: Use a Record with fields
Name: STRING
,Age: INTEGER
,Subjects: SET OF STRING
.
Problem Example 2: Weekday Representation
- Requirements: Allow only valid weekdays as input.
- Suggested: Use an Enumerated Type
Day = (Mon, Tue, Wed, Thu, Fri)
Problem Example 3: Traffic Light System
- Requirements: Store current light color (Red, Yellow, Green).
- Suggested: Enumerated type:
Light = (Red, Yellow, Green)
Problem Example 4: Dynamic List of Tasks
- Requirements: A list that grows dynamically as tasks are added.
- Suggested: Use a Pointer-based Linked List.
5. Summary Table of User-defined Types
Type | Description | Example Use Case |
---|---|---|
Enum | Named list of constant values | Days of week, traffic lights |
Pointer | Reference to memory address | Linked lists, tree structures |
Record | Group of different data fields | Student or employee records |
Set | Unordered collection of unique elements | Subjects chosen, available options |
Class | Encapsulation of data and behavior | OOP-based systems like simulations |