![]() |
VOOZH | about |
Derived data types are data types built from primitive (built-in) data types. They extend the functionality of the basic types and allow more complex data representation.
In C++, there are four different derived data types:
Pointers are symbolic representation of addresses. They can be said as the variables that can store the address of another variable as its value. The below example demonstrates the use of pointer in C++.
Value at ptr = 0x7fffa5bef2cc Value at var = 20 Value at *ptr = 20
Explanation: This above program demonstrates the use of pointers as a derived type. It declares pointer variable ptr and assigning the address of a variable var to it. It then prints the values of the pointer, the variable, and the dereferenced pointer, showcasing the basics of pointer usage in C++.
When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration.
x = 20 ref = 30
Explanation: The above program demonstrates the use of reference-derived type. A reference ref to an integer variable x. is created. If the value of ref is changed the value x, is also modified and vice versa.
A function is a block of code or program segment that is defined to perform a specific well-defined task. It is generally defined to save the user from writing the same lines of code again and again for the same input. All the lines of code are put together inside a single function and this can be called anywhere required.
m is 20
Explanation: This above program demonstrates the use of function derived types It defines a function called max this function returns the maximum of two integers provided as input. In the main function, max function is called to find the maximum of variables a and b and store it in m and finally print m(max number).
An array is a collection of items stored at continuous memory locations. The idea of array is to represent many variables using a single name. The below example demonstrates the use of array in C++.
5 2 -10 5 5
Explanation: This above program shows the use of array-derived type. It creates an integer array arr and assigns values using indices. Then it prints all the array elements.