Visual Learning Aids
ASCII diagrams and flowcharts to help visualize C++ concepts.
C++ Program Execution Flow
┌──────────────────┐
│ Source Code │ (.cpp file)
│ main.cpp │
└────────┬─────────┘
│ Compile (g++ main.cpp -o main)
▼
┌──────────────────┐
│ Preprocessor │ Handles #include, #define
│ Expands headers │
└────────┬─────────┘
▼
┌──────────────────┐
│ Compiler │ Converts C++ to assembly
│ Checks syntax │ Reports errors
└────────┬─────────┘
▼
┌──────────────────┐
│ Assembler │ Converts to machine code
│ (.o object file)│
└────────┬─────────┘
▼
┌──────────────────┐
│ Linker │ Links libraries
│ Creates .exe │ Resolves references
└────────┬─────────┘
▼
┌──────────────────┐
│ Executable │ ./main
│ Runs on OS │
└──────────────────┘OOP Relationships
┌─────────────────────────────────────────────┐ │ INHERITANCE (IS-A) │ │ │ │ ┌──────────┐ │ │ │ Animal │ ◄── Base Class │ │ │──────────│ │ │ │ name │ │ │ │ age │ │ │ │ speak() │ ◄── virtual │ │ └────┬─────┘ │ │ ┌────┴─────┐ │ │ │ │ │ │ ┌────▼───┐ ┌────▼───┐ │ │ │ Dog │ │ Cat │ ◄── Derived │ │ │────────│ │────────│ │ │ │ breed │ │ indoor │ │ │ │speak() │ │speak() │ ◄── Override │ │ │"Woof!" │ │"Meow!" │ │ │ └────────┘ └────────┘ │ └─────────────────────────────────────────────┘ ┌─────────────────────────────────────────────┐ │ COMPOSITION (HAS-A) │ │ │ │ ┌──────────┐ contains ┌─────────┐ │ │ │ Car │───────────────►│ Engine │ │ │ │──────────│ 1 1 │─────────│ │ │ │ brand │ │ hp │ │ │ │ model │ │ start() │ │ │ │ drive() │ │ stop() │ │ │ └──────────┘ └─────────┘ │ │ │ │ │ │ contains 4 │ │ ▼ │ │ ┌─────────┐ │ │ │ Wheel │ │ │ │─────────│ │ │ │ size │ │ │ │ rotate()│ │ │ └─────────┘ │ └─────────────────────────────────────────────┘
Memory Layout (Stack vs Heap)
┌─────────────────────────────────────┐ │ MEMORY LAYOUT │ ├─────────────────────────────────────┤ │ │ │ ┌─────────────────────────────┐ │ │ │ STACK (automatic) │ │ │ │ ┌───────────────────────┐ │ │ │ │ │ int x = 10; │ │ │ ← Local variables │ │ │ double y = 3.14; │ │ │ ← Function parameters │ │ │ char c = 'A'; │ │ │ ← Return addresses │ │ └───────────────────────┘ │ │ │ │ Grows ▼ downward │ │ │ └─────────────────────────────┘ │ │ │ │ ┌─────────────────────────────┐ │ │ │ HEAP (dynamic) │ │ │ │ ┌───────────────────────┐ │ │ │ │ │ int* p = new int(42); │ │ │ ← new / delete │ │ │ int* arr = new int[5];│ │ │ ← Dynamic arrays │ │ │ // Must call delete! │ │ │ ← Manual management │ │ └───────────────────────┘ │ │ │ │ Grows ▲ upward │ │ │ └─────────────────────────────┘ │ │ │ │ ┌─────────────────────────────┐ │ │ │ GLOBAL / STATIC │ │ │ │ int globalVar = 100; │ │ ← Whole program lifetime │ │ static int count = 0; │ │ │ └─────────────────────────────┘ │ │ │ │ ┌─────────────────────────────┐ │ │ │ CODE (Text Segment) │ │ │ │ main(), functions, etc. │ │ ← Read-only instructions │ └─────────────────────────────┘ │ └─────────────────────────────────────┘
Control Flow Decision Tree
┌─────────┐
│ START │
└────┬────┘
│
┌────▼────┐
┌────│ if (x>0) │────┐
│ └─────────┘ │
TRUE FALSE
│ │
┌────▼────┐ ┌────▼────────┐
│ "Pos." │ ┌────│ if (x==0) │────┐
└────┬────┘ │ └─────────────┘ │
│ TRUE FALSE
│ │ │
│ ┌────▼────┐ ┌────▼────┐
│ │ "Zero" │ │ "Neg." │
│ └────┬────┘ └────┬────┘
│ │ │
└────────┼──────────────────────┘
│
┌────▼────┐
│ END │
└─────────┘
┌─────────────────────────┐
│ FOR LOOP FLOW │
│ │
│ for(i=0; i<5; i++) │
│ │
│ INIT ──→ CHECK ──→ NO ──→ EXIT
│ │ │
│ YES │
│ │ │
│ BODY │
│ │ │
│ UPDATE ───────────┘
│ (i++)
└─────────────────────────┘File Handling Flow
┌──────────────────────────────────────────┐
│ FILE WRITING (ofstream) │
│ │
│ ┌──────────┐ ┌──────────────────┐ │
│ │ Program │───►│ ofstream file │ │
│ │ Data │ │ ("data.txt") │ │
│ └──────────┘ └────────┬─────────┘ │
│ │ │
│ ┌────────▼─────────┐ │
│ │ file << data; │ │
│ │ file << endl; │ │
│ └────────┬─────────┘ │
│ │ │
│ ┌────────▼─────────┐ │
│ │ file.close(); │ │
│ └────────┬─────────┘ │
│ │ │
│ ┌────────▼─────────┐ │
│ │ data.txt 📄 │ │
│ │ (on disk) │ │
│ └──────────────────┘ │
└──────────────────────────────────────────┘
┌──────────────────────────────────────────┐
│ FILE READING (ifstream) │
│ │
│ ┌──────────────────┐ ┌──────────┐ │
│ │ ifstream file │───►│ Program │ │
│ │ ("data.txt") │ │ Variable │ │
│ └────────┬─────────┘ └──────────┘ │
│ │ │
│ ┌────────▼─────────┐ │
│ │ while(getline │ │
│ │ (file, line)) │ │
│ │ { process line } │ │
│ └────────┬─────────┘ │
│ │ │
│ ┌────────▼─────────┐ │
│ │ file.close(); │ │
│ └──────────────────┘ │
└──────────────────────────────────────────┘Polymorphism in Action
┌────────────────────────────────────────────┐ │ POLYMORPHISM — "One Interface, │ │ Many Implementations" │ │ │ │ Base* ptr; // Base class pointer │ │ │ │ ┌──────────────────┐ │ │ │ Shape (abstract) │ │ │ │ ─────────────── │ │ │ │ + area() = 0 │ ◄── Pure Virtual │ │ │ + display() │ │ │ └────────┬─────────┘ │ │ ┌────┼────────┐ │ │ │ │ │ │ │ ┌────▼──┐ ┌──▼───┐ ┌──▼──────┐ │ │ │Circle │ │Rect. │ │Triangle │ │ │ │───────│ │──────│ │────────│ │ │ │area() │ │area()│ │area() │ │ │ │=πr² │ │=w*h │ │=½b*h │ │ │ └───────┘ └──────┘ └────────┘ │ │ │ │ Shape* s = new Circle(5); │ │ s->area(); // Calls Circle::area() │ │ │ │ s = new Rectangle(4, 6); │ │ s->area(); // Calls Rectangle::area() │ │ │ │ SAME call, DIFFERENT behavior! ✓ │ └────────────────────────────────────────────┘