-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_2.html
More file actions
107 lines (89 loc) · 3.96 KB
/
Copy pathCpp1_2.html
File metadata and controls
107 lines (89 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<article>
<h1>C++ I: Page 2 - Variables & Primitives</h1>
<section>
<h2>The Building Blocks of Data</h2>
<p>C++ is statically typed, meaning you must declare the type of a variable before using it. This allows the compiler to perform rigorous checks and optimize memory layout for specific data sizes.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: primitives.cpp
* Demonstration of C++ primitive types, type modifiers, and type inference
*/
#include <iostream>
#include <string>
#include <limits> // For numerical limits
#include <typeinfo> // For typeid
using namespace std;
// Enum for readable state constants
enum State { IDLE, RUNNING, ERROR };
// Struct for grouping related primitives
struct UserProfile {
int id;
double balance;
bool isActive;
};
// Function to print type info
void PrintTypeInfo(const char* name, const auto& val) {
cout << name << " type: " << typeid(val).name() << endl;
}
int main() {
// 1. Integer types with modifiers
short s = 10; // Usually 2 bytes
int i = 1000; // Usually 4 bytes
long l = 1000000L; // Usually 4 or 8 bytes
unsigned int ui = 500U; // Only non-negative
// 2. Floating point
float f = 3.14f;
double d = 3.1415926535;
// 3. Characters and Strings
char c = 'A';
string str = "Hello C++";
// 4. Boolean
bool isRunning = true;
// 5. Constants (Cannot be modified)
const int MAX_USERS = 100;
cout << "--- Primitive Types Summary ---" << endl;
cout << "Integer: " << i << endl;
cout << "Double: " << d << endl;
cout << "String: " << str << endl;
// 6. Modern C++: Type Inference with 'auto'
auto inferredInt = 100; // Deduced as int
auto inferredDouble = 5.5; // Deduced as double
PrintTypeInfo("inferredInt", inferredInt);
// 7. Using the struct
UserProfile user = {1, 150.50, true};
cout << "User ID: " << user.id << " | Balance: " << user.balance << endl;
// 8. Sizeof operator for platform inspection
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}
/*
* C++ Type System Nuances:
* 1. sizeof(type) helps determine platform-specific size, critical for memory optimization.
* 2. Unsigned types cannot represent negative numbers; useful for counting/size values.
* 3. 'const' keyword ensures data immutability, allowing compiler optimizations.
* 4. 'auto' keyword (C++11+) simplifies code by letting the compiler deduce types.
* 5. Structs are lightweight containers for data; class-like behavior without
* the default private access modifier (by default, structs are public).
* 6. typeid operator is useful for runtime type information (RTTI),
* though often used sparingly in performance-critical paths.
* 7. Enums provide named integral constants, improving code readability.
*/
</code></pre>
<div class="interactive-sim">
<h3>Variable Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_2');
out.innerHTML = 'int x = 10; int y = 20;<br>';
setTimeout(() => out.innerHTML += '>> Sum = ' + (10 + 20), 800);
">Run Simulation</button>
<div id="sim-output-cpp1_2" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp1_1.html" style="text-decoration: none; color: #6c757d;">← Previous: Intro & Environment</a>
<a href="#" data-file="Cpp1_3.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Operators & Logic →</a>
</div>
</footer>
</article>