C++ Programming: Objects, RAII and the STL

Modern C++ traced line by line: references, classes, constructors and destructors, inheritance and virtual functions, new and smart pointers, vector and map, templates, exceptions and move semantics. Every lesson is recorded from a real compiled run.

C++ Basics

  1. cout: Printing in C++: Print with cout and <<, end lines with endl, and show true/false with boolalpha.
  2. References: A reference is another name for an existing variable. Pass by reference to let a function change the caller's variable without pointers.
  3. std::string: C++ strings grow by themselves and know their length. Join, measure, slice, search, change and compare them.
  4. Function Overloading and Default Arguments: Several functions can share a name if their parameters differ, and parameters can have default values.
  5. Namespaces: Namespaces keep names apart: two functions called distance can live side by side.

Classes and Objects

  1. Classes and Objects: Define a class, create objects on the stack, call methods, and see that assigning an object copies it.
  2. Constructors, Destructors and RAII: Constructors set objects up; destructors clean up automatically when they go out of scope. This is RAII.
  3. Encapsulation: private and public: Keep data private and change it only through methods that check the rules.
  4. this and static Members: this points to the object a method runs on; static members are shared by every object of the class.
  5. Operator Overloading: Teach your own types to use +, == and << so they read like built-in types.

Inheritance and Polymorphism

  1. Inheritance: A Bike is a Vehicle. The base class part is built first, and the child inherits its methods.
  2. virtual Functions and Polymorphism: Through a base-class reference, a virtual call runs the derived version; a non-virtual call does not.
  3. Abstract Classes and Pure Virtual Functions: A pure virtual function (= 0) makes a class abstract. Children fill it in; shared code calls it.

Memory and Ownership

  1. new and delete: Create values and objects on the heap with new, and release them with delete or delete[].
  2. Smart Pointers: unique_ptr and shared_ptr: Smart pointers delete heap objects for you. unique_ptr has one owner; shared_ptr counts its owners.
  3. const Correctness: Mark what must not change: const variables, const references for parameters, and pointers to const.
  4. Move Semantics: Copying a string duplicates its text; moving it just hands the text over and leaves the source empty.

The Standard Library

  1. std::vector: A growable array: push_back, index, pop_back and loop with a range-based for.
  2. std::map: Key-value lookups with keys kept in sorted order, and loops with structured bindings.
  3. STL Algorithms and Lambdas: sort, count_if and find_if from <algorithm>, driven by small lambdas that can capture variables.
  4. Templates: Write one function or class that works for any type; the compiler makes a version per type used.
  5. Exceptions: throw, try, catch: Throw an exception for bad input and catch it, skipping the rest of the try block.