cs.thefarshad
intro

Getting Started with C++

Compile your first program, and meet the value semantics, references, and pointers that define C++.

C++ is a compiled, statically typed language that gives you control over memory and performance while still offering high-level abstractions. It is the standard choice for systems programming, game engines, high-frequency trading, and competitive programming — anywhere speed and predictability matter.

The single idea that separates C++ from most languages is how it treats names and memory. Step through the model below: watch which boxes change when you copy a value, alias it with a reference, or point at it.

int a = 10;
int b = a;
int& r = a;
int* p = &a;
r = 20;
*p = 30;
avalue
@ 0x7ffe10
bvalue
@ 0x7ffe14
rreference (alias)
@ 0x7ffe10
ppointer
@ 0x7ffe18
r → a (same box)p stores 0x7ffe10 → *p reaches ab is a copy (independent)
1/7
Four named slots in this stack frame, each at its own address. Nothing is alive yet.

Hello, world

#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
    return 0;
}

Compile and run it with a modern compiler:

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

The -std=c++20 flag selects the language standard and -O2 turns on optimizations.

Values, references, and pointers

This is the idea that trips up newcomers, and it is the heart of C++:

  • A value is a copy. int b = a; gives b its own independent copy of a.
  • A reference (int& r = a;) is another name for an existing object. Changing r changes a.
  • A pointer (int* p = &a;) stores the address of an object. You follow it with *p and it can be reseated or null.
int a = 10;
int& r = a;     // r is an alias for a
int* p = &a;    // p holds the address of a
r = 20;         // a is now 20
*p = 30;        // a is now 30

Understanding when data is copied versus shared is what makes C++ fast — and what makes dangling references and leaks possible if you are careless. Later lessons cover RAII (tying resource lifetime to scope), smart pointers, templates, and the STL containers and algorithms.

Why C++ for this site

Many data-structure and algorithm problems are written in C++ because it maps closely to what the machine does, so the cost model in those lessons is concrete. Running C++ against test cases in the browser arrives with the problem judge in a later phase; for now these lessons focus on the language and its mental model.

Takeaways

  • C++ is compiled and statically typed, trading some convenience for speed and control.
  • Value semantics by default; references alias, pointers hold addresses.
  • Knowing what gets copied versus shared is the foundation everything else builds on.