Difference between Fundamental Data Type and Derived Data Types in C++
In this article, you will learn about the difference between fundamental data type and derived data types in C++. But before discussing their differences, you must know about the fundamental data type and derived data types.
What are the Fundamental data types?
Fundamental data types, or primitive data types, are the basic building blocks that store simple values. C++ provides a set of pre-defined types, each with a specific size and range of values it can hold.
The essential ones:
int:
It stores integers (whole numbers). It's the go-to choice for representing quantities, ages, scores, etc.
Syntax:
int age = 25; (declares and initializes an integer variable named age)
Example: Storing the number of items in a shopping cart (can't have half an apple!).
char:
It stores a single character. It is perfect for representing letters, symbols, or individual characters in a string.
Syntax:
char initial = 'A'; (declares and initializes a character variable named initial)
Example: Storing the first letter of a person's name or a special character used in a game.
float:
It stores single-precision floating-point numbers (numbers with decimals). This type is suitable for calculations where high precision isn't critical.
Syntax:
float pi = 3.14159; (declares and initializes a float variable named pi)
Example: Representing percentages and scientific data where exact precision isn't crucial.
double:
It stores double-precision floating-point numbers. It offers higher precision compared to float for calculations requiring more accuracy.
Syntax:
double distance = 100.50; (declares and initializes a double variable named distance)
Example: Financial calculations and scientific computations demand high precision.
bool:
It represents Boolean values (true or false). This fundamental type is the foundation of logical operations and conditional statements.
Syntax:
bool isRunning = true; (declares and initializes a bool variable named isRunning)
Example: Checking if a game is running, representing a flag indicating a specific condition.
Program:
Let’s take a C++ program to illustrate the fundamental data types.
#include <iostream>
int main() {
// int
int age = 25;
std::cout << "Age: " << age << std::endl;
// char
char initial = 'A';
std::cout << "Initial: " << initial << std::endl;
// float
float pi = 3.14159f; // Use 'f' suffix for float literals
std::cout << "Pi (float): " << pi << std::endl;
// double
double distance = 100.50;
std::cout << "Distance (double): " << distance << std::endl;
// bool
bool isRunning = true;
std::cout << "Is running: " << isRunning << std::endl;
return 0;
}
Output:
![Difference between Fundamental Data Type and Derived Data Types in C++](https://static.tutorialandexample.com/cpp/difference-between-fundamental-data-type-and-derived-data-types-in-cpp1.png)
What are Derived Data Types?
Derived data types, also known as user-defined data types, are as Lego sets. They are more complex structures built using fundamental data types, just as you can build intricate structures using simple Lego bricks. They offer greater flexibility in organizing and manipulating your data, making them a practical tool in your programming journey. Let's delve into the most common ones:
Arrays:
An array is like a tray of cupcakes. It's an ordered collection of elements of the same data type, just as a tray of cupcakes contains multiple cupcakes of the same flavour. Consider it a simple, fixed-size box that can hold a specific number of similar items.
Syntax:
int numbers[5] = {1, 2, 3, 4, 5}; (declares an array named numbers of size 5 and initializes it with elements)
Parameters: The size within square brackets specifies the number of elements the array can hold.
Example:
Let’s take an example to illustrate the Arrays in Derived Data Types in C++.
#include <iostream>
int main() {
int numbers[5] = {1, 2, 3, 4, 5}; // Array of size 5 with initial values
std::cout << "Numbers: ";
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Numbers: 1 2 3 4 5
Pointers:
Pointers are variables that store memory addresses of other variables. Imagine a pointer as a sign pointing to the location (address) where the actual data resides.
Syntax:
int* ptr = &age; (declares a pointer variable ptr that holds the memory address of the variable age)
Parameters: The asterisk (*) before the variable name indicates it's a pointer.
Example:
Let’s take an example to illustrate the Pointers in Derived Data Types in C++.
#include <iostream>
int main() {
int num = 10;
int* ptr = #
std::cout << "Value of num: " << num << std::endl;
std::cout << "Address of num: " << ptr << std::endl;
std::cout << "Value pointed to by ptr: " << *ptr << std::endl;
return 0;
}
Output:
![Difference between Fundamental Data Type and Derived Data Types in C++](https://static.tutorialandexample.com/cpp/difference-between-fundamental-data-type-and-derived-data-types-in-cpp2.png)
Structures:
Structures are user-defined collections of variables with different data types grouped under a single name. Consider it as a blueprint for creating custom data objects that combine various data types.
Syntax:
It has the following syntax:
struct Student {
int id;
std::string name;
float gpa;
};
Student John; // Declares a student variable
John.id = 123; John.name = "John Doe"; John.gpa = 3.8; // Assigns values to structure members
Example:
Let’s take an example to illustrate the Structures in Derived Data Types in C++.
#include <iostream>
#include <string>
struct Student {
int id;
std::string name;
float gpa;
};
int main() {
Student John;
John.id = 123;
John.name = "John Doe";
John.gpa = 3.8;
std::cout << "Student Information:\n";
std::cout << "ID: " << John.id << std::endl;
std::cout << "Name: " << John.name << std::endl;
std::cout << "GPA: " << John.gpa << std::endl;
return 0;
}
Output:
![Difference between Fundamental Data Type and Derived Data Types in C++](https://static.tutorialandexample.com/cpp/difference-between-fundamental-data-type-and-derived-data-types-in-cpp3.png)
Unions: (While unions are less commonly used, here's an example)
Example:
Let us take an example to represent a data value that can be either an integer or a float (but only one at a time):
#include <iostream>
union Data {
int num;
float dec;
};
int main() {
Data data;
data.num = 10; // Store an integer value
std::cout << "Integer value: " << data.num << std::endl;
data.dec = 3.14f; // Store a float value (overwrites the integer)
std::cout << "Float value: " << data.dec << std::endl;
// data.num will now be undefined
return 0;
}
Output:
![Difference between Fundamental Data Type and Derived Data Types in C++](https://static.tutorialandexample.com/cpp/difference-between-fundamental-data-type-and-derived-data-types-in-cpp4.png)
Use Cases of the Derived Data Types:
There are several use cases of the Derived Data Types. Some main use cases of the Derived Data Types are as follows:
- Arrays: It stores collections of similar data (scores, items in a list).
- Pointers: Dynamic memory allocation, function arguments that modify original data, working with memory addresses.
- Structures: Group related data into a single unit (student information, product details).
- Unions: When you only need one value at a time from a set of options (data type can change based on the stored value).
Key Differences between Fundamental Data Types and Derived Data Types
There are several differences between the Fundamental Data Types and the Derived Data Types. Some main differences between the Fundamental Data Types and the Derived Data Types are as follows:
Feature | Fundamental Data Types | Derived Data Types |
Definition | Pre-defined building blocks | User-defined complex structures |
Data types used | Basic data types (int, char, etc.) | Combination of fundamental types |
Memory allocation | Fixed size at compile time | Can be dynamic (arrays, pointers) |
Operations | Basic arithmetic, logical | User-defined based on structure |
Use cases | Basic data representation | Complex data organization |