example of user-defined data type
A user-defined data type (UDT) is a data type that was created by modifying a preexisting data type. UDTs may be used to expand the built-in types and construct your own customised data types.
Types of User-defined Datatypes:
- Class
- Structure
- Union
- Enumeration
- Typedef defined DataType
Class
In C++, a class is the fundamental component that produces Object-Oriented programming. It is a custom data type that can be accessed and used by creating an instance of the associated class. It has its own set of attributes and functions associated with them. A C++ class functions similarly to an object's blueprint. Examples include the following: Consider the class of automobiles. There may be a large number of cars with different names and brands, but they all have some fundamental qualities, such as having four wheels, having a speed restriction, having a range, etc. So, in this case, Car is the class, and the attributes are wheels, speed limitations, and kilometers.
Example:
#include <bits/stdc++.h>
using namespace std;
class Program {
// Access Modifier
public:
string sname;
// Member Functions()
void printnames()
{
cout << “The String name is: " << sname;
}
};
int main()
{
// Declaring class Objects
Program object1;
// accessing data members
object1.sname = "Programming";
// accessing the functions of the member class
object1.printnames();
return 0;
}
Output
The string name is: Programming
Structure
In C/C++, a structure is a user-defined data type that may be used to combine elements of relatively various types into one type of data. In the C programming language, the structure keyword is used for defining a structure. The structure's members are the things in it, which can be of any suitable data type.
Syntax:
struct person_address {
char name[50];
char location[100];
char village[50];
char country[20];
int pincode;
};
Example:
//C++ Program for implementation of Structures
#include <iostream>
using namespace std;
struct values{
int n1, n2;
};
int main()
{
// An array consisting of structures
struct values array[10];
// Access array members
array[0].n1 = 20;
array[0].n2= 30;
cout << array[0].n1 << ", " << array[0].n2;
return 0;
}
Output
20, 30
Union
The Union is a user-defined data type in C++, as a structure, can contain components of several data types. In contrast to structures, every member of the C++ union remains in the same memory region. As a result, only one member can save data at any one time.
Syntax:
union union name{
datatype mem1;
datatype mem2;
...
};
Example:
#include <iostream>
using namespace std;
// union declarartiom
union testing {
int n1, n2;
};
int main()
{
//a union variable temp
union testing temp;
temp.n1 = 20;
cout << "After changing n1 = 20:"
<< endl
<< "n1 = " << temp.n1
<< ", n2 = " << temp.n2
<< endl;
temp.n2 = 30;
cout << "After changing n2 = 30:"
<< endl
<< "n1 = " << temp.n1
<< ", n2 = " << temp.n2
<< endl;
return 0;
}
Output
After changing n1 = 20:
n1 = 20, n2 = 20
After changing n2 = 30:
n1 = 30, n2 = 30
Enumeration
In C++, an enumeration (or enum) is a user-defined data type. It is mostly used to give names to integer constants, which makes a program easier to understand and maintain.
Syntax:
enum <identifier> (
num1,
num2,
num3,
...
)
Example:
// Program for enum implementation
#include <iostream>
using namespace std;
enum weeks_days { Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main()
{
enum weeks_days days;
days = Saturday;
cout << days;
return 0;
}
Output
5
Typedef defined DataType
The typedef keyword is used to establish a domain name for a certain data type. Writing customized wrapper classes is more difficult than using the typedef keyword. When you use the typedef keyword, you may concentrate more on what a variable should represent. This makes it much easier to develop clean code and adjust your code.
Syntax:
typedef <cur_name> <new_names>
Example:
// C++ Program for implementation of typedef
#include <bits/stdc++.h>
using namespace std;
int main()
{
// creating vectors
typedef std::vector<int> vInt;
// vectors are defined by int datatype
vInt v1;
v1.push_back(120);
v1.push_back(160);
v1.push_back(23);
v1.push_back(26);
v1.push_back(20);
for (auto x1 : v1) {
cout << x1 << " ";
}
return 0;
}
Output
120 160 23 26 20
Applications of typedef
- In C++, typedef can be used to rename specified data types with large names.
- It is compatible with STL data structures like Vectors, which are Strings, Maps, and so on.
- It can be used for arrays.
- Type def can be used for both normal and functional pointers.