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
- cout: Printing in C++: Print with cout and <<, end lines with endl, and show true/false with boolalpha.
- References: A reference is another name for an existing variable. Pass by reference to let a function change the caller's variable without pointers.
- std::string: C++ strings grow by themselves and know their length. Join, measure, slice, search, change and compare them.
- Function Overloading and Default Arguments: Several functions can share a name if their parameters differ, and parameters can have default values.
- Namespaces: Namespaces keep names apart: two functions called distance can live side by side.
Classes and Objects
- Classes and Objects: Define a class, create objects on the stack, call methods, and see that assigning an object copies it.
- Constructors, Destructors and RAII: Constructors set objects up; destructors clean up automatically when they go out of scope. This is RAII.
- Encapsulation: private and public: Keep data private and change it only through methods that check the rules.
- this and static Members: this points to the object a method runs on; static members are shared by every object of the class.
- Operator Overloading: Teach your own types to use +, == and << so they read like built-in types.
Inheritance and Polymorphism
- Inheritance: A Bike is a Vehicle. The base class part is built first, and the child inherits its methods.
- virtual Functions and Polymorphism: Through a base-class reference, a virtual call runs the derived version; a non-virtual call does not.
- 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
- new and delete: Create values and objects on the heap with new, and release them with delete or delete[].
- Smart Pointers: unique_ptr and shared_ptr: Smart pointers delete heap objects for you. unique_ptr has one owner; shared_ptr counts its owners.
- const Correctness: Mark what must not change: const variables, const references for parameters, and pointers to const.
- Move Semantics: Copying a string duplicates its text; moving it just hands the text over and leaves the source empty.
The Standard Library
- std::vector: A growable array: push_back, index, pop_back and loop with a range-based for.
- std::map: Key-value lookups with keys kept in sorted order, and loops with structured bindings.
- STL Algorithms and Lambdas: sort, count_if and find_if from <algorithm>, driven by small lambdas that can capture variables.
- Templates: Write one function or class that works for any type; the compiler makes a version per type used.
- Exceptions: throw, try, catch: Throw an exception for bad input and catch it, skipping the rest of the try block.