✅ Status: Completed – all exercises
🏫 School: 42 – C++ Modules (Module 05)
🏅 Score: (100)/100
Bureaucrats, forms, abstract interfaces, and a lot of exceptions. Repetition, validation, and clean error handling.
This repository contains my solutions to 42’s C++ Module 05 (C++98). The main focus is exceptions + repetition of OOP fundamentals through a small bureaucratic system:
- A
Bureaucratwith a strict grade range (1is highest,150is lowest) - Forms that require specific grades to sign and execute
- An abstract base class (
AForm) with concrete derived forms - An
Internthat creates forms by name (factory-like behavior)
All exercises are written in C++98 and compiled with strict flags.
Concepts practiced in this module include:
- Designing classes in Orthodox Canonical Form (constructors, copy, assignment, destructor)
- Writing and using custom exceptions (
GradeTooHighException,GradeTooLowException, etc.) - Defensive checks + clear failure behavior via
throw/try/catch - Encapsulation (private attributes, getters)
- Abstract classes and polymorphism (
AForm) - Separating “validation” logic from “action” logic in execution flows
- Avoiding messy if/else chains by using cleaner patterns in
Intern::makeForm()
Goal: Implement a Bureaucrat class with:
-
const std::string name -
int gradein range [1..150] -
Grade changes:
incrementGrade()(e.g., 3 → 2)decrementGrade()(e.g., 3 → 4)
-
Exceptions on invalid grade:
GradeTooHighExceptionGradeTooLowException
-
operator<<output format:<name>, bureaucrat grade <grade>.
Goal: Add a Form class containing:
const std::string namebool isSigned(initiallyfalse)const int gradeToSignconst int gradeToExecute
Key behavior:
-
Form::beSigned(Bureaucrat const&)signs if grade is high enough -
Bureaucrat::signForm(Form&)prints success/failure message with reason -
Exceptions:
Form::GradeTooHighExceptionForm::GradeTooLowException
Goal: Turn Form into an abstract base class AForm and implement 3 concrete forms.
Concrete forms:
-
ShrubberyCreationFormRequired grades: sign 145, exec 137 Creates<target>_shrubberyand writes ASCII trees inside. -
RobotomyRequestFormRequired grades: sign 72, exec 45 “Drilling noises” + 50% success robotomy. -
PresidentialPardonFormRequired grades: sign 25, exec 5 Announces pardon “by Zaphod Beeblebrox”.
Also required:
-
AForm::execute(Bureaucrat const& executor) constMust check:- form is signed
- executor grade is high enough Otherwise throw appropriate exceptions.
-
Bureaucrat::executeForm(AForm const&)prints success or explicit error.
Goal: Implement an Intern with:
-
AForm* makeForm(std::string formName, std::string target) -
Creates and returns the correct form based on
formName -
Prints:
Intern creates <form>
-
If unknown form name:
- print explicit error message
-
No ugly giant if/else/else-if chain (clean mapping approach expected)
-
Compiler:
c++ -
Flags:
-Wall -Wextra -Werror(+ must still compile with-std=c++98) -
OS: Unix-like (Linux/macOS)
-
No external libraries (no C++11, no Boost)
-
Forbidden functions:
printf(),malloc(),free()(and similar)
-
STL containers/algorithms are not allowed until later modules (08/09)
Clone the repository and build each exercise separately:
git clone <this-repo-url>
cd cpp-module-05cd ex00
make
./bureaucratcd ex01
make
./bureaucracycd ex02
make
./formscd ex03
make
./internExecutable names may differ depending on implementation — check each
Makefile.
cpp-module-05/
├── ex00/
│ ├── Makefile
│ ├── main.cpp
│ ├── Bureaucrat.hpp
│ └── Bureaucrat.cpp
│
├── ex01/
│ ├── Makefile
│ ├── main.cpp
│ ├── Bureaucrat.hpp / Bureaucrat.cpp
│ ├── Form.hpp
│ └── Form.cpp
│
├── ex02/
│ ├── Makefile
│ ├── main.cpp
│ ├── Bureaucrat.hpp / Bureaucrat.cpp
│ ├── AForm.hpp / AForm.cpp
│ ├── ShrubberyCreationForm.hpp / .cpp
│ ├── RobotomyRequestForm.hpp / .cpp
│ └── PresidentialPardonForm.hpp / .cpp
│
└── ex03/
├── Makefile
├── main.cpp
├── Intern.hpp
├── Intern.cpp
└── (all previous classes reused)
-
Grades
- Try constructing bureaucrats/forms with grades
0,151, etc. → must throw. - Test boundary values:
1and150.
- Try constructing bureaucrats/forms with grades
-
Signing
- A bureaucrat with insufficient grade must fail to sign and report why.
- A sufficiently ranked bureaucrat must sign successfully.
-
Execution
- Executing an unsigned form must throw.
- Executing with a too-low grade must throw.
- Successful executions should print the expected messages and side effects.
-
Robotomy
- Run multiple times to confirm ~50% success behavior.
-
Intern
- Try valid names (e.g.
"robotomy request") and invalid names → must show explicit error.
- Try valid names (e.g.
- C++ modules don’t enforce Norminette style, but readability still matters.
- Makefile rules are still checked during evaluation (
all/clean/fclean/re, no unnecessary relinking, explicit file listing, etc.). - Don’t just “make it work”: during defense you may be asked to tweak behavior quickly, so keep your design clean and explainable.
If you’re a 42 student working on the same module, feel free to explore the code, get inspired, but write your own implementation — that’s where the real learning happens. 🚀