11
OctData Types in C++: Primitive, Derived and User-defined Types
Data Types in C++: An Overview
A Data Type is a classification of specific types of data by a certain value or certain types of mathematical or logical operations. Data type depends on the command of the programming language such as letters, lowercase, uppercase, numbers, punctuation, and many more. C++ Online Training will help you to understand the principles of data types C++ is essential for good programming because it determines the type, size, and behaviour of variables and enables proper data manipulation.
What is Data Type in C++?
Data types in C++ programming
Primitive Data type
- Integer
- Character
- Boolean
- Floating Point
- Double Floating Point
- Valueless or Void
- Wide Character
1.) Integer
Integer data types represent whole numbers without a fractional or decimal part. They can be signed (positive, negative, or zero) or unsigned (only positive or zero).
Example
int signedInt = -42;unsigned int unsignedInt = 123;
This code declares two integer variables, one signed ("signedInt") with a value of -42 and the other unsigned ("unsignedInt") with a value of 123.
2.) Character
Character data types represent individual characters from a character set, like ASCII or Unicode. In C++, `char
` is commonly used to represent characters.
Example
char myChar = 'A';
The character variable "myChar
" with the value "A
" is declared using this code.
3.) Boolean
Boolean data types represent binary values, typically used for true (1) or false (0) conditions. In C++, `bool` is used for Boolean data.
Example
bool isTrue = true;
bool isFalse = false;
The two boolean variables "isTrue
" and "isFalse
" are declared in this code with the values true and false, respectively.
4.) Floating Point
Floating-point data types represent numbers with a fractional part. In C++, `float` is a single-precision floating-point type.
Example
float myFloat = 3.14159f;
The floating-point variable "myFloat
" is declared in this code with the value 3.14159.
5.) Double Floating Point
Double-precision floating-point data types are used to represent numbers with a larger range and higher precision compared to 'float'. In C++, 'double' is commonly used.
Example
double myDouble = 3.141592653589793;
This code declares a high-precision double-precision floating-point variable named "myDouble
" with the value of (pi).
6.) Valueless or Void
The void data type in C++ is used to indicate that a function does not return any value or to declare generic pointers that do not point to a specific data type.
Example
void myFunction() { // This function does not return a value
}
void* genericPointer;
This code declares a generic void pointer named "genericPointer
" as well as the void function "myFunction
," which has no return value.
7.) Wide Character
Wide character data types, like 'wchar_t
', are used to represent characters from extended character sets, such as Unicode characters that require more storage than a standard `char`.
Example
wchar_t myWideChar = L'A'; // Assigning the wide character 'A'
Using the wide-character literal "L," this code declares the wide-character variable "myWideChar" with the value "A."
Read More - C++ Interview Interview Questions and Answers
Derived Data type
- Function
- Array
- Pointer
- Reference
1. Function
A function is a reusable block of code that performs a specific task. It is a derived data type because you can define functions to work with other data types and can even return values of different data types.
Example
// Function that adds two integers and returns the result
int add(int a, int b) {
return a + b;
} // Function that concatenates two strings and returns the result
std::string concatenate(std::string str1, std::string str2) {
return str1 + str2;
} int main() {
int sum = add(5, 3);
std::string result = concatenate("Hello, ", "World!");
return 0;
}
The main
function in this code employs the two functions "add" for integer addition and "concatenate" for text concatenation, and it returns 0 as a result.
2. Array
An array is a derived data type that represents a collection of elements of the same data type, stored in contiguous memory locations. The elements can be accessed by their index.
Example
// Declaration and initialization of an integer array
int numbers[5] = {1, 2, 3, 4, 5}; // Accessing elements of the array
int firstNumber = numbers[0]; // Accessing the first element (index 0)
int thirdNumber = numbers[2]; // Accessing the third element (index 2)
This code defines and initializes an integer array called "numbers" with values ranging from 1 to 5. It then accesses and assigns the first element to "firstNumber" and the third element to "thirdNumber" by referring to the elements' respective indices (0 and 2).
3. Pointer
A pointer is a derived data type that stores the memory address of another data type. Pointers are often used for dynamic memory allocation and for accessing memory locations directly.
Example
// Declaration and initialization of a pointer
int* ptr; // Pointer to an integer
int num = 42;
ptr = # // Assigning the address of 'num' to 'ptr' // Accessing the value through the pointer
int value = *ptr; // Dereferencing the pointer to get the value (value = 42)
This code defines and initializes an integer pointer called "ptr," gives it the address of the integer variable "num," and then accesses the value stored at that address by dereferencing the pointer. The value 42 is then stored in the variable "value."
4. Reference
A reference is a derived data type that acts as an alias or an alternative name for an existing variable. It allows you to manipulate the original variable directly, and it cannot be null or reseated to another variable after initialization.
Example
int originalVar = 100;
int&
refVar = originalVar; // Creating a reference to 'originalVar'
refVar = 200; // Modifying 'originalVar' through 'refVar'
// Both 'originalVar' and 'refVar' now have the value 200
This code in C++ Online Compiler generates a reference variable "refVar" that references to the integer variable "originalVar" and allows adjustments through it, so updating "refVar" to 200 updates "originalVar" to 200 as well, resulting in both variables having the value 200.
Read more: Call by Value and Call by Reference in C++
User-defined or Abstract Data Type
- Class
- Structure
- Union
- Enumeration
- Typedef defined Datatype
1. Class
A class is a user-defined data type that represents a blueprint for creating objects. It encapsulates data (attributes) and functions (methods) that operate on that data. Classes are used to model real-world entities and provide a way to define custom data structures.
Example
// Define a class representing a 'Person'
class Person {
public:
// Attributes
std::string name;
int age; // Constructor
Person(std::string n, int a) : name(n), age(a) {} // Method to display information
void displayInfo() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
int main() {
// Create objects of the 'Person' class
Person person1("Alice", 30);
Person person2("Bob", 25); // Access and use class members
person1.displayInfo();
person2.displayInfo();
return 0;
}
This code creates a class called "Person" that has attributes for name and age as well as a constructor that initializes those attributes and a method called "displayInfo" that prints out the person's data. Two "Person" objects are created, and initialized with distinct data, and their information is displayed using the "displayInfo" method in the "main" function.
Read more: Object Oriented Programming (OOPs) Concepts in C++
2. Structure
A structure is a user-defined data type that groups variables of different data types under a single name. Structures are useful for organizing related data elements.
Example
// Define a structure for representing a 'Point' in 2D space
struct Point {
int x;
int y;
};
int main() {
// Create instances of the 'Point' structure
Point p1 = {2, 3};
Point p2 = {5, 7}; // Access structure members
int sumX = p1.x + p2.x;
int sumY = p1.y + p2.y;
return 0;
}
This program defines a "Point
" structure to represent two-dimensional coordinates builds two identical instances of the structure, and then computes the sums of the x and y components, storing the results in the "sumX
" and "sumY
" variables, respectively.
3. Union
A union is a user-defined data type that allows you to store different data types in the same memory location. Unlike structures, which allocate memory for all members, unions share memory among their members.
Example
// Define a union representing a 'Value' that can be either an integer or a float union Value {
int intValue;
float floatValue;
};
int main() {
// Create a union instance
Value v; // Assign values to union members
v.intValue = 42;
// Now, 'v.floatValue' might not be meaningful because the same memory is shared.
return 0;
}
This code constructs a union named "Value
" that can hold either an integer or a float, but because they share the same memory space, accessing one member (for example, "v.floatValue") after assigning a value to another (for example, "v.intValue") can provide unexpected results.
4. Enumeration
An enumeration is a user-defined data type that consists of a set of named integer constants. It provides a way to create symbolic names for values to improve code readability.
Example in C++ Compiler
// Define an enumeration for days of the week
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main() {
// Use enumeration constants
Days today = Wednesday;
if (today == Wednesday) {
std::cout << "It's Wednesday!" << std::endl;
}
return 0;
}
This code creates the "Days" enumeration, which represents the seven days of the week, assigns the value "Wednesday" to the variable "today," then checks to see if "today" equals "Wednesday," printing a message if it does.
4. Typedef Defined Data Type
typedef is a keyword that allows you to create aliases for existing data types, making your code more readable and providing abstraction. It's not a new data type but a way to create alternative names for existing ones.
Example
// Define a typedef for an integer pointer
typedef int* IntPtr;
int main() {
int number = 42;
IntPtr ptr = &number; // 'IntPtr' is an alias for 'int*'
// Now, 'ptr' is equivalent to 'int*' and can be used accordingly
return 0;
}
The typedef alias "IntPtr" is created by this code for an integer pointer (int*), and it is then used to declare the pointer variable "ptr," enabling it to be used as a conventional integer pointer, referring to the integer variable "number."
Datatype Modifiers in C++
In C++, data type modifiers are used to modify the behavior and storage characteristics of basic data types. In C++, there are four modifiers. int, double, and char are the data types that can be modified using these modifiers. They are as follows:
- Signed
- Unsigned
- Short
- Long
1. Signed
The signed modifier is used to indicate that a variable can hold both positive and negative values. However, most built-in integer types (like int) are signed by default in C++. So, you rarely need to explicitly use the signed keyword.
Example
int signedVariable = -10; // "int" is signed by default
This code in C++ Online Editor declares the signed integer variable "signedVariable" with the value -10. It's vital to know that "int" is signed by default in C++.
2. Unsigned
The unsigned modifier is used to indicate that a variable can only hold non-negative values (i.e., zero or positive values). It is often used when you know that a variable should not have negative values, and you want to save memory by avoiding the storage of negative numbers.
Example
unsigned int unsignedVariable = 20; // unsigned integer
The unsigned integer variable "unsignedVariable" is declared in this code with the value 20 and the restriction that it can only store non-negative integer values.
3. Short
The short modifier is used to indicate that a variable should be stored using fewer bits than the default data type. This is often used to save memory when you know that a variable's value will fit within a smaller range.
Example
short shortVariable = 32767; // short integer
The maximum value that can be declared for the short integer variable "shortVariable" according to this code is 32767.
4. Long
The long modifier is used to indicate that a variable should be stored using more bits than the default data type. This is used when you need to store larger values that cannot fit within the range of a standard data type.
Example
long long longVariable = 1234567890; // long integer
Data Type | Size | Range |
int or signed int | 4 Bytes | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 Bytes | 0 to 4,294,967,295 |
short int | 2 bytes | -32,768 to 32,767 |
long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned short int | 2 bytes | 0 to 65,535 |
unsigned long int | 8 Bytes | 0 to 4,294,967,295 |
long long int | 8 Bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8 Bytes | 0 to 18,446,744,073,709,551,615 |
signed char | 1 Bytes | -128 to 127 |
unsigned char | 1 Bytes | 0 to 255 |
wchar_t | 2 or 4 Bytes | 1 wide character |
float | 4 Bytes | |
double | 8 Bytes | |
long double | 12 Bytes |
Summary
This article gives a vast overview of what is data type in C++ language including its various data types in C++ with examples. This article also covers the size of data types in C++. With this knowledge of data types,C++ certification under your belt, you're well on your way to becoming a great C++ programmer!
FAQs
Q1. What are the 5 Data Types in C++?
Q2. What is a Data Type in C++?
Q3. What are the uses of Data Types in C++?
- Describing what kind of data a variable can hold.
- Efficient memory allocation for variables.
- Ensuring adequate data manipulation and expression and function compatibility.
- By specifying the type of data being utilized in detail, it is possible to improve the readability and maintainability of code.