Apart from storing simple data like numbers, characters, etc. we need to store data that is of a complex format. These complex format data might contain tabular data format, graphical data format, databases, etc. To store such complex data, we require more than Built-in Data types. For this purpose, we use Complex Data types called as User defined data types. In computer science, User defined data types are data types that are used as per the user’s need.
Definition
- In a programming language, User Defined Data Types are defined by the users in the program as per their needs in order to store data either of the same or different types as per the requirement.
- User Defined Data types or Composite Data types are derived from more than one built-in data type which is used to store complex data.
User Defined Data Types
Data types define the type of value or data a variable holds in it. Built-in Data types are used to store simple data types such as integers, decimal point values, characters, etc. But Data also occurs in a wide variety of formats. High Complexity of data can occur often. For this purpose programming languages provides us with User-Defined Data types that are created by the users/programmers as per the data complexity requirements.
For every Programming language, there are different types of User-Defined data types. Let us look at a few of them :
-
C/C++ Programming Language
- Class – A User-defined data type that holds data members and functions whose access can be specified as private, public, or protected. It uses the ‘class’ keyword for defining the data structure.
- Structure – A structured data type is used to group data items of different types into a single type. For Eg. Structure can be Address, in which it further contains information such as Flat number, Building name, street, city, state, pin code, etc. It is defined by using the ‘struct’
- Union – Union is a type of data structure where all the members of that union share the same memory location. If any changes made in the Union, it will be visible to others as well. The ‘union’ keyword is used to define this type of User-defined data type.
- Enumeration – It helps assign names to integer constants in the program. The keyword ‘enum’ is used. It is used to increase the readability of the code.
- Typedef – Typedef defines a new name for an existing data type. It does not create a new data class. It makes code readability easy and gives more clarity to the user.
The syntax for defining a Typedef Data Structure is:
typedef data_type new_name ;
-
Java/Python Programming Language
Along with structure, Union and Enumeration, Java also supports few other user-defined data types. They are:
- Classes
- Interface – Interface is a type of User-Defined data type where it specifies the behavior, the classes must implement. Once they are defined, any member of classes can implement an interface.
Browse more Topics under Structured Data Type
Benefits of User-Defined Data Types
- User-Defined data types store data elements of either the same types or different types. This gives more flexibility for the programmer to store different data types in a single variable as per their needs and requirements.
- Encapsulation – In Java/Python the variables and data values stored in User Defined Data types are hidden from other classes as per their accessibility declaration i.e. public, private, protected. The data values remain hidden and safe.
- Flexibility – They provide us the ability to create a data structure as per our requirements and needs.
- Reusability – Once defined, these data types can be reused within many definitions, saving coding time.
FAQs on User Defined Data Types
Q1. Which of the following keyword is used for defining a User-defined data type?
- def
- typedef
- definition
- int
Answer. Option B
Q2. Identify the following true statement.
- typedef creates a separate data type
- The typedef data type creates its own types
- typedef only creates a new name for the existing data type
- In programming, typedef is not a user-defined data type
Answer. Option C
Q3. ______ is a User Defined data type.
- Class
- Variable
- Integer
- None of the above
Answer. Option A
Q4. Which of the following is themselves a collection of different other data types?
- Union
- String
- Character
- Structure
Answer. Option D
Q5. User-defined data types can be derived by ______
- Struct
- Union
- Enumerator
- All of the above
Answer. Option D
Leave a Reply