A data type in C++ defines the type of data a variable can hold, determining the kind of values it can store and the operations that can be performed on it. C++ has various data types.

1. Primitive Data Types
These are the fundamental data types provided by C++ and form the foundation for all other types.
int
It is used to represent whole numbers (both positive and negative) without any decimal points.
It has a typical 4-byte int, the range is from -2,147,483,648 to 2,147,483,647.
int age= 35;
char
It is used to store a single character (e.g., ‘A’, ‘b’, etc.). In C++, characters are stored as integer values representing the ASCII code of the character.
It has Typically 1 byte. It has Typically range 0 to 255 for an unsigned char or -128 to 127 for a signed char.
char result = A;
float
It is used to store numbers with decimal points. It’s a single-precision floating-point number.
It has Typically 4 bytes and It has range usually -3.4E+38 to 3.4E+38
float marks = 98.99;
double
It is used to store numbers with decimal points with more precision than float.
It has Typically 8 bytes and usually range -1.7E+308 to 1.7E+308.
double a = 2.718281828459045;
bool
It is used to store boolean values, which can be either true or false.
It has Typically 1 byte and range Only two possible values: true (1) or false (0).
bool isManager = true;
2. Derived Data Types
These types are derived from basic data types and are used for more complex data structures.
Array
An array is a collection of elements of the same type stored in contiguous memory locations. The size of the array must be known at compile time.
int numbers[5] = {10, 20, 30, 40, 50};
numbers that can hold 5 integers.
Pointers
A pointer is a variable that stores the memory address of another variable. It is used for dynamic memory management and referencing variables indirectly.
int age = 35;
int* ptr = &age; // ptr stores the address of age
cout << *ptr; // Dereferencing the pointer to get the value stored at the address
References
A reference is an alias for another variable. It allows you to refer to the same memory location using a different name.
int age = 35;
int& ref = age; // ref is a reference to age
ref = 45; // age is now 45
Functions
In C++, a function is a block of code that performs a specific task. Functions themselves are types and can be pointers or can return values.
int add(int a, int b) {
return a + b;
}
3. User-Defined Data Types
struct (Structure)
A struct is a user-defined data type that allows you to group different types of data together under a single name.
struct Employee {
string name;
int age;
float salary;
};
Employee emp1 = {"John", 35, 15000.45};
union (Union)
A union allows different types of data to share the same memory space. Only one member can hold a value at a time.
Example:
union MyData {
int a;
char b;
float c;
};
MyData data;
data.a = 10; // Only one field will hold the value at any given time
enum (Enumeration)
An enum is a way of defining a set of named integer constants. It's used when you have a fixed set of related values.
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Day today = Sunday;
class
A class is a blueprint for creating objects (instances). It allows you to define both data (attributes) and functions (methods) that operate on that data.
#include <iostream>
#include <string> // Include this header for std::string
using namespace std; // This allows you to avoid using std:: every time
class Employee {
public:
string name;
int age;
void start() {
cout << "my employee name is John" << endl;
}
};
int main() {
Employee emp1; // Create an object of type Employee
emp1.name = "John"; // Set the model of the car
emp1.start(); // Call the start method
return 0;
}
Void Data Type
void
The void type represents the absence of a type. It’s used for functions that do not return a value and for pointers that point to unknown data types.
Example:
void displayMessage() {
cout << "Hello, My Friends!" << endl;
}