1
0

」 feat: Ex00 finished

This commit is contained in:
2025-03-08 20:04:33 +01:00
parent d13a82ac89
commit 4b150e5671
6 changed files with 252 additions and 0 deletions

5
.clang-format Normal file
View File

@ -0,0 +1,5 @@
UseTab: Always
IndentWidth: 4
TabWidth: 4
AlignConsecutiveDeclarations: true
ConstructorInitializerIndentWidth: 4

BIN
ex00/Bureaucrat Executable file

Binary file not shown.

57
ex00/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */
/* Updated: 2025/03/08 20:00:23 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) {
if (&cpy == this)
return *this;
_grade = cpy._grade;
return *this;
}
Bureaucrat &Bureaucrat::operator++(void) {
_grade--;
if (_grade < MAXGRADE)
throw GradeTooHighException();
return *this;
}
Bureaucrat &Bureaucrat::operator--(void) {
_grade++;
if (_grade > MINGRADE)
throw GradeTooLowException();
return *this;
}
Bureaucrat Bureaucrat::operator++(int) {
Bureaucrat old = *this;
_grade--;
if (_grade < MAXGRADE)
throw GradeTooHighException();
return (old);
}
Bureaucrat Bureaucrat::operator--(int) {
Bureaucrat old = *this;
_grade++;
if (_grade > MINGRADE)
throw GradeTooLowException();
return old;
}
std::ostream &operator<<(std::ostream &os, Bureaucrat &val) {
os << val.getName() << ", bureaucrat grade " << (int)val.getGrade();
return os;
}

85
ex00/Bureaucrat.hpp Normal file
View File

@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 13:59:43 by adjoly #+# #+# */
/* Updated: 2025/03/08 19:58:18 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <exception>
#include <fstream>
#include <stdexcept>
#include <string>
typedef unsigned char uint8_t;
inline void _log(std::string emoji, std::string what, std::string who,
std::string str) {
#ifdef VERBOSE
if (who.empty())
std::cout << "" << emoji << "" << what << ": " << str << std::endl;
else
std::cout << "" << emoji << "" << what << "(" << who << "): " << str
<< std::endl;
#else
(void)emoji, (void)what, (void)who, (void)str;
#endif
}
#define MAXGRADE 1
#define MINGRADE 150
class Bureaucrat {
public:
Bureaucrat(void) {
_log("", "Bureaucrat", "", "default constructor called");
}
Bureaucrat(const Bureaucrat &cpy) {
_log("", "Bureaucrat", "", "copy constructor called");
if (&cpy != this)
*this = cpy;
}
Bureaucrat(std::string name, uint8_t grade) : _name(name), _grade(grade) {
_log("", "Bureaucrat", "", "constructor called");
}
~Bureaucrat(void) { _log("", "Bureaucrat", "", "destructor called"); }
const std::string &getName(void) { return _name; }
uint8_t getGrade(void) { return _grade; }
class GradeTooHighException : public std::exception {
public:
virtual const char *what() const throw() {
return ("Grade is too high");
}
};
class GradeTooLowException : public std::exception {
public:
virtual const char *what() const throw() {
return ("Grade is too low");
}
};
// Copy assignement operator
Bureaucrat &operator=(Bureaucrat const &);
// Preincrement operator
Bureaucrat &operator++(void);
Bureaucrat &operator--(void);
// Post increment operator
Bureaucrat operator++(int);
Bureaucrat operator--(int);
private:
const std::string _name;
uint8_t _grade;
};
std::ostream &operator<<(std::ostream &, Bureaucrat &);

60
ex00/Makefile Normal file
View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/03/08 19:55:47 by adjoly ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = Bureaucrat
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
ifeq ($(VERBOSE),true)
FLAGS += -D VERBOSE
endif
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean all
.PHONY: clean fclean all re

45
ex00/main.cpp Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */
/* Updated: 2025/03/08 20:04:22 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include <iostream>
int main(void) {
Bureaucrat knl("Kanel", 10);
knl--;
std::cout << knl << std::endl;
knl++;
std::cout << knl << std::endl;
try {
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
} catch (const std::exception &e) {
std::cerr << "Error : " << e.what() << std::endl;
}
std::cout << knl << std::endl;
}