A comprehensive collection of C++ exercises covering fundamental to advanced concepts. This progression builds from basic syntax to advanced template usage, object-oriented design patterns, and algorithmic problem-solving.
10 modules (CPP00–CPP09) with multiple exercises per module:
- CPP00–CPP04: Core OOP concepts (classes, inheritance, polymorphism)
- CPP05: Exception handling and form validation
- CPP06: Type casting and serialization
- CPP07: Templates (generic functions and classes)
- CPP08: STL containers and algorithms
- CPP09: Advanced algorithms and data structures
CPP/
├── CPP00/ # Basics (classes, constructors, getters/setters)
├── CPP01/ # Memory management (pointers, heap allocation)
├── CPP02/ # Operator overloading (Fixed-point numbers)
├── CPP03/ # Inheritance (multiple inheritance, diamond problem)
├── CPP04/ # Polymorphism (virtual functions, abstract classes)
├── CPP05/ # Exception handling (try-catch, custom exceptions)
├── CPP06/ # Type casting (static_cast, dynamic_cast, reinterpret_cast)
├── CPP07/ # Templates (function templates, class templates)
├── CPP08/ # STL containers and algorithms
└── CPP09/ # Advanced algorithms (Bitcoin exchange, RPN, merge sort)
- ex00:
megaphone— converts input to uppercase - ex01:
phonebook— simple contact management (max 8 contacts) - ex02:
Account— bank account simulation with static logging
cd CPP00/ex00 && make && ./megaphone "hello world"
cd CPP00/ex01 && make && ./phonebook
cd CPP00/ex02 && make && ./account- Classes and object construction
- Getters/setters and encapsulation
- Static members and methods
- Basic file I/O and string handling
- ex00:
Zombie— stack vs. heap allocation - ex01:
zombieHorde— array allocation on heap - ex02:
fileReplace— read file, replace string, write output - ex03:
Weapon— pointer references between objects - ex04:
sed— file search/replace (likesed) - ex05:
Harl— function pointers for logging levels - ex06:
Harl Filter— switch-case on string (filter log level)
cd CPP01/ex00 && make && ./zombie
cd CPP01/ex02 && make && ./sed input_file.txt "search" "replace"
cd CPP01/ex05 && make && ./harl- Stack vs. heap allocation
- Pointer arithmetic and references
- File I/O streams
- Function pointers
Implements a Fixed-point number class with custom operators.
- ex00: Basic Fixed class (constructor, copy, assignment)
- ex01: Overload arithmetic operators (
+,-,*,/) - ex02: Overload comparison operators (
<,>,==, etc.) and increment - ex03:
bsp()— point-in-triangle test using cross products
cd CPP02/ex02 && make && ./fixed
cd CPP02/ex03 && make && ./bsp- Copy constructor and assignment operator
- Operator overloading (arithmetic, comparison, pre/post-increment)
- const correctness
- Insertion operator (
<<)
- ex00: Basic inheritance (ClapTrap)
- ex01: Multiple levels (ScavTrap inherits ClapTrap)
- ex02: Multiple inheritance (FragTrap inherits ClapTrap)
- ex03: Diamond problem (DiamondTrap from ScavTrap + FragTrap)
cd CPP03/ex00 && make && ./clapTrap
cd CPP03/ex03 && make && ./diamondTrap- Single and multiple inheritance
- Constructor/destructor chaining
- Diamond problem and virtual inheritance
- Protected vs. private members
- ex00:
Animalhierarchy (Animal → Dog/Cat, WrongAnimal for comparison) - ex01:
Braindeep copy (fix resource leaks) - ex02:
Brainwith array of ideas (100 ideas per animal) - ex03:
Materiasystem — abstract base, Ice/Cure implementations, inventory
cd CPP04/ex00 && make && ./animal
cd CPP04/ex03 && make && ./materia- Virtual functions and pure virtual (abstract) classes
- Polymorphic behavior via pointers/references
- Deep copy semantics
- Factory pattern (MateriaSource)
- ex00:
Bureaucratwith grades (1–150), throw exceptions on invalid grade - ex01:
Formclass with sign conditions,Bureaucratsigns forms - ex02:
AForm(abstract) withPresidentialPardonForm,RobotomyRequestForm,ShrubberyCreationForm - ex03:
Interncreates forms from name string (factory pattern)
cd CPP05/ex00 && make && ./bureaucrat
cd CPP05/ex03 && make && ./intern- Custom exceptions (inherit from
std::exception) - Try-catch blocks
- Exception safety and re-throw
- Const correctness in exception handling
- ex00:
ScalarConverter— parse scalar string, detect type (int/float/double/char), convert & print - ex01:
Serializer— serialize/deserialize pointer toDatastruct (reinterpret_cast) - ex02:
Base— identify object type via RTTI (dynamic_cast)
cd CPP06/ex00 && make && ./convert "42"
cd CPP06/ex01 && make && ./serialize
cd CPP06/ex02 && make && ./identifystatic_cast— safe conversions (e.g., int to float)reinterpret_cast— low-level bit reinterpretation (pointers)dynamic_cast— RTTI for polymorphic types- Type detection via
typeid()
Generic programming with templates.
- ex00:
template<T> void swap(T &a, T &b)— generic swap function - ex01:
template<T> T iter(...)— iterate and apply function to array - ex02:
template<typename T> class Array— generic dynamic array
cd CPP07/ex00 && make && ./whatever
cd CPP07/ex01 && make && ./iter
cd CPP07/ex02 && make && ./array- Function templates with deduction
- Class templates and member function definitions
- Template specialization
- Non-type template parameters
- ex00:
easyfind()— template function to find element in container via STLfind() - ex01:
Span— container storing up to N integers,addNumber(),shortestSpan(),longestSpan() - ex02:
MutantStack— stack with iterator support (template + inheritance)
cd CPP08/ex00 && make && ./easyfind
cd CPP08/ex01 && make && ./span
cd CPP08/ex02 && make && ./mutantstack- STL containers (vector, list, stack, map)
- STL algorithms (find, sort, transform)
- Iterators (forward, bidirectional, random-access)
- Adapter patterns (stack wrapping deque)
Parse CSV historical Bitcoin prices, read input file with dates/amounts, output exchange values.
Files:
data.csv— historical Bitcoin prices (date, price)input— list of dates and amountsmain.cpp— parse and compute
Build & Run:
cd CPP09/ex00 && make && ./btc inputOutput:
2011-01-03 => 1.2 = 11.6
2011-01-03 => 1 = 9.67
2012-01-11 => 7.1 = 4.9971
...
Key Concepts:
- File parsing and CSV handling
- Map (key-value) data structure
- Binary search (
lower_bound()) - Date validation
Evaluate mathematical expressions in RPN using a stack.
Example:
Input: "5 1 2 + 4 * + 3 -"
Output: 32
# (5 + (1 + 2) * 4) - 3 = 32
Build & Run:
cd CPP09/ex01 && make && ./RPN "5 1 2 + 4 * + 3 -"Key Concepts:
- Stack-based computation
- Postfix notation evaluation
- String tokenization
- Operator handling
Merge-insert sort (Ford-Johnson algorithm) using two different containers (vector + deque).
Build & Run:
cd CPP09/ex02 && make && ./PmergeMe 5 2 8 1 9Output:
Before: 5 2 8 1 9
After: 1 2 5 8 9
Time to process a range of 5 elements with std::vector: 0.001 ms
Time to process a range of 5 elements with std::deque: 0.001 ms
Key Concepts:
- Merge-insert sort algorithm
- Container comparison (vector vs. deque performance)
- High-order element tracking
- Timing measurements (
gettimeofday())
Each exercise has its own Makefile. General build:
cd CPP<XX>/ex<YY>
make # compile
make run # compile and run (if main exists)
make clean # remove .o files
make fclean # remove executable and .o
make re # clean + compileStandard compilation with:
-Wall -Wextra -Werror— strict warnings-std=c++98or-std=c++11(project-specific)-O2or-O3(optimization)
Example Makefile snippet:
CXX = c++
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
LDFLAGS =
SRC = $(wildcard *.cpp)
OBJ = $(SRC:.cpp=.o)
TARGET = program_name
all: $(TARGET)
$(TARGET): $(OBJ)
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
rm -f $(OBJ)
fclean: clean
rm -f $(TARGET)
re: fclean all
.PHONY: all clean fclean re| Module | Focus | Difficulty |
|---|---|---|
| CPP00 | Classes & basics | ⭐ |
| CPP01 | Pointers & memory | ⭐⭐ |
| CPP02 | Operator overloading | ⭐⭐ |
| CPP03 | Inheritance | ⭐⭐ |
| CPP04 | Polymorphism | ⭐⭐⭐ |
| CPP05 | Exceptions | ⭐⭐ |
| CPP06 | Type casting | ⭐⭐ |
| CPP07 | Templates | ⭐⭐⭐ |
| CPP08 | STL & algorithms | ⭐⭐⭐⭐ |
| CPP09 | Advanced algorithms | ⭐⭐⭐⭐ |
- OOP fundamentals: classes, inheritance, polymorphism
- Memory safety: pointers, references, RAII, smart pointers (C++11)
- Operator overloading: custom behavior for built-in operators
- Exception safety: try-catch, custom exceptions
- Generic programming: function & class templates
- STL mastery: containers, iterators, algorithms
- Algorithmic thinking: sorting, searching, optimization