Change Theme
# C++ Fundamentals ### Lecture 3: Structure, Scope, & Storage
### Basic Program Structure ## `hello.cpp` A minimal C++ program: ```cpp #include
int main() { std::cout << "Hello, World!" << std::endl; return 0; } ``` --- ## `hello.cpp` A minimal C++ program: ```cpp #include
using namespace std; int main() { cout << "Hello, World!" << endl; return 0; } ``` --- ### Key Parts Explained - `#include
` - A **preprocessor directive** that includes the standard input/output stream library. - `int main()` - The main **function** where program execution begins. - `std::cout` - The standard **output stream** object, used for printing. `std` is the namespace. - `return 0;` - A **return statement** that signals successful program termination.
# Namespaces --- ## Concept - **Purpose:** To group related identifiers (variables, functions, classes) to avoid naming conflicts. - The `std::` prefix tells the compiler to look for an identifier (`cout`, `endl`) in the `std` (standard) namespace. - **Best Practice:** Prefer explicit `std::` usage. Avoid `using namespace std;` in header files or at a global level. --- ## Example ```cpp // Custom namespace for our math functions namespace MyMath { int add(int a, int b) { return a + b; } } int main() { // Use the scope resolution operator :: int result = MyMath::add(5, 3); // result is 8 return 0; } ```
# C++ Operators --- ## Common Operators | Category | Operators | Example | | :--- | :--- | :--- | | Arithmetic | `+`, `-`, `*`, `/`, `%` | `5 + 3` | | Assignment | `=`, `+=`, `-=` | `x = 5` | | Comparison | `==`, `!=`, `<`, `>` | `a == b`| | Logical | `&&`, `\|\|`, `!` | `a > 0 && b > 0` | | Increment | `++`, `--` | `i++` | | Scope Resolution | `::` | `std::cout` | --- ## Operator Precedence - Determines the order of operations in an expression. - Just like `*` is evaluated before `+` in math. - **Rule of Thumb:** When in doubt, use parentheses `()` to enforce the order you want. It makes your code safer and easier to read. ```cpp // Without parentheses, multiplication is first int x = 5 + 3 * 2; // result is 11 // With parentheses, addition is first int y = (5 + 3) * 2; // result is 16 ```
# Variable Scope --- ## What is Scope? - **Visibility:** The region of code where a variable can be accessed by its name. - **Lifetime:** The period during which the variable exists in memory. These are related but distinct concepts! --- ## Types of Scope - **Local Scope (Block Scope):** - Declared inside a function or block `{...}`. - Created when the block is entered, destroyed when it's exited. - **Global Scope (File Scope):** - Declared outside of all functions. - Exists for the entire duration of the program. - **Warning:** Use sparingly to avoid bugs. --- ## Scope Example ```cpp #include
int globalVar = 100; // Global scope void myFunction() { int localVar = 10; // Local scope to myFunction std::cout << "Local: " << localVar << std::endl; std::cout << "Global: " << globalVar << std::endl; } int main() { myFunction(); // ERROR! localVar is not in this scope // std::cout << localVar; std::cout << "Global from main: " << globalVar << std::endl; return 0; } ```
# Storage Classes --- ## Concept A specifier that controls a variable's **lifetime** and **linkage** (visibility across files). Main specifiers we care about: `static` and `extern`. --- ### `static` (inside a function) - **Lifetime:** Becomes the entire program duration. - The variable **keeps its value** between function calls. - It is initialized only **once**. ```cpp cout << "--- Static Variable Example ---" << endl; { static int count = 0; // persists between block executions count++; cout << "Called " << count << " times." << endl; } { static int count = 0; // persists across runs of program block count++; cout << "Called " << count << " times." << endl; } ``` --- ### `extern` - **Declaration**, not a definition. - Tells the compiler: "This global variable is defined in another file, but I want to use it here." - Used to **share a single global variable** across multiple `.cpp` files. --- **file1.cpp** ```cpp // Definition of the variable int sharedData = 42; ``` **file2.cpp** ```cpp #include
// Declaration telling compiler it exists elsewhere extern int sharedData; void useSharedData() { std::cout << sharedData; // Accesses 42 from file1.cpp } ``` Compile both files by linking them ``` g++ file1.cpp file2.cpp -o myprogram ``` --- ## Summary - **Local (default):** Temporary, visible only inside a function/block. - **Global (default):** Permanent, visible everywhere (potentially dangerous). - **`static` (local):** Permanent, but visible only inside a function/block. - **`extern`:** A promise that a global variable exists in another file.