C++ Notes
Three tools sit behind every C++ program you run. Two of them turn your source code into something the machine can execute, the third helps you when execution goes wrong.
| Tool | Role |
|---|---|
gcc | Compiler used for C code |
g++ | Compiler used for C++ code |
gdb | Debugger, used after the compilation process is finished |
#include <iostream> loads the library for input and output, and it's also what makes things available under the std namespace.int main() is the entry point of a C++ program. It exists outside any class, unlike Java where everything has to live inside one.return 0 reports success to the OS. Any non-zero value means failure. If you leave it out entirely, success is assumed.std::cout is an object of type std::ostream. It doesn't print directly, it sends data to its internal buffer, which is a std::streambuf object.<< inserts data into cout, and cout then returns itself, which is exactly why you can chain multiple insertions in one line.std::endl is inserted, or right before input is about to be taken through std::cin.std::cin is tied to a separate buffer that receives input, following the path keyboard, then OS, then the cin buffer, then finally your variable.cin and cout do not share a buffer, so they never interfere with each other even when used back to back.| Type | When Caught | What Happens |
|---|---|---|
| Compile Time Error | Compile time | Compilation fails, no execution happens at all. Caused by illegal code such as invalid syntax or broken semantic rules. |
| Runtime Error | During execution | Compilation succeeds, but the program hits an invalid operation while running. May crash and terminate the program. |
| Warning | Compile time | Compilation and execution both succeed. The code is legal but suspicious, and it should not be ignored. |
;.main function, so the compiler already knows it exists by the time it's called.Add these flags to your VS Code tasks.json under args to enable the C++20 standard and turn on strict warnings.
-std=c++20 → enables C++20 standard features-Wall → enables all common warnings-Wextra → enables extra warnings beyond -Wall-pedantic-errors → enforces strict standard compliance and turns warnings into errorsTurning warnings into errors with -pedantic-errors feels strict at first, but it catches the exact kind of "technically works, secretly wrong" code that C++ makes it easy to write by accident.
| Stream | Type | Role |
|---|---|---|
cout | std::ostream | Prints data to console |
cerr | std::ostream | Prints error messages to console. Not buffered |
clog | std::ostream | Prints log messages to console. Buffered |
cin | std::istream | Reads data from console |
getline | function | std::getline(std::cin, name) reads a full line including spaces, using the cin object under the hood |
std::cout << name; → variable → cout object → buffer → displaystd::cin >> first; → data → OS → buffer → cin object → variablestd::getline(std::cin, name); → reads the full line, including spaces, straight from the buffer into the variableWhen you read an integer with cin >> x, the newline character you pressed to submit it stays behind in the buffer. If getline runs right after, it immediately reads that leftover newline instead of waiting for real input, and hands you back an empty string.
std::cin.ignore()std::getline(std::cin >> std::ws, name)std::ws is a stream manipulator that extracts and discards all leading whitespace from the buffer before getline reads anything.ignore() in practice.ignore() only clears one character, so it quietly breaks the moment there's more than a single stray newline in the buffer. std::ws clears everything leading, which is why it's the one to reach for by default.
When a program runs, the OS loads it into memory. The program area holds the instructions. Variables live in a separate data area in RAM. The CPU fetches instructions from the program area, executes them, and returns to the next instruction using a return address stored in its own register.
a = 10 (int) b = 5 (int) c = f_add(a,b) ...
Step by step, this is what actually happens from launch to exit:
0001, 0002, and so on.0001 and fetches the first instruction. It reads it, executes it, then moves on to the next address.a = 10, the value gets stored in a separate section of RAM called variable storage. The CPU reads from and writes to this area as needed.f_add(a, b), the arguments are pushed onto the stack in RAM. The CPU stores a return address in its register so it knows exactly where to come back to once the function finishes.cout buffer first. The buffer flushes to the console when it's full, on endl, or right before input is taken.return 0, the OS clears the RAM it had allocated and the process terminates.#include <iostream> // function declaration must appear before main int sum(int a, int b); int main() { std::cout << "Enter first num: "; // prints: Enter first num: int first; std::cin >> first; // reads integer from keyboard std::cout << "Enter second num: "; // prints: Enter second num: int second; std::cin >> second; // reads integer from keyboard // std::cin.ignore() discards only ONE leftover newline, may fail if more whitespace // better approach below using std::ws int result = sum(first, second); // calls sum(), stores return value std::string name; std::cout << "What is your name: "; // prints: What is your name: std::getline(std::cin >> std::ws, name); // std::ws discards leftover whitespace first std::cout << "Hello : " << name << '\n'; // prints: Hello : Hassan std::cout << "Result: " << result << '\n'; // prints: Result: 15 return 0; // reports success to OS } // function definition can appear after main since we declared it above int sum(int x, int y) { int z = x + y; return z; // returns result back to caller }
C++ is built in three layers, and each one depends on the one before it.
The basic building blocks of C++ itself. It defines the rules, syntax, and behavior of the language, and it's the foundation everything else is written on. Nothing else in C++ can exist without this layer.
if, loopsA collection of ready-to-use components built using C++'s own core features. It handles common tasks so you don't have to deal with low-level details manually every time, which is what makes real-world programming practical and efficient.
| Header | Provides |
|---|---|
<iostream> | cin, cout |
<string> | std::string |
<fstream> | File handling |
<cmath> | Math functions |
A specialized subset of the Standard Library that gives you practical, optimized implementations of common data structures and algorithms, so you don't have to build them from scratch. It's built using templates, which is exactly why every STL component works with any data type without needing to be rewritten per type.
| Part | Role | Examples |
|---|---|---|
| Containers | Data structures that store data | vector, map, set |
| Algorithms | Operations you run on data | sort, find, count |
| Iterators | Generalized pointers used to traverse containers, connecting algorithms to containers | — |
STL is where C++ stops feeling low-level. Everything up to here is the language itself, this is the toolbox built on top of it.
That covers the ground floor: tools, hello world, how input and output actually move through the program, and where C++ as a language ends and its libraries begin. Next up, the data types themselves.