Derived Data Types in C++

26 Feb, 2020
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 DataTypesDerived Data TypesUser-defined DataTypes
Pre-defined DataTypes
Derived Data Types
User-defined DataTypes
In this article, the Derived Data Type is explained:
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:
Function
Array
Pointers
References
Let’s briefly understand each of the following derived datatypes:
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:// C++ program to demonstrate// Function Derived Type  #include <iostream>using namespace std;  // max here is a function derived typeint max(int x, int y){    if (x > y)        return x;    else        return y;}  // main is the default function derived typeint 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
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:// C++ program to demonstrate// Array Derived Type  #include <iostream>using namespace std;int main(){      // Array Derived Type    int arr[5];    arr[0] = 5;    arr[2] = -10;      // this is same as arr[1] = 2    arr[3 / 2] = 2;      arr[3] = arr[0];      cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3];      return 0;}Output:5 2 -10 5
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;Example:int *ptr;   

ptr points to an address
which holds int dataExample:// C++ program to illustrate// Pointers Derived Type  #include <bits/stdc++.h>using namespace std;  void geeks(){    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 << "\n";    cout << "Value at var = "         << var << "\n";    cout << "Value at *ptr = "         << *ptr << "\n";}  // Driver programint main(){    geeks();}Output:Value at ptr = 0x7ffc10d7fd5c
Value at var = 20
Value at *ptr = 20
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.Example:// C++ program to illustrate// Reference Derived Type  #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

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:// C++ program to demonstrate// Function Derived Type  #include <iostream>using namespace std;  // max here is a function derived typeint max(int x, int y){    if (x > y)        return x;    else        return y;}  // main is the default function derived typeint 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

Syntax:
FunctionType FunctionName(parameters)
Example:
// C++ program to demonstrate// Function Derived Type  #include <iostream>using namespace std;  // max here is a function derived typeint max(int x, int y){    if (x > y)        return x;    else        return y;}  // main is the default function derived typeint 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;}
m is 20

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:// C++ program to demonstrate// Array Derived Type  #include <iostream>using namespace std;int main(){      // Array Derived Type    int arr[5];    arr[0] = 5;    arr[2] = -10;      // this is same as arr[1] = 2    arr[3 / 2] = 2;      arr[3] = arr[0];      cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3];      return 0;}Output:5 2 -10 5

Syntax:
DataType ArrayName[size_of_array];
Example:
// C++ program to demonstrate// Array Derived Type  #include <iostream>using namespace std;int main(){      // Array Derived Type    int arr[5];    arr[0] = 5;    arr[2] = -10;      // this is same as arr[1] = 2    arr[3 / 2] = 2;      arr[3] = arr[0];      cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3];      return 0;}
5 2 -10 5

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;Example:int *ptr;   

ptr points to an address
which holds int dataExample:// C++ program to illustrate// Pointers Derived Type  #include <bits/stdc++.h>using namespace std;  void geeks(){    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 << "\n";    cout << "Value at var = "         << var << "\n";    cout << "Value at *ptr = "         << *ptr << "\n";}  // Driver programint main(){    geeks();}Output:Value at ptr = 0x7ffc10d7fd5c
Value at var = 20
Value at *ptr = 20

Syntax:
datatype *var_name;
Example:
int *ptr;   

ptr points to an address
which holds int data
Example:
// C++ program to illustrate// Pointers Derived Type  #include <bits/stdc++.h>using namespace std;  void geeks(){    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 << "\n";    cout << "Value at var = "         << var << "\n";    cout << "Value at *ptr = "         << *ptr << "\n";}  // Driver programint main(){    geeks();}
Value at ptr = 0x7ffc10d7fd5c
Value at var = 20
Value at *ptr = 20

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.Example:// C++ program to illustrate// Reference Derived Type  #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

Example:
// C++ program to illustrate// Reference Derived Type  #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;}
x = 20
ref = 30

saisarat_219
C Basics
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Pair in C++ Standard Template Library (STL)
Friend class and function in C++
std::string class in C++
Queue in C++ Standard Template Library (STL)
std::find in C++
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
vector insert() function in C++ STL