Notifications

No notifications

/Phase 1

Introduction to C++

Welcome to C++ โž•

C++ is C with superpowers: classes, templates, the Standard Template Library (STL), and decades of polish. It's the go-to language for:

  • ๐Ÿ† Competitive programming (Codeforces, AtCoder, ICPC)
  • ๐ŸŽฎ Game engines (Unreal, Unity's runtime, CryEngine)
  • ๐ŸŒ Browsers (Chrome, Firefox, Edge)
  • ๐Ÿ’ฐ High-frequency trading (latency in microseconds)
  • ๐Ÿค– Robotics, embedded, aerospace
#include <iostream>
using namespace std;

int main() { cout << "Hello, C++!" << endl; return 0; }

Why Learn C++?

You wantC++ gives you
SpeedNative machine code, no garbage collector
PowerPointers, manual memory, SIMD, inline assembly
AbstractionsClasses, templates, RAII, smart pointers
LibraryThe STL โ€” vectors, maps, sorting, in 1 line
PortabilityRuns on every OS, every CPU

C++ vs C

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)
  • Classes & objects (OOP)
  • References (&) โ€” safer than pointers
  • Templates (generic code)
  • Exceptions
  • Lambdas, auto, range-for

Compile & Run

g++ -std=c++20 -O2 hello.cpp -o hello
./hello

That's it. You're now a C++ programmer.

On this page

Detailed Theory

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.

Hello World โ€” Dissected

#include <iostream>            // bring in iostream library
using namespace std;            // skip "std::" prefix

int main() { // entry point cout << "Hello, C++!\n"; // print return 0; // success }

LineMeaning
#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 00 = 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).

How a C++ Program Becomes Something You Run

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.

Setting Up

Windows

  • Install MinGW-w64 (g++) or MSVC (Visual Studio Build Tools).
  • VS Code + the C/C++ extension is the easy path.

macOS

  • xcode-select --install โ€” gives you clang++.

Linux

  • sudo apt install g++ (or your distro's equivalent).

Online

  • Compiler Explorer (godbolt.org) โ€” see compiled assembly.
  • Replit, OnlineGDB, Codeforces Custom Test โ€” for quick play.

The Big Mental Model

LayerWhat lives here
Standard Libraryiostream, string, vector, algorithm, ...
Core C++classes, templates, lambdas, exceptions
C compatibilityint, char, pointers, malloc/free, printf

You'll spend most of your time at the top two layers. Drop down only when performance demands.

C++ Style Pillars

  • RAII โ€” *Resource Acquisition Is Initialisation*. Objects clean themselves up when they go out of scope. No manual free.
  • Value semantics โ€” by default, assigning copies. Cheap thanks to move semantics.
  • Templates over duck typing โ€” generic code is checked at compile time.
  • Exceptions for errors, return values for normal flow.

A Tiny Tour

#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++.

What You'll Learn in This Track

PhaseTopics
1Syntax, types, references, I/O, control flow
2Functions, lambdas, strings, vector, classes
3Inheritance, smart pointers, templates, STL containers
4STL 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.

Common Beginner Surprises

  • Compile errors are LONG. A missing semicolon in a template can produce 50 lines. Read the FIRST error.
  • Header-vs-source split โ€” declarations in .h, definitions in .cpp. (Templates break this rule.)
  • endl flushes the buffer (slower); "\n" doesn't. In tight loops use "\n".
  • Undefined behaviour exists โ€” same as C. Out-of-bounds indexing, uninitialised reads, signed overflowโ€ฆ don't.

Cheat-Sheet

WantUse
Printcout << x;
Readcin >> x;
Newline"\n"
Comment// line or /* block */
Compileg++ -std=c++20 -O2 file.cpp -o file
Run./file

You're set. Let's write your first real C++ program.