Non-primitive data types are those data types that are derived from primitive data types. They are further divided into derived and user-defined data types. They are also called “reference variables” or “object references” since they reference a memory location that stores the data.
Derived Data Types
The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types.
These include arrays, pointers, functions, and reference.
Let us look into each one in detail:-
A function is generally defined to save the user from writing the same lines of code again and again for the different input.
Let us write a program to understand syntax and usability of functions better.
These are lists of variables of the same data type. To understand arrays better consider a list of cars. All elements in the list are cars although their names can differ. Arrays are useful when we want to store similar elements together. Consider the following code to understand implementation and syntax better.
When we declared a variable as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting & in the declaration.
These are basically variables that store addresses of other variables. The value stored in a pointer irrespective of their data type is the same, long hexadecimal number. Pointers are very essential to perform tasks such as dynamic memory allocation. They are used to implement arrays and linked lists. Let us see how we can use pointers.
When we declare an array we are basically declaring a pointer to the first element of the array so when we say:
int a 
we mean to say create a pointer a which will point to the location of the base or first element of the array.
A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
Don't get confused between the *, a pointer is declared using an asterisk and once it stores the reference, it is dereferenced using asterisk itself.
User Defined Data Types
These are abstract data types that are defined by users themselves. The user can make use of one or more of the available data types to make his customized data type and use it in the program when required. Classes, structures, unions, and typedef are user-defined data types that allow us to create a customized data type.
Suppose you own an automobile company and you wish to build a car. The first thing we do is decide on the design and specify the features as well as the safety measures. Once your design is finalized all the manufactured cars will have the same design. This design is also known as a class.
Similarly in C+,+ a class is a design based on which objects belonging to that class are created. A class is essentially a blueprint, which means it has no actual space in memory, in fact only when an object which belongs to a particular class is created, space is allotted in the memory. An object is like a variable and a class is a data type.
Lets us see an example to understand better:
In many situations, we need to store a group of data whether of similar data types or non-similar data types. Arrays can be used to store data of the same data type but in order to group data of different data types, we use structures. The struct keyword is used to create a structure.
A Union is a user-defined datatype. The members of the union share the same memory location. The size of a union is decided by the size of the largest member of the union. If you want to use the same memory location for two or more members, a union is your most efficient option. They allow us to use the same memory location for multiple purposes. It is declared by using the keyword “union“.
It literally means "type-definition", it is used for creating alternative names for data types such as int, float, char, and so on. It can also be used to have an alternative name for user-defined data types. It provides clarity to your code and makes it easier to update your code.
Now, you can use integer instead of int in the program.
Consider this example: