「🎉」 init: hello world !
This commit is contained in:
122
ex01/Bureaucrat.cpp
Normal file
122
ex01/Bureaucrat.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/18 20:11:39 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/06/27 14:10:53 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
#include "Form.hpp"
|
||||
#include <exception>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(void)
|
||||
: _name("kanel"), _grade(150)
|
||||
{
|
||||
_log("➕", "Bureaucrat", "", "default constructor called");
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const Bureaucrat &cpy)
|
||||
{
|
||||
_log("➕", "Bureaucrat", "", "copy constructor called");
|
||||
if (&cpy != this)
|
||||
{
|
||||
_name = cpy.getName();
|
||||
_grade = cpy.getGrade();
|
||||
}
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(uint8_t grade)
|
||||
: _name("kanel"), _grade(grade)
|
||||
{
|
||||
_log("➕", "Bureaucrat", "", "constructor called");
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(std::string name)
|
||||
: _name(name), _grade(150)
|
||||
{
|
||||
_log("➕", "Bureaucrat", "", "constructor called");
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(std::string name, uint8_t grade)
|
||||
: _name(name), _grade(grade)
|
||||
{
|
||||
_log("➕", "Bureaucrat", "", "constructor called");
|
||||
}
|
||||
|
||||
const std::string &Bureaucrat::getName(void) const { return _name; }
|
||||
uint8_t Bureaucrat::getGrade(void) const { return _grade; }
|
||||
|
||||
Bureaucrat::~Bureaucrat(void)
|
||||
{
|
||||
_log("➖", "Bureaucrat", "", "destructor called");
|
||||
}
|
||||
|
||||
void Bureaucrat::signForm(Form &f)
|
||||
{
|
||||
try
|
||||
{
|
||||
f.beSigned(*this);
|
||||
std::cout << _name << " signed " << f.getName() << std::endl;
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
std::cout << _name << " counldn't sign " << f.getName() << " because " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy)
|
||||
{
|
||||
if (&cpy == this)
|
||||
return *this;
|
||||
_grade = cpy._grade;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Bureaucrat::IncrementGrade(void)
|
||||
{
|
||||
if (_grade - 1 < MAXGRADE)
|
||||
throw GradeTooHighException();
|
||||
_grade--;
|
||||
}
|
||||
|
||||
void Bureaucrat::DecrementGrade(void)
|
||||
{
|
||||
if (_grade + 1 > MINGRADE)
|
||||
throw GradeTooLowException();
|
||||
_grade++;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Bureaucrat &val)
|
||||
{
|
||||
os << val.getName() << ", bureaucrat grade " << (int)val.getGrade();
|
||||
return os;
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return ("Grade is too high");
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return ("Grade is too low");
|
||||
}
|
||||
63
ex01/Bureaucrat.hpp
Normal file
63
ex01/Bureaucrat.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/18 20:11:25 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/06/27 14:05:55 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
void _log(std::string emoji, std::string what, std::string who, std::string str);
|
||||
|
||||
class Form;
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
#define MAXGRADE 1
|
||||
#define MINGRADE 150
|
||||
|
||||
class Bureaucrat {
|
||||
public:
|
||||
Bureaucrat(void);
|
||||
Bureaucrat(const Bureaucrat &cpy);
|
||||
Bureaucrat(uint8_t grade);
|
||||
Bureaucrat(std::string name);
|
||||
Bureaucrat(std::string name, uint8_t grade);
|
||||
~Bureaucrat(void);
|
||||
|
||||
Bureaucrat &operator=(const Bureaucrat& other);
|
||||
|
||||
const std::string &getName(void) const;
|
||||
uint8_t getGrade(void) const;
|
||||
|
||||
void IncrementGrade(void);
|
||||
void DecrementGrade(void);
|
||||
|
||||
class GradeTooHighException : public std::exception {
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
class GradeTooLowException : public std::exception {
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
|
||||
void signForm(Form &);
|
||||
|
||||
private:
|
||||
std::string _name;
|
||||
uint8_t _grade;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &, Bureaucrat &);
|
||||
78
ex01/Form.cpp
Normal file
78
ex01/Form.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Form.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/27 13:49:35 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/06/27 15:12:49 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Form.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
Form::Form(void)
|
||||
: _name("Form"), _signed(false), _minForSign(1), _minForExec(1)
|
||||
{
|
||||
_log("➕", "Form", "", "default constructor called");
|
||||
}
|
||||
|
||||
Form::Form(std::string name, uint8_t minForSign, uint8_t minForExec)
|
||||
: _name(name), _signed(false), _minForSign(minForSign), _minForExec(minForExec)
|
||||
{
|
||||
_log("➕", "Form", "", "constructor called");
|
||||
_gradeCheck();
|
||||
}
|
||||
|
||||
Form::Form(const Form &f)
|
||||
: _name(f.getName()), _signed(f.getSigned()), _minForSign(f.getMinForSign()), _minForExec(f.getMinForExec())
|
||||
{
|
||||
_log("➕", "Form", "", "copy constructor called");
|
||||
_gradeCheck();
|
||||
}
|
||||
|
||||
Form::~Form(void) { _log("➖", "Form", "", "destructor called"); }
|
||||
|
||||
Form &Form::operator=(const Form &f) {
|
||||
if (this == &f)
|
||||
return *this;
|
||||
_name = f.getName();
|
||||
_signed = f.getSigned();
|
||||
return *this;
|
||||
}
|
||||
|
||||
const char *Form::GradeTooHighException::what() const throw() {
|
||||
return ("grade is too high");
|
||||
}
|
||||
|
||||
const char *Form::GradeTooLowException::what() const throw() {
|
||||
return ("grade is too low");
|
||||
}
|
||||
|
||||
std::string Form::getName(void) const { return _name; }
|
||||
bool Form::getSigned(void) const { return _signed; }
|
||||
uint8_t Form::getMinForSign(void) const { return _minForSign; }
|
||||
uint8_t Form::getMinForExec(void) const { return _minForExec; }
|
||||
|
||||
void Form::beSigned(Bureaucrat b) {
|
||||
if (_minForSign > b.getGrade()) {
|
||||
throw GradeTooLowException();
|
||||
}
|
||||
_signed = true;
|
||||
}
|
||||
|
||||
void Form::_gradeCheck(void) const {
|
||||
if (_minForExec < MAXGRADE || _minForSign < MAXGRADE)
|
||||
throw GradeTooHighException();
|
||||
if (_minForExec > MINGRADE || _minForExec > MINGRADE)
|
||||
throw GradeTooLowException();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Form &val) {
|
||||
os << "Form: " << val.getName()
|
||||
<< ", minGrade to sign: " << int(val.getMinForSign())
|
||||
<< ", minGrade to execute: " << int(val.getMinForExec());
|
||||
return (os);
|
||||
}
|
||||
58
ex01/Form.hpp
Normal file
58
ex01/Form.hpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Form.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/27 13:36:33 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/06/27 15:06:04 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
void _log(std::string emoji, std::string what, std::string who, std::string str);
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
class Form {
|
||||
public:
|
||||
Form(void);
|
||||
Form(std::string name, uint8_t minForSign, uint8_t minForExec);
|
||||
Form(const Form &);
|
||||
~Form(void);
|
||||
|
||||
Form &operator=(const Form &);
|
||||
|
||||
class GradeTooHighException : public std::exception {
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
class GradeTooLowException : public std::exception {
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
|
||||
std::string getName(void) const;
|
||||
bool getSigned(void) const;
|
||||
uint8_t getMinForSign(void) const;
|
||||
uint8_t getMinForExec(void) const;
|
||||
|
||||
void beSigned(Bureaucrat);
|
||||
|
||||
private:
|
||||
std::string _name;
|
||||
bool _signed;
|
||||
const uint8_t _minForSign;
|
||||
const uint8_t _minForExec;
|
||||
|
||||
void _gradeCheck(void) const;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &, Form &);
|
||||
|
||||
58
ex01/Makefile
Normal file
58
ex01/Makefile
Normal file
@@ -0,0 +1,58 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
|
||||
# Updated: 2025/06/27 14:11:13 by mmoussou ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SHELL = bash
|
||||
|
||||
RED = \033[0;31m
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[1;33m
|
||||
PURPLE = \e[0;35m
|
||||
NC = \033[0m
|
||||
DELETE = \x1B[2K
|
||||
|
||||
CC = c++
|
||||
|
||||
FLAGS = -std=c++98 -Wall -Werror -Wextra
|
||||
|
||||
INCLUDES = ./
|
||||
|
||||
NAME = ex01
|
||||
|
||||
SRCS = main.cpp Bureaucrat.cpp Form.cpp
|
||||
|
||||
OBJSDIR = obj/
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
@$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
|
||||
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
|
||||
|
||||
$(OBJSDIR)%.o: %.cpp
|
||||
@mkdir -p $(@D)
|
||||
@$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@
|
||||
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
|
||||
|
||||
clean:
|
||||
@rm -f $(OBJS)
|
||||
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@rm -Rf $(OBJSDIR)
|
||||
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
|
||||
|
||||
re: fclean
|
||||
@$(MAKE) -s all
|
||||
|
||||
.PHONY: clean fclean all re
|
||||
74
ex01/main.cpp
Normal file
74
ex01/main.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/19 01:13:45 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/06/29 20:48:03 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Form.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
try
|
||||
{
|
||||
Form test("test form", 175, 15);
|
||||
std::cout << test << std::endl;
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
Form test("test form", 120, 10);
|
||||
Form test2(test);
|
||||
Bureaucrat tester("john", 1);
|
||||
std::cout << test << std::endl;
|
||||
std::cout << test2 << std::endl;
|
||||
tester.signForm(test2);
|
||||
std::cout << test << std::endl;
|
||||
std::cout << test2 << std::endl << std::endl;
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
Form test;
|
||||
Bureaucrat tester;
|
||||
|
||||
std::cout << test << std::endl;
|
||||
tester.signForm(test);
|
||||
std::cout << test << std::endl;
|
||||
tester.signForm(test);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
Form test("some form", 149, 149);
|
||||
Bureaucrat tester;
|
||||
|
||||
std::cout << test << std::endl;
|
||||
tester.signForm(test);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user