Variable Types
In C++, variables can be of various types, each serving different purposes and consuming different amounts of memory. Here's an overview of the standard variable types in C++:
Basic Data Types
Integer Types
- int: A standard integer type.
- short: A short integer, typically 2 bytes.
- long: A long integer, typically 4 or 8 bytes.
- long long: A long long integer, typically 8 bytes.
Example:
int age = 25;
short year = 2024;
long population = 7800000000;
long long distance = 123456789012345;
Floating-Point Types
- float: Single-precision floating-point.
- double: Double-precision floating-point.
- long double: Extended-precision floating-point.
Example
float height = 5.9f;
double pi = 3.141592653589793;
long double e = 2.718281828459045L;
Character Types
- char: Represents a single character, typically 1 byte.
- wchar_t: Represents a comprehensive character.
Example:
char initial = 'A';
wchar_t wideChar = L'Ω';
Boolean Type
- bool: Represents a boolean value, true or false.
Example:
bool isStudent = true;
Derived Data Types
Arrays
- A collection of elements of the same type.
int numbers[5] = {1, 2, 3, 4, 5};
Pointers
- Variables that store memory addresses of other variables.
Example:
int x = 10;
int* ptr = &x;
References
- An alias for another variable.
Example:
int y = 20;
int& ref = y;
Enumerations
- A user-defined type consisting of a set of named integral constants.
Example:
enum Color { RED, GREEN, BLUE };
Color myColor = GREEN;
User-Defined Types
Structures
- A collection of variables of different types.
struct Person {
int age;
float height;
char initial;
};
Person john = {25, 5.9, 'J'};
Unions
- Like structures, it can store only one of its non-static data members at a time.
Example:
union Data {
int i;
float f;
char str[20];
};
Data data;
data.i = 10;
Classes
- Similar to structures but with access control and other OOP features.
Example:
class Car {
public:
int speed;
void setSpeed(int s) {
speed = s;
}
};
Car myCar;
myCar.setSpeed(100);
Example Code
Here's a complete example demonstrating different variable types in C++:
#include <iostream>
using namespace std;
// Enumeration type
enum Color { RED, GREEN, BLUE };
// Structure type
struct Person {
int age;
float height;
char initial;
};
// Union type
union Data {
int i;
float f;
char str[20];
};
// Class type
class Car {
public:
int speed;
void setSpeed(int s) {
speed = s;
}
};
int main() {
// Basic data types
int age = 25;
float height = 5.9f;
char initial = 'J';
bool isStudent = true;
// Derived data types
int numbers[5] = {1, 2, 3, 4, 5};
int* ptr = &age;
int& ref = age;
// Enumeration
Color myColor = GREEN;
// Structure
Person john = {25, 5.9, 'J'};
// Union
Data data;
data.i = 10;
// Class
Car myCar;
myCar.setSpeed(100);
// Output values
cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Initial: " << initial << endl;
cout << "Is student: " << boolalpha << isStudent << endl;
cout << "Numbers: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
cout << endl;
cout << "Pointer to age: " << *ptr << endl;
cout << "Reference to age: " << ref << endl;
cout << "Color: " << myColor << endl;
cout << "John's age: " << john.age << endl;
cout << "John's height: " << john.height << endl;
cout << "John's initial: " << john.initial << endl;
cout << "Union data (int): " << data.i << endl;
cout << "Car speed: " << myCar.speed << endl;
return 0;
}
This code provides a comprehensive overview of variable types in C++ and demonstrates their usage.