C++ Data Types
In C++, data types are classifications that specify the type of data a variable can hold. Understanding data types is fundamental to programming as it affects memory usage, performance, and the operations you can perform on the data. C++ provides several built-in data types and allows the creation of user-defined data types. Here’s an overview of the various data types in C++:
1. Basic Data Types
These are the fundamental data types provided by C++.
a. Integer Types
int
: Represents a signed integer, typically 4 bytes.int age = 25;
short
: Represents a short signed integer, typically 2 bytes.short height = 160;
long
: Represents a long signed integer, typically 4 or 8 bytes.long population = 7800000000;
long long
: Represents a long long signed integer, typically at least 8 bytes.long long distance = 123456789012345;
b. Character Type
char
: Represents a single character, typically 1 byte.char initial = 'A';
c. Floating-Point Types
These types represent numbers with fractional parts.
float
: Represents a single-precision floating-point number, typically 4 bytes.float salary = 50000.50f;
double
: Represents a double-precision floating-point number, typically 8 bytes.double pi = 3.14159;
long double
: Represents an extended-precision floating-point number, typically at least 8 bytes (but often more).long double e = 2.718281828459045;
d. Boolean Type
bool
: Represents a boolean value, eithertrue
orfalse
.bool isStudent = true;
2. Modifiers
Modifiers can be applied to the basic data types to alter their meaning. They change the size or the sign of the data type.
signed
: Can hold both positive and negative values (default forint
).unsigned
: Can hold only non-negative values, effectively doubling the maximum value that can be stored.unsigned int uAge = 30; // Can only be non-negative
short
: Modifies the size of the integer type to a smaller one.long
: Modifies the size of the integer type to a larger one.
3. Derived Data Types
These data types are derived from the basic data types.
- Arrays: A collection of elements of the same data type.
int numbers[5] = {1, 2, 3, 4, 5};
- Functions: A block of code that performs a specific task and can return a value.
int add(int a, int b) { return a + b; }
- Pointers: Variables that store the memory address of another variable.
int* ptr = &age; // Pointer to an integer
4. User-defined Data Types
C++ allows the creation of user-defined data types to model complex data more effectively.
a. Structures
Used to group different data types under a single name.
struct Person {
std::string name;
int age;
};
b. Unions
Allows storing different data types in the same memory location, but only one at a time.
union Data {
int intValue;
float floatValue;
};
c. Enumerations (Enums)
Defines a variable that can hold a set of predefined constants.
enum Color { RED, GREEN, BLUE };
d. Classes
Defines a blueprint for creating objects, encapsulating data and functions.
class Car {
public:
std::string brand;
void honk() {
std::cout << "Beep!";
}
};