Skip to main content

C++ - Derived data types: arrays, structures, unions, enums

Derived Data Types: Arrays, Structures, Unions, Enums

C++ is an object-oriented programming language, which means that it is based on data types. Derived data types are a type of data type that contains multiple elements of the same type, such as an array or structure. These data types allow programmers to store and manage multiple values of the same type in one variable. In this article, we will discuss four derived data types: arrays, structures, unions, and enums.

1. Arrays

An array is a collection of items of the same data type. It is a data structure that stores a fixed-size sequential collection of elements of the same type. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. An array can be one-dimensional or multi-dimensional.

In C++, an array is declared by specifying the data type, followed by the name of the array, and the size of the array between square brackets. For example:

int arr[10];

This declares an array of 10 integers called arr. To access a particular element in an array, its index is specified between square brackets after the name of the array. For example, to access the fourth element of arr, we would use the following code:

int fourthElement = arr[3];

Tip: When declaring an array, make sure that the size of the array is larger than the number of elements you need to store. This will help you avoid errors when accessing elements in the array.

2. Structures

A structure is a user-defined data type that can store multiple values of different data types in a single variable. It is useful for storing related data that is logically grouped together. A structure is declared by using the keyword struct, followed by the name of the structure and the data elements that the structure contains. For example:

struct Student { char name[100]; int age; float grade; };

This declares a structure called Student that contains three elements: name (a character array of size 100), age (an integer), and grade (a float). To access an element of a structure, the structure name is followed by a dot and the name of the element. For example, to access the age element of Student, we would use the following code:

int age = Student.age;

Tip: When declaring a structure, make sure that the data types of the elements are compatible with each other. This will help you avoid errors when accessing elements in the structure.

3. Unions

A union is a user-defined data type that can store multiple values of different data types in the same memory location. It is useful for storing data that can take on different forms, such as a number that can be an integer or a float. A union is declared by using the keyword union, followed by the name of the union and the data elements that the union contains. For example:

union Number { int intValue; float floatValue; };

This declares a union called Number that contains two elements: intValue (an integer) and floatValue (a float). To access an element of a union, the union name is followed by a dot and the name of the element. For example, to access the floatValue element of Number, we would use the following code:

float floatValue = Number.floatValue;

Tip: When declaring a union, make sure that the data types of the elements are compatible with each other. This will help you avoid errors when accessing elements in the union.

4. Enums

An enum (enumeration) is a user-defined data type that can store a set of named integer constants. It is useful for storing data that can take on a limited number of values, such as a boolean value that can either be true or false. An enum is declared by using the keyword enum, followed by the name of the enum and the constants that the enum contains. For example:

enum Boolean { TRUE, FALSE };

This declares an enum called Boolean that contains two constants: TRUE and FALSE. To access a constant in an enum, the enum name is followed by a dot and the name of the constant. For example, to access the FALSE constant of Boolean, we would use the following code:

Boolean myBoolean = Boolean.FALSE;

Tip: When declaring an enum, make sure that the constants are distinct from each other. This will help you avoid errors when accessing constants in the enum.

Conclusion

Derived data types are a type of data type that contains multiple elements of the same type. In C++, there are four derived data types: arrays, structures, unions, and enums. Arrays are used to store multiple values of the same data type in a single variable, while structures, unions, and enums are used to store multiple values of different data types in a single variable. When defining derived data types, it is important to make sure that the data types of the elements are compatible with each other to avoid errors.