An Introduction to C++

  1. Fastest Environment Setup
    1. Install the C/C++ Extension Pack in vscode, which includes C/C++, C/C++ Themes, CMake Tools extensions.
    2. For Windows systems, use the Winlibs standalone build package, which includes a complete gcc, g++ compiler environment for Windows. Just configure the environment variables to use it.An Introduction to C++
  2. Why Do We Need a Compiler?
    1. C++ is a compiled language, meaning that source code must be converted into machine code by a compiler to generate an executable file, hence the need for a compiler.
    2. In contrast, Python and Lua are interpreted languages that read, parse, and execute code line by line through an interpreter, allowing the source code to run directly without compilation.
  3. Difference Between Local and Cross Compilation Chains
    1. Since C++ does not define a unified virtual machine runtime environment like Java, it requires direct compilation of source code into machine code for the corresponding platform. Therefore, compiled programs cannot be ported across different operating systems and hardware architectures without recompilation, which is why there is a distinction between local and cross compilation chains.
    2. A local compilation chain means that the compilation environment and the execution environment of the executable file are the same; a cross compilation chain means that the compilation environment and the execution environment of the executable file are different. If a file compiled under the X86_64 architecture needs to run in the X86_64 architecture, a local compilation chain is chosen; if it needs to run under the arm architecture, a cross compilation chain is selected.An Introduction to C++
  4. Why Do Java and Go Not Have Local and Cross Compilers?
    1. Both Java and Go use a “compile-interpret” hybrid model, possessing both compilation and interpretation characteristics, but they handle them differently.
    2. Java source code is first compiled by the Javac compiler into bytecode bytecode, stored in .class/.jar files. During execution, this bytecode is interpreted by the Java Virtual Machine (JVM), which translates the bytecode into machine code for the specific platform for execution.
    3. Thanks to the high consistency and standardization of the Java ecosystem, there is virtually no environment for cross-compilation.
    4. Go achieves cross-platform capability through local compilation that supports multiple platforms. The Go compiler can directly compile code into machine code for the target platform, meaning the compiled executable is purely local and does not require an intermediary layer like the JVM. Additionally, the Go toolchain has built-in strong support for cross-compilation.
    5. The executable files of Go are statically linked by default, compiling the dependency libraries directly into the program, which eliminates the need for external environment dependencies at runtime. However, this independence and compilation completeness can increase the size of Go programs.

Basic Data TypesInteger: Represents whole numbers; Character: Represents characters; Boolean: Represents boolean values; Floating-point: Represents decimal numbers.

#include <iostream>
#include <cstdint> // For fixed-length integer types
int main() {    // Integer    int a = 42;    short b = 32767;    long c = 1234567890;    long long d = 1234567890123456789LL;
    // Unsigned integer (requires <cstdint> for specific width types)    uint32_t e = 0xFFFFFFFF; // Maximum value: 4294967295
    // Character    char f = 'A';    wchar_t g = L'Ω'; // Using wide character
    // Boolean    bool h = true;
    // Floating-point    float i = 3.14159f;    double j = 2.718281828459;    long double k = 1.4142135623730950488L;
    // Output some variables    std::cout << "int a: " << a << std::endl;    std::cout << "short b: " << b << std::endl;    std::cout << "long c: " << c << std::endl;    std::cout << "long long d: " << d << std::endl;    std::cout << "uint32_t e: " << e << std::endl;    std::cout << "char f: " << f << std::endl;    std::wcout << L"wchar_t g: " << g << std::endl; // Using std::wcout to output wide character    std::cout << "bool h: " << std::boolalpha << h << std::endl; // Output boolean value name    std::cout << "float i: " << i << std::endl;    std::cout << "double j: " << j << std::endl;    std::cout << "long double k: " << k << std::endl;
    return 0;}

Control StructuresConditional Statements

if (condition1) {    // Execute code block 1}
else if (condition2) {    // Execute code block 2}
else {    // Execute code block 3}

Multi-condition Judgments

switch (expression) {    case constant1:        // Execute code block 1        break;    case constant2:        // Execute code block 2        break;    default:        // Execute default code block, can be omitted        break;}

Loop Statements

// for loop, executes a fixed number of iterations
for (int i = 0; i < 10; ++i) {    // Repeat this code 10 times}
// while loop, executes an uncertain number of iterations, continues as long as the condition is true
while (condition) {    // Repeat this code as long as condition is true}
// do while loop, executes the loop body first, then checks if the condition is true. For example, in a login scenario, first get user input for the password, then check if the password is correct
do {    // Execute this code first} while (condition); // Then check if the condition is true

Jump Statements

break // Exit loop or switch statement
continue // Skip remaining code in the current loop, proceed to the next loop check
return // Return value from function and exit function
goto // Force jump to a certain location, can disrupt execution logic, use with caution

An Introduction to C++

Leave a Comment