๐ฆ
Chapter 2
Elementary Data Types
Master scalar and composite data types, understand memory management, and learn type properties in C++.
2.1 Properties of Types and Objects
Understanding the fundamental properties that define types and objects in C++.
| Aspect | Definition | Example |
|---|---|---|
| Static Types | Fixed during compile time. |
int x = 369;
|
| Dynamic Types | Determined at runtime. |
var x = 369; (in dynamic languages)
|
| Mutability | Whether an object can be modified. |
const int x = 369;
|
| Address | Memory location of an object. |
&x gives the address of x
|
| Lifetime | Duration during which an object exists. |
Local variables exist during function execution
|
โถ Type Properties Example
#include <iostream>
using namespace std;
int main() {
// Static typing - type known at compile time
int x = 369;
const int y = 100; // Immutable
cout << "Value of x: " << x << endl;
cout << "Address of x: " << &x << endl;
// y = 200; // Error: cannot modify const
// Lifetime demonstration
{
int temp = 50; // temp exists only in this block
cout << "Temp value: " << temp << endl;
}
// temp is destroyed here
return 0;
}
Click "Run Code" to execute
2.2 Scalar Data Types
Basic data types that hold single values.
| Aspect | Definition | Example |
|---|---|---|
| Integer | Whole numbers. |
int x = 369;
|
| Floating Point | Decimal numbers. |
float y = 3.69;
|
| Character | Single character. |
char ch = 'A';
|
| Boolean | Represents true/false values. |
bool isTrue = true;
|
| Size and Range | Depends on the system architecture. |
int: -2^31 to 2^31-1
|
๐ Data Type Sizes Comparison
โถ Scalar Data Types Example
#include <iostream>
using namespace std;
int main() {
// Integer types
int age = 25;
short year = 2024;
long population = 8000000000L;
// Floating point types
float pi = 3.14159f;
double precise = 3.14159265359;
// Character type
char grade = 'A';
// Boolean type
bool isPassed = true;
// Display values
cout << "Age: " << age << endl;
cout << "Pi (float): " << pi << endl;
cout << "Pi (double): " << precise << endl;
cout << "Grade: " << grade << endl;
cout << "Passed: " << isPassed << endl;
// Size of data types
cout << endl << "Sizes:" << endl;
cout << "sizeof(int): " << sizeof(int) << " bytes" << endl;
cout << "sizeof(float): " << sizeof(float) << " bytes" << endl;
cout << "sizeof(double): " << sizeof(double) << " bytes" << endl;
cout << "sizeof(char): " << sizeof(char) << " byte" << endl;
return 0;
}
Click "Run Code" to execute
2.3 Composite Data Types
Complex data types that combine multiple values.
| Aspect | Definition | Example |
|---|---|---|
| Arrays | Collection of elements of the same type. |
int arr[3] = {3, 6, 9};
|
| Structures | Groups related variables of different types. |
struct Tesla { int id; char name[10]; };
|
| Classes | User-defined data types with methods. |
class MyClass { public: int x; };
|
| Pointers | Holds memory address of another variable. |
int* ptr = &x;
|
| Enums | Represents named integral constants. |
enum Day { MON, TUE, WED };
|
โถ Arrays Example
#include <iostream>
using namespace std;
int main() {
// Array declaration and initialization
int numbers[5] = {3, 6, 9, 12, 15};
cout << "Array elements:" << endl;
for(int i = 0; i < 5; i++) {
cout << "numbers[" << i << "] = " << numbers[i] << endl;
}
// Multi-dimensional array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
cout << endl << "Matrix:" << endl;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
Click "Run Code" to execute
โถ Structures Example
#include <iostream>
#include <cstring>
using namespace std;
// Structure definition
struct Tesla {
int modelNumber;
char name[20];
float batteryCapacity;
bool autopilot;
};
int main() {
// Create and initialize structure
Tesla model3;
model3.modelNumber = 3;
strcpy(model3.name, "Model 3");
model3.batteryCapacity = 75.0;
model3.autopilot = true;
// Display structure data
cout << "Tesla Information:" << endl;
cout << "Model Number: " << model3.modelNumber << endl;
cout << "Name: " << model3.name << endl;
cout << "Battery: " << model3.batteryCapacity << " kWh" << endl;
cout << "Autopilot: " << (model3.autopilot ? "Yes" : "No") << endl;
return 0;
}
Click "Run Code" to execute
โถ Pointers Example
#include <iostream>
using namespace std;
int main() {
int num = 369;
int* ptr = # // Pointer to num
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Value of ptr: " << ptr << endl;
cout << "Value pointed by ptr: " << *ptr << endl;
// Modify through pointer
*ptr = 500;
cout << endl << "After modification through pointer:" << endl;
cout << "Value of num: " << num << endl;
// Pointer arithmetic
int arr[3] = {10, 20, 30};
int* arrPtr = arr;
cout << endl << "Pointer arithmetic:" << endl;
for(int i = 0; i < 3; i++) {
cout << "arr[" << i << "] = " << *(arrPtr + i) << endl;
}
return 0;
}
Click "Run Code" to execute
๐ฏ Pointer Memory Visualization
โถ Enumerations Example
#include <iostream>
using namespace std;
// Enum definition
enum Day {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
enum TeslaModel {
MODEL_S,
MODEL_3,
MODEL_X,
MODEL_Y
};
int main() {
Day today = WEDNESDAY;
TeslaModel myTesla = MODEL_3;
cout << "Today is day: " << today << endl;
cout << "My Tesla model: " << myTesla << endl;
// Using enum in switch
switch(myTesla) {
case MODEL_S:
cout << "You have a Model S - Luxury sedan" << endl;
break;
case MODEL_3:
cout << "You have a Model 3 - Affordable sedan" << endl;
break;
case MODEL_X:
cout << "You have a Model X - SUV" << endl;
break;
case MODEL_Y:
cout << "You have a Model Y - Compact SUV" << endl;
break;
}
return 0;
}
Click "Run Code" to execute
๐ก Practice Exercise
Try these exercises to master data types:
- Create an array of your favorite numbers and calculate their average
- Define a structure for a Student with name, age, and grade
- Use pointers to swap two integer values
- Create an enum for months of the year
- Experiment with different data type sizes using sizeof()