class: center, middle # CMPE 30: Lecture 1 Introduction to C++ --- # Learning Objectives - Write, compile, and run a `Hello, World!` program - Explain `#include`, `main()`, semicolons, and curly braces - Use `std::cout` to write output and `std::cin` to read input - Describe what a namespace is and why `std::` is everywhere - Read command-line arguments via `argc` / `argv` --- # Why C++? - A **compiled** language -- source code is translated into a runnable program - Used for games, browsers, operating systems, embedded, finance, science - Trade-off: more control over the machine, more responsibility for you - Today is the tiniest taste -- we spend the whole term going deeper --- # Hello, World! .lc[ ```cpp #include
int main() { std::cout << "Hello, World!" << std::endl; return 0; } ``` ] .rc[ - `#include
` pulls in the I/O library - `int main()` -- program entry point - `<<` is the stream insertion operator - `std::endl` ends the line and flushes - `return 0;` -- `0` means success ] --- # Compiling and Running Save as `hello.cpp` then run: ``` c++ -o hello hello.cpp ./hello ``` Always compile with warnings: ``` c++ -Wall -Wextra -pedantic -o hello hello.cpp ``` - `-o hello` names the output (otherwise you get `a.out`) - The compiler is your friend -- turn the warnings on --- # What a Compiler Error Looks Like Drop the `;` after `std::endl`: ``` hello.cpp:5:5: error: expected ';' before 'return' 5 | return 0; | ^~~~~~ ``` - Read the message - Check the line *above* the one reported - Look for missing `;` or `}` --- # Semicolons and Curly Braces - Every **statement** ends with `;` - `{` and `}` define a **block** of code - They always come in pairs -- open one, close one - Indent everything inside a block (4 spaces) - Missing `;` or `}` is the most common beginner mistake --- # Namespaces and std:: - The C++ standard library groups names inside the `std` namespace - `std::cout` means "the `cout` that lives in `std`" - `::` is the **scope resolution** operator - Shortcut: `using namespace std;` -- but avoid it in real code - We write `std::` explicitly for the rest of the course --- # Explicit vs Shortcut .lc[ ```cpp #include
int main() { std::cout << "explicit" << std::endl; } ``` ] .rc[ ```cpp #include
using namespace std; int main() { cout << "shortcut" << endl; } ``` ] --- # Output with std::cout .lc[ ```cpp #include
int main() { std::cout << "Come as you are" << ", " << "as you were" << std::endl; std::cout << "The year is " << 1991 << std::endl; } ``` ] .rc[ - `<<` is evaluated left-to-right, like reading - Strings, numbers, and variables all chain together - Without a newline the output stays on the current line - `std::endl` starts a fresh line ] --- # Input with std::cin .lc[ ```cpp #include
#include
int main() { std::string name; std::cout << "What is your name? "; std::cin >> name; std::cout << "Hola, " << name << "!" << std::endl; } ``` ] .rc[ - `std::cin` is the standard input stream - `>>` is the **extraction** operator - `#include
` is required for `std::string` - `std::cin >> name` reads **one word** -- it stops at whitespace ] --- # Reading Whole Lines ```cpp std::string name; std::getline(std::cin, name); ``` **Trap:** if the user types `Los Del Rio`, `std::cin >> name` only stores `Los`. Use `std::getline` when you want the whole line. Numbers work the same way: ```cpp int year; std::cin >> year; ``` --- # Command-Line Arguments .lc[ ```cpp #include
int main(int argc, char *argv[]) { if (argc < 2) { std::cout << "USAGE: " << argv[0] << "
\n"; return 1; } std::cout << "Hello, " << argv[1] << "!\n"; } ``` ] .rc[ - `argc` -- **argument count**, including program name - `argv` -- array of strings, one per argument - `argv[0]` is always the program name - User arguments start at `argv[1]` - **Always** validate `argc` before touching `argv[i]` ] --- # Putting It Together .lc[ ```cpp #include
#include
int main() { std::string song; int year; std::cout << "Name a 90s song: "; std::getline(std::cin, song); std::cout << "What year? "; std::cin >> year; std::cout << song << " (" << year << ") es increible!\n"; } ``` ] .rc[ Sample run: ``` Name a 90s song: Smells Like Teen Spirit What year? 1991 Smells Like Teen Spirit (1991) es increible! ``` `std::getline` for the song (multiple words), `std::cin >> year` for the number. ] --- # What is printed? .lc[ ```cpp #include
int main() { std::cout << "A" << "B" << std::endl; std::cout << "C" << std::endl; } ``` ] .rc[ 1. `A B C` 1. `AB` then `C` on a new line 1. `A`, `B`, `C` each on own line 1. `ABC` all on one line 1. Ben got this wrong ] --- # Where is the bug? .lc[ ```cpp 1: #include
2: int main() 3: { 4: std::cout << "Here we are now" << std::endl 5: return 0; 6: } ``` ] .rc[ 1. Line 1 -- wrong header 1. Line 2 -- `main` should be `void` 1. Line 4 -- missing semicolon 1. Line 5 -- cannot return `0` 1. Ben got this wrong ] --- # If argc is 4, how many user arguments? 1. 0 1. 2 1. 3 1. 4 1. 5 -- Ben got this wrong --- # Key Points - Every program starts at `main()` - Every statement ends with `;` - `{}` define blocks -- they always pair up - `#include
` for `std::cout` / `std::cin` - `<<` writes, `>>` reads - `std::` names come from the standard library - Validate `argc` before touching `argv[i]` - Always compile with `-Wall -Wextra -pedantic` **Read:** chapter 2 of *Gorgo Starting C++*. **Do:** exercises 1-9.