Open In App

Derived Data Types in C++

Last Updated : 18 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. They are generally the data types that are created from the primitive data types and provide some additional functionality.

In C++, there are four different derived data types:

Let’s understand each of the following derived datatypes:

Functions

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.

Let’s take a look at example that demonstrates the use of function in C++:

C++
#include <iostream>
using namespace std;

// max here is a function which is a derived type
int max(int x, int y) {
    if (x > y)
        return x;
    else
        return y;
}

// main function is also a derived type
int main() {
    int a = 10, b = 20;

    // Calling above function to
    // find max of 'a' and 'b'
    int m = max(a, b);

    cout << "m is " << m;
    return 0;
}

Output
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).

Arrays

Anarray 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++.

C++
#include <iostream>
using namespace std;

int main() {

    // Array Derived Type
    int arr[5] = {1, 2, 3, 4, 5};
    arr[0] = 5;
    arr[2] = -10;
    arr[3] = arr[0];

  	// Printing the data
    for (int i = 0; i < 5; i++)
      	printf("%d ", arr[i]);

    return 0;
}

Output
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.

Pointers

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++.

C++
#include <iostream>
using namespace std;

int main() { 
	int var = 20;

    // Pointers Derived Type
    // declare pointer variable
    int* ptr;

    // note that data type of ptr
    // and var must be same
    ptr = &var;

    // assign the address of a variable
    // to a pointer
    cout << "Value at ptr = " << ptr << endl;
    cout << "Value at var = " << var << endl;
    cout << "Value at *ptr = " << *ptr;
  
  	return 0;
}

Output
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++.

References

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.

The below example demonstrates the use of reference in C++.

C++
#include <iostream>
using namespace std;

int main() {
    int x = 10;

    // Reference Derived Type
    // ref is a reference to x.
    int& ref = x;

    // Value of x is now changed to 20
    ref = 20;
    cout << "x = " << x << endl;

    // Value of x is now changed to 30
    x = 30;
    cout << "ref = " << ref << endl;

    return 0;
}

Output
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.

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.



Next Article
Practice Tags :

Similar Reads