Last 30 Days
No notifications
C++ is C with superpowers: classes, templates, the Standard Template Library (STL), and decades of polish. It's the go-to language for:
#include <iostream>
using namespace std;int main() {
cout << "Hello, C++!" << endl;
return 0;
}
| You want | C++ gives you |
| Speed | Native machine code, no garbage collector |
| Power | Pointers, manual memory, SIMD, inline assembly |
| Abstractions | Classes, templates, RAII, smart pointers |
| Library | The STL โ vectors, maps, sorting, in 1 line |
| Portability | Runs on every OS, every CPU |
C++ is a superset of C โ almost any C program also compiles as C++. But C++ adds:
std::string, std::vector, std::map (no manual malloc/free)&) โ safer than pointersg++ -std=c++20 -O2 hello.cpp -o hello
./helloThat's it. You're now a C++ programmer.
C++ was created by Bjarne Stroustrup at Bell Labs in 1979 as "C with Classes". Today (C++23) it's one of the most powerful โ and complex โ languages in mainstream use.
> Good news: you don't need to learn ALL of C++. The 30% you'll use covers 95% of real work.
#include <iostream> // bring in iostream library
using namespace std; // skip "std::" prefixint main() { // entry point
cout << "Hello, C++!\n"; // print
return 0; // success
}
| Line | Meaning |
#include | Pulls in I/O streams (cin, cout) |
using namespace std; | Lets you write cout instead of std::cout |
int main() | Where the program starts |
cout << "..." | Send text to standard output |
return 0 | 0 = success; non-zero = failure |
> About using namespace std; โ convenient for learning and competitive programming, but in real codebases prefer the explicit std::cout, std::vector, etc. (avoids name clashes in big projects).
1. Preprocessor โ handles #include, #define, conditional compilation. (Same as C!)
2. Compiler โ turns each .cpp into machine code (.o object file). May take a while because of templates.
3. Linker โ stitches object files + libraries into one executable.
4. Run โ the OS loads it, your main() runs.
g++) or MSVC (Visual Studio Build Tools).xcode-select --install โ gives you clang++.sudo apt install g++ (or your distro's equivalent).| Layer | What lives here |
| Standard Library | iostream, string, vector, algorithm, ... |
| Core C++ | classes, templates, lambdas, exceptions |
| C compatibility | int, char, pointers, malloc/free, printf |
You'll spend most of your time at the top two layers. Drop down only when performance demands.
free.#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {
vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6};
sort(nums.begin(), nums.end());
for (int x : nums) cout << x << ' ';
cout << '\n';
}
In ~10 lines you used: dynamic array, STL initialiser list, the sort algorithm, and a range-based for loop. Welcome to the productivity of C++.
| Phase | Topics |
| 1 | Syntax, types, references, I/O, control flow |
| 2 | Functions, lambdas, strings, vector, classes |
| 3 | Inheritance, smart pointers, templates, STL containers |
| 4 | STL algorithms, iterators, modern C++, practice |
By the end you'll be able to read most C++ code in the wild and write idiomatic, modern C++ yourself.
.h, definitions in .cpp. (Templates break this rule.)endl flushes the buffer (slower); "\n" doesn't. In tight loops use "\n".| Want | Use |
cout << x; | |
| Read | cin >> x; |
| Newline | "\n" |
| Comment | // line or /* block */ |
| Compile | g++ -std=c++20 -O2 file.cpp -o file |
| Run | ./file |
You're set. Let's write your first real C++ program.