https://github.com/tigran-sargsyan-w/cpp-module-05
This module is designed to help you understand try/catch and exceptions in C++.
https://github.com/tigran-sargsyan-w/cpp-module-05
42 42lyon 42school c-plus-plus canonical-forms cpp cpp98 custom-exceptions ecole-42 exceptions factory-pattern object-oriented-programming ocf oop operator-overloading polymorphism try-catch
Last synced: 6 days ago
JSON representation
This module is designed to help you understand try/catch and exceptions in C++.
- Host: GitHub
- URL: https://github.com/tigran-sargsyan-w/cpp-module-05
- Owner: tigran-sargsyan-w
- Created: 2026-01-30T19:34:16.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2026-03-03T18:37:17.000Z (3 months ago)
- Last Synced: 2026-05-06T11:49:04.041Z (29 days ago)
- Topics: 42, 42lyon, 42school, c-plus-plus, canonical-forms, cpp, cpp98, custom-exceptions, ecole-42, exceptions, factory-pattern, object-oriented-programming, ocf, oop, operator-overloading, polymorphism, try-catch
- Language: C++
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# C++ Module 05 β Repetition & Exceptions π§Ύβ‘οΈ
β
**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.*
---
## π Table of Contents
* [Description](#-description)
* [Goals of the Module](#-goals-of-the-module)
* [Exercises Overview](#-exercises-overview)
* [ex00 β Bureaucrat](#ex00--bureaucrat)
* [ex01 β Forms](#ex01--forms)
* [ex02 β AForm + Concrete Forms](#ex02--aform--concrete-forms)
* [ex03 β Intern](#ex03--intern)
* [Requirements](#-requirements)
* [Build & Run](#-build--run)
* [Repository Layout](#-repository-layout)
* [Testing Tips](#-testing-tips)
* [42 Notes](#-42-notes)
---
## π Description
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 `Bureaucrat` with a strict **grade range** (`1` is highest, `150` is lowest)
* Forms that require specific grades to **sign** and **execute**
* An abstract base class (`AForm`) with concrete derived forms
* An `Intern` that creates forms by name (factory-like behavior)
All exercises are written in **C++98** and compiled with strict flags.
---
## π― Goals of the Module
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()`
---
## π¦ Exercises Overview
### ex00 β Bureaucrat
**Goal:** Implement a `Bureaucrat` class with:
* `const std::string name`
* `int grade` in range **[1..150]**
* Grade changes:
* `incrementGrade()` (e.g., 3 β 2)
* `decrementGrade()` (e.g., 3 β 4)
* Exceptions on invalid grade:
* `GradeTooHighException`
* `GradeTooLowException`
* `operator<<` output format:
* `, bureaucrat grade .`
---
### ex01 β Forms
**Goal:** Add a `Form` class containing:
* `const std::string name`
* `bool isSigned` (initially `false`)
* `const int gradeToSign`
* `const 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::GradeTooHighException`
* `Form::GradeTooLowException`
---
### ex02 β AForm + Concrete Forms
**Goal:** Turn `Form` into an abstract base class `AForm` and implement 3 concrete forms.
Concrete forms:
* **`ShrubberyCreationForm`**
Required grades: sign **145**, exec **137**
Creates `_shrubbery` and writes ASCII trees inside.
* **`RobotomyRequestForm`**
Required grades: sign **72**, exec **45**
βDrilling noisesβ + **50% success** robotomy.
* **`PresidentialPardonForm`**
Required grades: sign **25**, exec **5**
Announces pardon βby Zaphod Beeblebroxβ.
Also required:
* `AForm::execute(Bureaucrat const& executor) const`
Must check:
* form is signed
* executor grade is high enough
Otherwise throw appropriate exceptions.
* `Bureaucrat::executeForm(AForm const&)` prints success or explicit error.
---
### ex03 β Intern
**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 `
* If unknown form name:
* print explicit error message
* **No ugly giant if/else/else-if chain** (clean mapping approach expected)
---
## π Requirements
* **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)
---
## βΆοΈ Build & Run
Clone the repository and build each exercise separately:
```bash
git clone
cd cpp-module-05
```
### ex00
```bash
cd ex00
make
./bureaucrat
```
### ex01
```bash
cd ex01
make
./bureaucracy
```
### ex02
```bash
cd ex02
make
./forms
```
### ex03
```bash
cd ex03
make
./intern
```
> Executable names may differ depending on implementation β check each `Makefile`.
---
## π Repository Layout
```text
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)
```
---
## π Testing Tips
* **Grades**
* Try constructing bureaucrats/forms with grades `0`, `151`, etc. β must throw.
* Test boundary values: `1` and `150`.
* **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.
---
## π§Ύ 42 Notes
* 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. π