Topic One (1.1): User-defined Data Types
Introduction
A good program will generally represent data in a way that is similar to the real world; databases show dates as DATETIME rather than the number it is stored as, we show a string as characters rather than a combination of binary ASCII codes.
Sometimes, however, the data types we are used to (string, integer, float/real, etc.) just aren’t enough. In this case we can create our own data types. These can represent a single piece of data, often an enumerated type. If you were to develop a simple text-based adventure game, you may find instructions and directions naturally lend themselves to being a data type.
READ - Text-based Games (Wikipedia) Links to an external site.
Taking this concept to the next level we can also create composite data types; if we wanted to store data on a book, we would want to have its author, its title, the ISBN number… and by creating a data type that has all these things we can just define ‘a book’.
Watch
WATCH – Using Records (classes) in Python as a User Defined Record Structure. (YouTube) Links to an external site. This video walks through using a class as a record structure in Python.
WATCH - Learning Pascal x20 User Defined Types (YouTube) Links to an external site. Python, being weakly typed, does not make user defined data types as clear as they could be. This video demonstrates using UDTs in Pascal, a strongly typed language.
WATCH - Introduction to Sets - Data Structures and Algorithms (YouTube) Links to an external site. This video leads you through the use of sets. Note that while the theory goes further than you need here, it is super useful information.
Read and Make Notes
Read pages 313 to 317 of your textbook. Complete the questions and tasks within the text as you go.
CHECK – Module 1 Textbook Answers Download CHECK – Module 1 Textbook Answers
Tasks
Complete the following:
- Create a user-defined data structure to store details for a book.
- Create an enumerated data type to store the genre of the book.
- Add some books to your data structure implementation.
- Output the contents of the data structure implementation.
- REVIEW - UDT Book (ReplIt) Links to an external site. An example implementation can be found here.
Further Reading
READ - User-defined Data Types in C (Geeks for Geeks) Links to an external site. The Geeks for Geeks website has a useful document on the use of user-defined data structures.
Extended Learning
The next step on from a user-defined data structure is an abstract data type. This is often linked to, and uses the same structures as, object-oriented programming. As well as storing data about an object (such as our book) an ADT has methods that can be performed on that structure. Using the Internet, explore the use of ADTs and try to expand your ‘book’ data structure with some methods.
End of Topic 1 Review
Complete exam-style questions 1 and 4 on pages 326 to 327.
CHECK – Module 1 Exam-style Answers Download CHECK – Module 1 Exam-style Answers
Top Tips
Ensure that you understand the following before moving on to the next topic.
- A data type is a single element.
- A data structure is multiple elements, also known as a composite data type.
- Data types and data structures can be developed by the user to simplify implementation, and often mirror the real-world system.
Key Terms
Add the following subject vocabulary to your glossary. Write the definitions in your own words to embed understanding.
- Data type
- User-defined data type
- Non-composite data type
- Enumerated data type
- Composite user-defined data type
- Pointer data type
- Set data type
Consolidate
Extend your understanding by working through these additional resources/tasks:
- Explore the use of sets in Python. Practise defining sets and performing unions, differences and intersections on them.
- Python does not include pointers. Explore why C uses pointers but Python doesn’t.