Updated June 26, 2023
Introduction to User-Defined Data Types in C++
User Defined Data types in C++ are a type for representing data. The data type will inform the interpreter how the programmer will use the data. A data type can be pre-defined or user-defined. Examples of pre-defined data types are char, int, float, etc.
As the programming languages allow the user to create their own data types according to their needs, the data types defined by the users are known as user-defined data types. For example, arrays, classes, structures, unions, enumerations, pointers, etc. These data types hold more complexity than pre-defined data types.
Types of User-Defined Data in C++
The types of user-defined data are as follows:
1. Structure
A structure is a collection of various types of related information under one name. The declaration of structure forms a template, and the variables of structures are known as members. All the members of the structure are generally related. The keyword used for the structure is “struct.”
For example, a structure for student identity having ‘name,’ ‘class,’ ‘roll_number,’ and ‘address’ as a member can be created as follows:
struct stud_id
{
char name[20];
int class;
int roll_number;
char address[30];
};
This is called the declaration of the structure, and it is terminated by a semicolon (;). The memory is not allocated, while the structure declaration is delegated when specifying the same. The structure definition creates structure variables and gives storage space for them. One can define the variables of the structure as follows:
stud_id I1, I2;
Where I1 and I2 are the two variables of stud_id. After defining the structure, one can access its members using the dot operator as follows:
I1.roll_number will access the roll number of I1
I2.class will access the class of I2
Example:
struct stud_id
{
int class, roll_number;
};
int main()
{
struct stud_id entries[10]; // Create an array of structures
entries[0].class = 4; // Access array members
entries[0].roll_number = 20;
cout <<entries[0].class << ", " << entries[0].roll_number;
return 0;
}
2. Array
An array is a collection of homogeneous data and must be defined before using it for the storage of information. The array can be defined as follows:
<datatype> <array_name><[size of array]>
int marks[10]
The above statement defined an integer-type array named “marks” that can store the marks of 10 students. After creating an array, one can access any element of an array by writing the name of an array, followed by its index. For example, to access the 5th element from marks, the syntax will be:
marks[5]
It will give the marks stored at the 5th location of an array. An array can be one-dimensional, two-dimensional, or multi-dimensional, depending upon the specification of elements.
Example:
int main()
{
int marks[10];
marks[0] = 5;
marks[2] = -10;
cout<<marks[0], marks[2]);
return 0;
}
3. Union
Just like structures, the union also contains members of different data types. The main difference is that the union saves memory because union members share the same storage area. In contrast, members of the structure have their unique storage areas. One declares the unions with the keyword “union,” as shown below:
union employee
{
int id;
double salary;
char name[20];
}
One can define the variable of the union as:
union employee E;
To access the members of the union, one can use the dot operator as follows:
E.salary;
4. Class
A class is an essential feature of object-oriented programming languages like C++. A class is a group of objects with the same operations and attributes. To declare a class, use the keyword “class” and follow this syntax:
class <classname>
{
private:
Data_members;
Member_functions;
public:
Data_members;
Member_functions;
};
In this, the names of data members should be different from member functions. There are two access specifiers for classes that define the scope of the members of a class. These are private and public. The member specified as private can only be accessed by the member functions of that particular class. The members, defined as the public, have internal and external access to the class. The members with no specifier are private by default. We refer to the objects that belong to a class as instances of the class. The syntax for creating an object of a class is as follows:
<classname> <objectname>
Example:
class kids
{
public: //Access specifier
char name[10]; //Data members
int age;
void print() //Member function
{
cout<<"name is:"<< name;
}
}
Int main
{
Kids k; //object of class kid is created as k
k.name="Eash";
k.print();
return 0;
}
5. Enumeration
Enumeration is specified by using the keyword “enum.” It is a set of named integer constants that define all the possible values a variable of that particular type can have. For example, the enumeration of the week can have names of all the seven days of the week as shown below:
Example:
enum week_days{sun, mon, tues, wed, thur, fri, sat};
int main()
{
enum week_days d;
d = mon;
cout << d;
return 0;
}
6. Pointer
A Pointer is a user-defined data type that creates variables for holding the memory address of other variables. If one variable holds the address of another variable, we say that the first variable is the pointer to the other variable. The syntax for the same is:
type *ptr_name;
Here type is any data type of the pointer, and ptr_name is the pointer’s name.
Example:
void main()
{
int a = 10;
int *p; // pointer variable is declared
p = &a; // data type of pointer ‘p’ and variable ‘a’ should be same
cout<<"Value at p = ",<<p); // the address of a variable is assigned to a pointer
cout<<"Value at variable a = “,<<a);
cout<<"Value at *p = ",<< *p);
}
7. Typedef
Using the keyword “typedef,” one can define new data type names to the existing ones. Its syntax is:
typedef <type> <newname>;
typedef float balance;
When we create a new name for the float data type, such as “balance,” we can use it to declare variables of the float type. The use of a typedef can make the code not only easy to read but also to port to a new machine as well.
Example:
typedef int score;
int main()
{
score s1, s2;
s1 = 80;
cout << " " << b1;
return 0;
}
Conclusion
C++ supports different kinds of user-defined data types, as discussed above. Many other data types exist, such as functions, references, etc. Their use makes programming much easier, and they also help us to club different types of data in a single variable.
Recommended Articles
This is our guide to User-Defined Data Types in C++. Here are some further articles to learn more: