Introduction to User Defined Data Types in C++
User Defined Data type in c++ is a type by which the data can be represented. The type of data 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. We will discuss user-defined data types in detail.
As the programming languages allow the user to create their own data types according to their needs. Hence, the data types that are defined by the user are known as user-defined data types. For example; arrays, class, structure, union, Enumeration, pointer, etc. These data types hold more complexity than pre-defined data types.
Types of User-Defined Data in C++
Here are the types mentioned below:
1. Structure
A structure is defined as 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’, ‘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 allocates storage space for them. Structures variables can be defined as follows:
stud_id I1, I2;
Where I1, I2 are the two variables of stud_id. After defining the structure, its members can be accessed using the dot operator as follows:
I1.roll_number will access roll number of I1
I2.class will access 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 defined as a collection of homogeneous data. It should 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 marks of 10 students. After the array is created, one can access any element of an array by writing the name an array followed by its index. For example; to access 5th element from marks, the syntax is as follows:
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 contain members of different data types. The main difference between the two is that union saves memory as members of a union share the same storage area whereas members of the structure are assigned their own unique storage area. Unions are declared with keyword “union” as follows:
union employee
{
int id;
double salary;
char name[20];
}
The variable of the union can be defined as:
union employee E;
To access the members of the union, the dot operator can be used as follows:
E.salary;
4. Class
A class is an important feature of object-oriented programming language just like C++. A class is defined as a group of objects with the same operations and attributes. It is declared using a keyword “class”. The syntax is as follows:
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 be only accessed by the member functions of that particular class only. However, the members defined as the public can be accessed from within and from outside the class also. The members with no specifier are private by default. The objects belonging to a class are called 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 a keyword “enum”. It is defined as a set of named integer constants that specify all the possible values a variable of that type can have. For example, 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 that kind of user-defined data type that creates variables for holding the memory address of other variables. If one variable carries the address of another variable, the first variable is said to be the pointer of another. 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”, you can define new data type names to the existing ones. Its syntax is:
typedef <type> <newname>;
typedef float balance;
Where a new name is created for float i.e. using balance, we can declare any variable of float type.
The use of a typedef can make the code easy to read and also easy to port to a new machine.
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. There many other such data types such as functions, reference, 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 a guide to User-Defined Data Types in C++. Here we discuss the user-defined data types in c++ along with respective examples. You may also have a look at the following articles to learn more –
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses