Tutorial Study Image

Derived and User-Defined Data Types in C++


August 4, 2022, Learn eTutorial
1638

As we know the data types in C++ are mainly divided into three types pre-defined data types,  derived data types, and user-defined data types. The pre_ defined data types are discussed in the previous tutorial.

In this tutorial, we are mainly focusing on the derived data type as well as user_ defined data types.

Derived data type

Derived Data Types are data types that are derived from primitive or built-in data types.

Datatypes in C++

Derived Datatypes in C++

These are classified into four types:

  • Function
  • Array
  • Pointers
  • References
     

Let us quickly explain each of the derived datatypes listed above:

  1. Function

    A function is a block of code or a programme segment that is defined to accomplish a certain, well-defined task. Typically, the purpose of a function is to prevent the user from repeatedly inputting the same lines of code for the same input. All of the lines of code are combined into a single function, which may be called from anywhere. main() is a default function that is defined in every C++ programme.

    Syntax:
    
    FunctionType FunctionName(parameters)
     
    

    A sample program in C++ to Display Function Derived Type

    
    #include <iostream>
    using namespace std;
    // max here is a function derived type
    int max(int y, int z)
    {
        if (y > z)
            return y;
        else
            return z;
    }
    
    // main is the default function derived type
    int main()
    {
        int a = 30, b = 60;
    
        // Calling above function to
        // find max of 'a' and 'b'
        int m = max(a, b);
    
        cout << "m is " << m;
        return 0;
    }
    

    Output:

    
    m is 60
     
  2. Array

    An array is a collection of items stored in continuous memory regions. The purpose of an array is to store several instances in a single variable.

    Arrays in C++

    Arrays in C++

    Syntax:
    
    DataType ArrayName[size_of_array];
     
    

    Array Derived Type Demonstration in C++

    
    #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<
    
    Output:
    
    5 2 -10 5
     
  3. Pointers

    Symbolic representations of addresses are called pointers. They allow programmes to imitate call-by-reference operations as well as generate and manage dynamic data structures. The format of its general declaration in C/C++ is:

    Syntax:
    
    datatype *var_name;
    

    Example

    
    int *ptr;   
     
    

    ptr points to an address which will hold the int data

    A sample Program in C++ to Demonstrate the Pointers Derived Type

    
    #include <bits/stdc++.h> 
    using namespace std;
    
    void learn()
    {
     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 program
    int main()
    {
     learn();
    }
    }
    

    Output:

    
    Value at ptr = 0x7ffc10d7fd5c
    Value at var = 20
    Value at *ptr = 20
     
  4. References

    When a variable is declared as a reference, it becomes a synonym for another variable. By including '&' in the declaration, a variable can be declared as a reference.

    A sample Program in C++ to Demonstrate the Reference Derived Type

    
    // 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
     

User-defined data type

Data types defined by the user are referred to as derived datatypes or user-defined derived data types.

User-defined in C++

User-defined Datatypes in C++

These kinds consist of:

  • Class
  • Structure
  • Union
  • Enumeration
  • Typedef defined DataType

The following types are described in detail below.

  1. Class

    A Class is the C++ building block that leads to Object-Oriented programming. It is a user-defined data type with its own set of data members and member functions that can be accessed and used by creating an instance of that class. A class is similar to an object's blueprint.

    Classin C++

    Class in C++

    A C++ program which mainly given here to demonstrate Class

    
    // C++ program to demonstrate
    // Class
    
    #include <bits/stdc++.h>
    using namespace std;
    
    class Learns {
        // Access specifier
    public:
        // Data Members
        string learnname;
    
        // Member Functions()
        void printname()
        {
            cout << "Learnname is: " << learnname;
        }
    };
    
    int main()
    {
    
        // Declare an object of class geeks
        Learns obj1;
    
        // accessing data member
        obj1.learnname = "Learn eTutorials";
    
        // accessing member function
        obj1.printname();
    
        return 0;
    }
    
    

    Output:

    
    Learnname is: Learn eTutorials
     
  2. Structure 

    In C/C++, a structure is a user-defined data type. A structure defines a data type that can be used to combine items of feasibly different types into a single type.

    Syntax:
    
    struct address {
        char name[50];
        char street[100];
        char city[50];
        char state[20];
        int pin;
    };
    

    A sample Program in C++ to Demonstrate the Structures in C++

    
    // C++ program to demonstrate
    // Structures in C++
    
    #include <iostream>
    using namespace std;
    
    struct Point {
        int x, y;
    };
    
    int main()
    {
        // Create an array of structures
        struct Point arr[10];
    
        // Access array members
        arr[0].x = 10;
        arr[0].y = 20;
    
        cout << arr[0].x << ", " << arr[0].y;
    
        return 0;
    }
    Output:
    
    10, 20
     
  3. Union

    Union is also a user-defined data type, similar to Structures. All members of a union share the same memory location. In the following program, for example, x and y share the same location. When we change x, we can see how the changes are reflected in y.

    A sample Program in C++ to Demonstrate the Union

    
    #include <iostream>
    using namespace std;
    
    // Declaration of union is same as the structures
    union test {
        int x, y;
    };
    
    int main()
    {
        // A union variable t
        union test t;
    
        // t.y also gets value 2
        t.x = 2;
    
        cout << "After making x = 2:"
            << endl
            << "x = " << t.x
            << ", y = " << t.y
            << endl;
    
        // t.x is also updated to 10
        t.y = 10;
    
        cout << "After making Y = 10:"
            << endl
            << "x = " << t.x
            << ", y = " << t.y
            << endl;
    
        return 0;
    }
    
    Output:
    
    After making x = 2:
    x = 2, y = 2
    After making Y = 10:
    x = 10, y = 10
  4. Enumeration

    An enumeration (or enum) is mainly a user-defined data type. It is primarily used to give names to integral constants, which makes a program easier to read and maintain.

    Syntax:
    
    enum State {Working = 1, Failed = 0}; 
    

    A sample Program in C++ to Demonstrate the working of enum in C++.

    
    #include <iostream>
    using namespace std;
    
    enum week { Mon,
                Tue,
                Wed,
                Thur,
                Fri,
                Sat,
                Sun };
    
    int main()
    {
        enum week day;
    
        day = Wed;
    
        cout << day;
    
        return 0;
    }
    
    
    Output:
    
    2
     
  5. Typedef

    The keyword typedef in C++ allows you to explicitly define new data type names. The use of typedef does not create a new data class but rather defines a name for an existing type. This can improve a program's portability (the ability of a program to be used across different types of machines; i.e., mini, mainframe, micro, etc; without requiring significant changes to the code) because only the typedef statements would need to be changed. By allowing descriptive names for standard data types, typedef can also help with self-documenting code.

    Syntax:
    typedef type name;
    

    An example C++ program to demonstrate typedef

    
    #include <iostream>
    using namespace std;
    
    // After this line BYTE can be used
    // in place of unsigned char
    typedef unsigned char BYTE;
    
    int main()
    {
        BYTE b1, b2;
        b1 = 'c';
        cout << " " << b1;
        return 0;
    }
    Output:
    
    c