C++: classes, the STL and safe memory
You design classes, use the STL containers and algorithms, and write C++ that avoids memory errors through smart pointers.
Level 201 · 19 lessons · 15 practice labs
Lessons
Classes
- Classes, constructors and destructors: You write classes, start objects in a valid state with constructors and see when destructors run.
- Encapsulation and const member functions: You expose private fields through controlled functions and mark functions that do not change the object as const.
- static members and friend
- Operator overloading: You define operators such as ==, << and + for your own class in a meaningful, expected way.
- Inheritance, virtual functions and polymorphism: You write derived classes, get the right function chosen at run time with virtual and never forget the virtual destructor.
- Abstract classes and interfaces: You define the contract subclasses must follow using pure virtual functions.
- Short task: a shape hierarchy that computes area and perimeter
Memory management
- Pointers, new and delete: You see how manual allocation and release work and why they invite leaks.
- RAII: tying resources to object lifetime: You design classes that release memory, files and locks automatically through the object’s destructor.
- unique_ptr, shared_ptr and weak_ptr: You state ownership explicitly with smart pointers and write leak-free code without manual delete.
- Copying, moving and the rule of five: You choose a class’s copy and move behaviour deliberately and protect against errors such as double deletion.
- Finding memory errors: AddressSanitizer and Valgrind: You enable -fsanitize=address in g++ to detect leaks and invalid access, and read the report.
- Short task: move a raw-pointer class to smart pointers
The STL and modern C++
- STL containers: vector, map, set, unordered_map: You pick the container that suits the data and perform insertion, lookup and removal.
- Iterators and algorithms: sort, find, count_if: You use ready-made algorithms instead of hand-written loops and know when an iterator becomes invalid.
- Lambda expressions and an introduction to templates: You write lambdas that give algorithms custom criteria and define a simple type-independent template function.
- string_view and span: copy-free, bounded views: You pass data to functions without copying it and watch the lifetime of memory that a view does not own.
- Short task: a word frequency table with the STL
Capstone
- Capstone: a leak-free, bounds-checked inventory program: You write an inventory program with classes, smart pointers and the STL and confirm it runs clean under the sanitizer.
Practice labs
- Adding const member functions to a temperature class: Add const member functions for unit conversion to a temperature class and fix the usage that fails to compile on a const object.
- Defining operators for a duration class: Add +, ==, < and << operators to a duration class holding minutes and seconds and test that overflowing seconds carry into minutes.
- Finding the inconsistency in an object counter: Fix a static object counter that miscounts on copy and move by handling every special member function and pass the lifecycle tests.
- Separating notification types with a virtual function: Derive e-mail and SMS notifications from a common base, override the send function and call it through a base pointer.
- Writing an abstract interface for storage: Define a storage interface with pure virtual functions and show that the in-memory and file-based implementations pass the same tests.
- Closing the leak of a missing virtual destructor: Show with ASan the leak in a hierarchy whose derived objects are deleted through a base pointer, add the virtual destructor and also remove the slicing bug.
- Making a factory return unique_ptr: Change a factory that returns a raw pointer the caller forgets to delete so that it uses std::make_unique.
- Wrapping a C file handle in RAII: Write a wrapper class that closes an fopen handle on every exit path and is movable but not copyable.
- Breaking a shared_ptr cycle with weak_ptr: Find the destructors that never run in a tree whose parent and child nodes hold each other by shared_ptr, break the cycle with weak_ptr and pass the leak test.
- Removing hand-written special functions: Use std::vector instead of a raw array to make a class's hand-written copy functions and destructor unnecessary and show the tests still pass.
- Applying the rule of five to a buffer class: Add copy and move functions to a buffer class that has only a destructor and double-frees when copied, handling self-assignment too.
- Solving a use-after-free from an ASan report: Find the code touching a deleted node of a task list from the stack traces in the AddressSanitizer report, repair it and show Valgrind is clean too.
- Grouping words by length: Group the words of a text by length with std::map and return an alphabetically sorted list for each length.
- Conditional counting with a lambda: Use count_if and sort with a lambda capturing a threshold to count under-stocked products and order them by price.
- Repairing dangling string_view tokens: Fix a tokenizer that yields garbage because it returns string_view into a temporary, following lifetime rules, and write a bounds-checked version with span.
About this course
You design classes, use the STL containers and algorithms, and write C++ that avoids memory errors through smart pointers.
What you will learn
- Classes
- Memory management
- The STL and modern C++
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.