A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/User-defined data types



 You have already met a variety of built-in datatypes with integers, strings, chars and more. But often these limited datatypes aren't enough and a programmer wants to build their own datatypes. Just as an integer is restricted to "a whole number from -2,147,483,648 through 2,147,483,647", user-defined datatypes have limits placed on their use by the programmer.

Enumerated, subrange, sets

Enumerated
If you are using lots of constants in your program that are all related to each other it is a good idea to keep them together using a structure called an. For example you might want to store the names of each set of cards in a deck, instead of writing: We can bring them together in a nice neat structure called an enum: This allows you to set meaningful names to the enum and its members, meaning it it easier to remember and makes your code more readable.

We might also create separate constants to store the points of football results With enums we can create a datatype called Result and store the points within it, under easy to remember name:

Declare an enum called  that holds the months along with how many days in each for a non-leap year

What would the following code do:

Records and Fields
Records are collections of data items (fields) stored about something. They allow you to combine several data items (or fields) into one variable. An example at your college they will have a database storing a record for each student. This student record would contain fields such as ID, Name and Date of Birth. The following example that DOESN'T USE RECORDS might be simple enough for one student:

For the following input:

insert the id: 12

insert the name: Nigel

insert the Date of Birth: 12/12/1994

new record created: 12 Nigel 12/12/1994

But what if your college has more than one student, we'd have to write: It would take an awfully long time to declare them all, let alone saving writing data to them. So how do we solve this? Well we need to combine two things we have learnt about so far, the record and the array. We are going to make an array of student records: This seems to solve our problem, you might want to try it out yourself but decrease the number of students slightly!

Declare an record called  to store the following Role Playing Game attributes: health, name, characterclass (barbarian, wizard, elf), gold, gender

Creates 2 characters, Gandolf and Conan using the player record

However, what use is a program that only saves all the students for as long as it is running? We need to know how to write to files, and read the data back, we'll look at that in the next chapter.

It's pretty cool creating your own datatypes where you can set up collections of attributes (fields), but it would be even cooler if you could store custom procedures and functions as well. This is where Object Orientation comes in. It's a very common way of writing code with many of the world's most popular languages (Java, C++, C#, Python) making use of it. You'll learn a lot more about it in A2 but there is nothing to stop you getting started now.