User-Defined Data Types
User-Defined Data Types
1 Data representation
0T
0T
0T
Composite
Set
Record
Class/Object
Non-composite
Enumerated (enum)
Pointers
Non-composite data types
Enumeration
U
If you are using lots of constants in your program that are all related to each other, then it is a good
idea to keep them together using a structure called an Enum. For example, you might want to store
the names of each set of cards in a deck.
Heres one way of writing that in code:
Page 1 of 14
This allows you to set meaningful names to the enum and its members, making it much easier to
remember as well as making the code easier to read.
We may also create separate constants to store the points of a football match
With enums, we could create a datatype called Result and store the points within it, under an easy
to remember name.
Page 2 of 14
A pointer is a variable that represents the location of a particular data item within a specified domain
(such as the hard drive, or an array). Within the computers memory, every stored data item occupies
one or more contiguous memory cell/s. The number of memory cells needed to store a data item
depends on the data type of that item.
For example, a single character will typically be stored in 1 byte of memory; an integer usually
requires 4 contiguous bytes, a floating-point number usually requires 4 contiguous bytes, and so
on
U
To understand pointers, you must first understand how data is stored in the computers memory
In typical computer architecture of the memory, each byte of the memory has a unique address. Let
us assume the first byte has an address of 201. Then the next address will be 202 and well go on
203,204 and so on
Now when we initialize a variable, the computer allocates some amount of memory corresponding to
this particular variable depending on the data type of the variable.
So for example a as type integer that means the computer allocates 4 bytes of memory, we can
allocate memory from 204 to 207
Page 3 of 14
Now the main question is, can we know the address of a variable in our program? Yes we can! Using
the concept of pointers.
We can have another variable, type of which is a pointer p. Now this variable p can store the
address of a. p also takes some memory, so lets say that its stored at location address 64 and it
also takes 4 bytes of memory.
Int a;
Int *p; / /is the pointer
p = &a / /assigning address of a to p
a=5;
Print p
/ /output is 204
Print &a / /output is 204
Print &p / /output is 64
(Coding in C language)
Page 4 of 14
Int a;
Int *p; / /is the pointer
p = &a / /assigning address of a to p
a=5;
Print p
/ / 204
Print &a / / 204
Print &p / / 64
Print *p / / 5
(Coding in C language)
Page 5 of 14
Set:
U
It is a composite data type that can store data in any particular order, in which each element is
indexed. Sets cannot have data items repeated more than once
Constructing Sets
One way to construct sets is by passing any sequential object to the "set" constructor.
We can also add elements to sets one by one, using the "add" function.
The set function also provides a copy constructor. However, remember that the copy constructor will
copy the set, but not the individual elements.
Page 6 of 14
We can also test the membership of entire sets. Given two sets
a subset or a superset of .
0T
0T
0T
and
0T
, we check if
0T
is
0T
Removing Items
There is the remove function to remove a specified element from the set
Page 7 of 14
and
0T
0T
0T
0T
Union
The union is the merger of 2 sets. Any element in
0T
or
0T
0T
0T
0T
Set difference
It will return all the elements that are in
but not in
frozenset
A frozenset is basically the same as a set, except that its members cannot be changed. This means
that they can be used as members in other sets. frozensets have the same functions as normal sets,
except the functions which change the contents (add,remove,update, etc.)
Page 8 of 14
This code helps store data about a student in record form, however after entering the data of one
student, the program will terminate. How can we edit this code to allow for multiple records to be
added?
Page 9 of 14
It would take an awfully long time to declare them all, as well as writing data in them. So how do we
solve this? Well we need to combine two things weve already learnt so far, the record and the
array. We are going to make an array of student records.
Structure student 'record declaration
Dim id As Integer 'field
Dim name As String 'field
Dim DoB As Date 'field
End Structure
Sub Main()
Dim newStudents(400) As student 'declare an array of
student records, a school with 401 students
for x = 0 to 400 'insert the details for each student
Console.WriteLine("insert the id")
newStudents(x).id = Console.ReadLine()
Console.WriteLine("insert the name")
newStudents(x).name = Console.ReadLine()
Console.WriteLine("insert the Date of Birth")
newStudents(x).DoB = Console.ReadLine()
next
for x = 0 to 400 'print out each student
Console.WriteLine("new record created: " &
newStudents(x).id & " " & newStudents(x).name & " " &
newStudents(x).DoB)
next
End Sub
Page 10 of 14
Exercise 1: Declare a record called player to store the following Role Playing Game attributes:
health, name, class (barbarian, wizard, elf), gold, gender
Exercise 2: Create 2 characters, Gandolf and Conan using the player record
Page 11 of 14
Structures (Records) are very similar to Classes in that they collect data together. However, classes
extend this idea and are made from two different things:
Lets take a look at the following example:
Page 12 of 14
Remember this is a class and therefore only a template, we need to create it using an object.
Attributes
These store information about the object. In the example above we store the fuel and maxSpeed. The
attributes are attached to the classes, and if there are several instances (objects) of the classes then
each will store its own version of these variables. The terms private and public are substitutes of
the term dim and belongs to another section in OOP.
Methods
Unlike structures, OOP allows you to attach functions and procedures to your code. This means that
not only can you store details about your car (attributes), you can also allow for sub routines such as
drive() and refuel. Which are attached to each class.
Page 13 of 14
Page 14 of 14