Derived Data Types in C++
Last Updated :
11 Jan, 2024
Data types are means to identify the type of data and associated operations of handling it. There are three types of data types:
- Pre-defined Data Types
- Derived Data Types
- User-defined Data Types
Â
In this article, the Derived Data Type is explained:
Derived Data Types in C++
The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely:
Let’s briefly understand each of the following derived datatypes:
1. Function
A Function is a block of code or program segment that is defined to perform a specific well-defined task. A function 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. main() is a default function that is defined in every program of C++.
Syntax
FunctionType FunctionName(parameters)
Example:
The below example demonstrates the use of function in C++.
CPP
#include <iostream>
using namespace std;
int max( int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main()
{
int a = 10, b = 20;
int m = max(a, b);
cout << "m is " << m;
return 0;
}
|
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).
2. Array
An Array is a collection of items stored at continuous memory locations. The idea of array is to represent many instances in one variable.
Syntax
DataType ArrayName[size_of_array];
Example:
The below example demonstrates the use of array in C++.
CPP
#include <iostream>
using namespace std;
int main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3 / 2] = 2;
arr[3] = arr[0];
cout << arr[0] << " " << arr[1] << " " << arr[2] << " "
<< arr[3];
return 0;
}
|
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.
3. Pointers
Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. It’s general declaration in C/C++ has the format:
Syntax
datatype *var_name;
for example: int *ptr; (ptr points to an addresswhich holds int data).
Example:
The below example demonstrates the use of class in C++.
CPP
#include <iostream>
using namespace std;
void geeks()
{
int var = 20;
int * ptr;
ptr = &var;
cout << "Value at ptr = " << ptr << "\n" ;
cout << "Value at var = " << var << "\n" ;
cout << "Value at *ptr = " << *ptr << "\n" ;
}
int main() { geeks(); }
|
Output
Value at ptr = 0x7ffc04f3f894
Value at var = 20
Value at *ptr = 20
Explanation: This above program demonstrates the use of pointers as a derived type. It deca 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++.
4. Reference
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.
Syntax
data_type &ref = variable;
Example:
The below example demonstrates the use of reference in C++.
CPP
#include <iostream>
using namespace std;
int main()
{
int x = 10;
int & ref = x;
ref = 20;
cout << "x = " << x << endl;
x = 30;
cout << "ref = " << ref << endl;
return 0;
}
|
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.
Conclusion
Derived data types in C++ are functions, arrays, pointers, and references offer so many useful tools to handle data. Functions let us write the reusable code. Arrays help manage multiple items and data very efficiently. Pointers allows the flexible memory use and also referencing. References provide a way to create alias and simpler ways to work with variables. Overall, these derived datatypes make C++ coding more systematic and effective.
Share your thoughts in the comments
Please Login to comment...