Skip to content

Samia-Hb/CPP-Modules

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

102 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

C++ Modules β€” 1337 coding School(42 Network)

A journey through object-oriented programming in C++ β€” from the very first class to advanced STL algorithms.


πŸ“– Table of Contents


🧠 About

The C++ Modules project at 42 School is a series of progressively challenging exercises designed to introduce the core concepts of Object-Oriented Programming in C++98. Starting from namespaces and simple classes, the journey culminates in sophisticated uses of the Standard Template Library, casts, templates, and sorting algorithms.

Each module lives in its own directory (cpp00/ through cpp09/), and every exercise within a module builds upon the last.

Key rules applied throughout:

  • C++98 standard only (-std=c++98)
  • No external libraries
  • Orthodox Canonical Form (OCF) for all classes when required
  • No memory leaks β€” proper new/delete management

πŸ—ΊοΈ Modules Overview

Module Core Topics Exercises
CPP00 Namespaces, Classes, I/O, const, static ex00 Β· ex01 Β· ex02
CPP01 Heap vs Stack, Pointers, References, Switch ex00 β†’ ex06
CPP02 Fixed-point arithmetic, Operator Overloading, OCF ex00 Β· ex01 Β· ex02
CPP03 Single Inheritance, Constructor chaining ex00 Β· ex01 Β· ex02
CPP04 Virtual functions, Deep copy, Abstract classes ex00 Β· ex01 Β· ex02
CPP05 Exceptions, try/catch, Abstract forms ex00 β†’ ex03
CPP06 static_cast, reinterpret_cast, dynamic_cast ex00 Β· ex01 Β· ex02
CPP07 Function templates, Class templates ex00 Β· ex01 Β· ex02
CPP08 easyfind, Span, MutantStack ex00 Β· ex01 Β· ex02
CPP09 map, stack, vector/deque, Ford-Johnson sort ex00 Β· ex01 Β· ex02

πŸ“¦ CPP00 β€” Namespace Β· Classes Β· I/O

Your first step into C++ β€” discovering namespaces, classes, member functions, and the standard I/O stream.

Exercises

ex00 β€” Megaphone

A straightforward warm-up: takes command-line arguments and prints them in ALL CAPS. If no argument is given, it shouts the classic feedback noise.

$ ./megaphone "hello world"
HELLO WORLD

Concepts: std::cout, toupper, command-line arguments.


ex01 β€” My Awesome PhoneBook

An interactive phonebook application stored entirely in memory (no file persistence). Supports two commands:

  • ADD β€” adds a new contact (up to 8 slots, oldest is overwritten)
  • SEARCH β€” displays a formatted list, then lets you inspect a contact by index

Classes: Contact (stores name, phone number, darkest secret) Β· PhoneBook (manages up to 8 contacts)

Concepts: Classes, member functions, std::cin, string formatting, field truncation with ....


ex02 β€” The Job Of Your Dreams

Re-implements the Account class so that its output exactly matches a provided log file (19920104_091532.log). A reverse-engineering exercise to understand class design from observable behavior.

Concepts: static members, constructor/destructor ordering, formatted output.


🧟 CPP01 β€” Memory Β· References Β· Pointers

Who controls memory controls the program. Learn to create objects on the heap and the stack, and master the difference between references and pointers.

Exercises

ex00 β€” BraiiiiiiinnnzzzZ

Introduces heap vs. stack allocation through a Zombie class that announces itself.

  • newZombie() β€” allocates on the heap, returns the pointer (caller manages lifetime)
  • randomChump() β€” allocates on the stack (destroyed at end of scope)

Concepts: new, delete, constructors/destructors, scope lifetime.


ex01 β€” Moar Brainz!

Allocates a horde of N zombies in a single new[] call β€” demonstrating contiguous heap allocation.

Concepts: new[], delete[], array of objects.


ex02 β€” HI THIS IS BRAIN

Explores the equivalence of a pointer and a reference to the same variable, printing the address and value through both.

Concepts: Pointer vs. reference, address-of operator &, dereferencing.


ex03 β€” Unnecessary Violence

HumanA holds its Weapon by reference (always has one), while HumanB holds it by pointer (may not have one). Both can attack() with their weapon.

Concepts: Reference vs. pointer members, when each is appropriate.


ex04 β€” Sed is for losers

Opens an input file, replaces every occurrence of a string s1 with s2, and writes the result to <filename>.replace β€” without using std::string::replace.

Concepts: File I/O (std::ifstream, std::ofstream), string manipulation.


ex05 β€” Harl 2.0

Harl complains at four severity levels: DEBUG, INFO, WARNING, ERROR. Instead of a long if/else chain, the complain() method dispatches using an array of pointers to member functions.

Concepts: Pointers to member functions, dispatch tables.


ex06 β€” Harl Filter

Filters Harl's output using a switch statement on the log level β€” once a level is matched, all higher-severity levels also print (fall-through behavior).

Concepts: switch/case, fall-through, string-to-enum mapping.


πŸ”’ CPP02 β€” Fixed-Point Β· Operator Overloading

Numbers with fractional parts β€” but without floating-point hardware. Learn the Orthodox Canonical Form and make your class behave like a native type.

Exercises

ex00 β€” My First Class in Orthodox Canonical Form

Introduces the Fixed class representing a fixed-point number (8 fractional bits) using an int as storage. Implements the Orthodox Canonical Form: default constructor, copy constructor, copy assignment operator, destructor.

Concepts: OCF, static const members, getRawBits()/setRawBits().


ex01 β€” Towards a More Useful Fixed-Point Number Class

Extends Fixed with constructors that accept int and float, plus toFloat() and toInt() conversion methods. Also overloads operator<< for easy printing.

Concepts: Implicit conversions, static_cast, bit shifting, stream operators.


ex02 β€” Now We're Talking

The full-featured Fixed class with:

  • All six comparison operators (>, <, >=, <=, ==, !=)
  • All four arithmetic operators (+, -, *, /)
  • Pre/post increment and decrement operators
  • static min() and max() functions

Concepts: Operator overloading, const and non-const overloads.


🏰 CPP03 β€” Inheritance

Build a hierarchy of game characters, each more powerful than the last β€” by inheriting and extending.

All three exercises center on a game-inspired class family. Each derived class calls the parent constructor and extends behavior.

Exercises

ex00 β€” ClapTrap

The base class for a simple robot fighter:

Attribute Value
Hit Points 10
Energy Points 10
Attack Damage 0

Methods: attack(), takeDamage(), beRepaired().


ex01 β€” Serena, My Love!

ScavTrap inherits from ClapTrap, overrides stats and introduces guardGate().

Attribute Value
Hit Points 100
Energy Points 50
Attack Damage 20

Concepts: Public inheritance, constructor/destructor chaining, method overriding.


ex02 β€” Repetition is the Key to Mastery

FragTrap inherits from ClapTrap with its own stats and highFivesGuys() method.

Attribute Value
Hit Points 100
Energy Points 100
Attack Damage 30

Concepts: Same inheritance mechanics, reinforcing the pattern.


🐾 CPP04 β€” Polymorphism Β· Abstract Classes

A dog says woof. A cat says meow. But what does an Animal say? Welcome to virtual dispatch.

Exercises

ex00 β€” Polymorphism

Animal is a base class with a virtual makeSound(). Dog and Cat override it. WrongAnimal/WrongCat demonstrate what happens without virtual β€” the base class method is called regardless.

Concepts: virtual, vtable, upcasting, WrongAnimal counter-example.


ex01 β€” I Don't Want to Set the World on Fire

Each Dog and Cat now owns a Brain object (an array of 100 std::string ideas). Deep copy is required: the copy constructor and assignment operator must copy the Brain content, not just the pointer.

Concepts: Deep copy vs. shallow copy, pointer ownership.


ex02 β€” Abstract Class

Animal::makeSound() becomes pure virtual (= 0), making Animal an abstract class that cannot be instantiated directly. Only Dog and Cat can be instantiated.

Concepts: Pure virtual functions, abstract classes, interface design.


πŸ›οΈ CPP05 β€” Exceptions

When things go wrong, throw an exception. When an exception is thrown, catch it gracefully.

The entire module revolves around a bureaucratic universe of Bureaucrats and Forms.

Exercises

ex00 β€” Mommy, When I Grow Up, I Want to Be a Bureaucrat!

Bureaucrat has a const name and a grade (1 = highest, 150 = lowest). Attempting to set a grade out of range throws GradeTooHighException or GradeTooLowException.

Concepts: Custom exception classes, std::exception, what(), try/catch.


ex01 β€” Form Up, Maggots!

Form has a name, a sign grade, and an execute grade. Bureaucrat::signForm() tries to sign it; if the bureaucrat's grade is too low, it throws.

Concepts: Exception propagation, const member variables.


ex02 β€” No, You Need Form 28B, Not Form 28C...

AForm becomes an abstract base class. Three concrete forms are implemented:

Form Sign Grade Exec Grade Effect
ShrubberyCreationForm 145 137 Creates a file with ASCII trees
RobotomyRequestForm 72 45 50% chance of successful robotomy
PresidentialPardonForm 25 5 Announces a presidential pardon

Concepts: Abstract base classes, pure virtual execute(), polymorphism + exceptions.


ex03 β€” At Least This Is Easier Than Form 28B

Intern can create any of the three forms by name using a dispatch table of function pointers β€” no if/else chains.

Concepts: Intern factory pattern, array of function pointers, scalable dispatch.


πŸ”€ CPP06 β€” C++ Casts

C++ offers four named cast operators. Know when to use each one β€” and when not to.

Exercises

ex00 β€” Conversion of Scalar Types

ScalarConverter is a non-instantiable utility class (all constructors private). Its static convert() method accepts a string literal representing a scalar value and outputs the equivalent char, int, float, and double.

Handles special cases: nan, nanf, +inf, -inf, non-displayable characters.

Cast used: static_cast


ex01 β€” Serialization

Serializer converts a Data* pointer to a uintptr_t integer via serialize() and back via deserialize() β€” demonstrating that the original pointer is recovered.

Cast used: reinterpret_cast


ex02 β€” Real Type Identification

generate() randomly creates an A, B, or C object (all deriving from Base) and returns it as a Base*. The two identify() overloads β€” one taking a pointer, one a reference β€” determine the real derived type at runtime.

Cast used: dynamic_cast, typeid


πŸ“ CPP07 β€” Templates

Write once, run with any type. Templates are C++'s answer to generic programming.

Exercises

ex00 β€” Start With a Few Functions

Three function templates that work with any type supporting comparison and assignment:

template <typename T> void swap(T& a, T& b);
template <typename T> T    min(T a, T b);
template <typename T> T    max(T a, T b);

ex01 β€” Iter

A function template iter() that applies a given function to every element of an array:

template <typename T, typename T1>
void iter(T array[], int length, void (func)(T1&));

ex02 β€” Array

A fully-featured template class Array<T> that:

  • Allocates elements with new T[n]
  • Provides operator[] with bounds checking (throws Out_Of_Range)
  • Implements the Orthodox Canonical Form
  • Tracks its own size via T_size()

Concepts: Class templates, template specialization, exception-safe indexing.


πŸ“š CPP08 β€” Templated Containers Β· Iterators Β· Algorithms

The STL is your toolbox. Learn to use and extend it.

Exercises

ex00 β€” Easy Find

A function template easyfind<T> that searches any first-class container for an integer. Throws NonExistException if not found.

template <typename T>
void easyfind(T& container, int value);

ex01 β€” Span

Span stores up to N integers and can report:

  • shortestSpan() β€” minimum difference between any two stored numbers
  • longestSpan() β€” maximum difference between any two stored numbers

Internally backed by std::vector<int> and uses std::sort + std::min_element/std::max_element.

Concepts: STL algorithms, range-based insertion, exception handling for capacity and insufficient elements.


ex02 β€” Mutated Abomination

MutantStack<T> inherits from std::stack<T> and adds iterator support by exposing begin() and end() from the underlying container (std::deque by default).

template <typename T>
class MutantStack : public std::stack<T> { ... };

Concepts: Adapter pattern, exposing protected members, iterator typedefs.


πŸ’± CPP09 β€” STL Containers

The final boss: apply the right container for the job, and implement a legendary sorting algorithm.

Exercises

ex00 β€” Bitcoin Exchange

Reads a CSV database of historical BTC prices and an input file of date | value pairs. For each input line, it finds the closest earlier date in the database and prints date => value = result.

  • Parses and validates dates and values rigorously
  • Uses std::multimap<Date, std::string> for ordered date lookup
$ ./btc input.txt
2011-01-03 => 3 = 0.9
2011-01-09 => 1 = 1
...

Containers: std::map, std::multimap


ex01 β€” Reverse Polish Notation

A stack-based RPN calculator. Accepts a mathematical expression in postfix notation and evaluates it.

$ ./RPN "8 9 * 9 - 9 - 9 - 4 - 1 +"
42
$ ./RPN "7 7 * 7 -"
42

Container: std::stack<int>


ex02 β€” PmergeMe

Implements the Ford-Johnson merge-insert sort algorithm β€” one of the most comparison-efficient sorting algorithms known β€” using two different containers and measures the time taken by each.

$ ./PmergeMe 3 5 9 7 4 1 2
Before: 3 5 9 7 4 1 2
After : 1 2 3 4 5 7 9
Time to process with std::vector : 0.00031 us
Time to process with std::deque  : 0.00029 us

Containers: std::vector<std::pair<int,int>> and std::deque<std::pair<int,int>>

Algorithm steps:

  1. Build pairs and recursively sort the larger elements
  2. Insert smaller elements using the Jacobsthal sequence for optimal binary insertion order

πŸ› οΈ How to Build & Run

Every exercise has its own Makefile. Navigate to the exercise directory and run:

# Build
make

# Run (example)
./megaphone "hello 42"

# Clean object files
make clean

# Remove binary too
make fclean

# Rebuild from scratch
make re

Compiler flags used:

c++ -Wall -Wextra -Werror -std=c++98

πŸ‘©β€πŸ’» Author

shebaz β€” 1337 School student


Built with β˜• and a lot of compiler errors at 1337 School

About

A collection of C++ projects , covering core Object-Oriented Programming concepts, memory management, templates, STL, and advanced C++ features through hands-on exercises.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors