This commit is contained in:
Ladebeze66 2024-02-20 15:37:45 +01:00
parent 8ad95969ba
commit bb9b81c53f
15 changed files with 341 additions and 203 deletions

View File

@ -57,7 +57,50 @@
"C_Cpp_Runner.useLinkTimeOptimization": false, "C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false, "C_Cpp_Runner.msvcSecureNoWarnings": false,
"files.associations": { "files.associations": {
"iostream": "cpp", "iostream": "cpp",
"ostream": "cpp" "ostream": "cpp",
} "array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
},
"C_Cpp.errorSquiggles": "disabled"
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */ /* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/14 14:38:10 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 16:48:16 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
// Constructeur par défaut // Constructeur par défaut
Bureaucrat::Bureaucrat() : name("Default"), grade(150) Bureaucrat::Bureaucrat() : name("Default"), grade(150)
{ {
std::cout << GREEN << "Bureaucrat default constructor called!\n" << RESET << std::endl; std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl;
} }
// Constructeur avec paramètres // Constructeur avec paramètres
Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade) Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade)
@ -73,7 +73,8 @@ void Bureaucrat::incrementGrade()
// Décrémente le grade // Décrémente le grade
void Bureaucrat::decrementGrade() void Bureaucrat::decrementGrade()
{ {
if (grade >= 150) { if (grade >= 150)
{
throw GradeTooLowException(); throw GradeTooLowException();
} }
++grade; ++grade;
@ -81,12 +82,12 @@ void Bureaucrat::decrementGrade()
// Méthodes pour les exceptions // Méthodes pour les exceptions
const char* Bureaucrat::GradeTooHighException::what() const throw() const char* Bureaucrat::GradeTooHighException::what() const throw()
{ {
return "Grade too hight\n"; return ("Grade too hight\n");
} }
const char* Bureaucrat::GradeTooLowException::what() const throw() const char* Bureaucrat::GradeTooLowException::what() const throw()
{ {
return "Grade too low\n"; return ("Grade too low\n");
} }
// Surcharge de l'opérateur << // Surcharge de l'opérateur <<
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat)
@ -94,4 +95,3 @@ std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat)
os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade(); os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade();
return (os); return (os);
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/14 14:32:41 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 16:39:04 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,35 +30,34 @@
class Bureaucrat class Bureaucrat
{ {
public: public:
class GradeTooHighException : public std::exception class GradeTooHighException : public std::exception
{ {
public: public:
const char* what() const throw() override; const char* what() const throw() override;
}; };
class GradeTooLowException : public std::exception class GradeTooLowException : public std::exception
{ {
public: public:
const char* what() const throw() override; const char* what() const throw() override;
}; };
Bureaucrat(); Bureaucrat();
Bureaucrat(const std::string& name, int grade); Bureaucrat(const std::string& name, int grade);
Bureaucrat(const Bureaucrat& other); Bureaucrat(const Bureaucrat& other);
Bureaucrat& operator=(const Bureaucrat& other); Bureaucrat& operator=(const Bureaucrat& other);
~Bureaucrat(); ~Bureaucrat();
std::string getName() const; std::string getName() const;
int getGrade() const; int getGrade() const;
void incrementGrade(); void incrementGrade();
void decrementGrade(); void decrementGrade();
private: private:
const std::string name; const std::string name;
int grade; int grade;
}; };
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat); std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);
#endif #endif

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:28:49 by fgras-ca #+# #+# */ /* Created: 2024/02/11 13:28:49 by fgras-ca #+# #+# */
/* Updated: 2024/02/14 14:37:10 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 16:48:36 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */ /* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/14 14:38:10 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 17:30:32 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
// Constructeur par défaut // Constructeur par défaut
Bureaucrat::Bureaucrat() : name("Default"), grade(150) Bureaucrat::Bureaucrat() : name("Default"), grade(150)
{ {
std::cout << GREEN << "Bureaucrat default constructor called!\n" << RESET << std::endl; std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl;
} }
// Constructeur avec paramètres // Constructeur avec paramètres
Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade) Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade)
@ -73,20 +73,34 @@ void Bureaucrat::incrementGrade()
// Décrémente le grade // Décrémente le grade
void Bureaucrat::decrementGrade() void Bureaucrat::decrementGrade()
{ {
if (grade >= 150) { if (grade >= 150)
{
throw GradeTooLowException(); throw GradeTooLowException();
} }
++grade; ++grade;
} }
void Bureaucrat::signForm(Form& form)
{
try
{
form.beSigned(*this);
std::cout << this->name << " signed " << form.getName() << std::endl;
}
catch (std::exception& e)
{
std::cout << this->name << " couldnt sign " << form.getName() << " because " << e.what() << std::endl;
}
}
// Méthodes pour les exceptions // Méthodes pour les exceptions
const char* Bureaucrat::GradeTooHighException::what() const throw() const char* Bureaucrat::GradeTooHighException::what() const throw()
{ {
return "Grade too hight\n"; return ("Grade too hight\n");
} }
const char* Bureaucrat::GradeTooLowException::what() const throw() const char* Bureaucrat::GradeTooLowException::what() const throw()
{ {
return "Grade too low\n"; return ("Grade too low\n");
} }
// Surcharge de l'opérateur << // Surcharge de l'opérateur <<
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat)
@ -94,4 +108,3 @@ std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat)
os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade(); os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade();
return (os); return (os);
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/14 14:32:41 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 17:30:30 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@
#include <string> #include <string>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include "Form.hpp"
#define RESET "\033[0m" #define RESET "\033[0m"
#define BLACK "\033[30m" #define BLACK "\033[30m"
@ -30,35 +31,35 @@
class Bureaucrat class Bureaucrat
{ {
public: public:
class GradeTooHighException : public std::exception class GradeTooHighException : public std::exception
{ {
public: public:
const char* what() const throw() override; const char* what() const throw() override;
}; };
class GradeTooLowException : public std::exception class GradeTooLowException : public std::exception
{ {
public: public:
const char* what() const throw() override; const char* what() const throw() override;
}; };
Bureaucrat(); Bureaucrat();
Bureaucrat(const std::string& name, int grade); Bureaucrat(const std::string& name, int grade);
Bureaucrat(const Bureaucrat& other); Bureaucrat(const Bureaucrat& other);
Bureaucrat& operator=(const Bureaucrat& other); Bureaucrat& operator=(const Bureaucrat& other);
~Bureaucrat(); ~Bureaucrat();
std::string getName() const; std::string getName() const;
int getGrade() const; int getGrade() const;
void incrementGrade(); void incrementGrade();
void decrementGrade(); void decrementGrade();
void signForm(Form& form);
private: private:
const std::string name; const std::string name;
int grade; int grade;
}; };
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat); std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);
#endif #endif

View File

@ -6,16 +6,17 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */ /* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 16:05:55 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 18:19:00 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include "Form.hpp" #include "Form.hpp"
Form::Form(const std::string& name) Form::Form()
: name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1) : name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1)
{ {
std::cout << GREEN << "Form default constructor called!" << RESET << std::endl;
} }
Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute) Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute)
@ -34,15 +35,21 @@ Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredTo
Form::Form(const Form& other) Form::Form(const Form& other)
: name(other.name), isSigned(other.isSigned), gradeRequiredToSign(other.gradeRequiredToSign), gradeRequiredToExecute(other.gradeRequiredToExecute) : name(other.name), isSigned(other.isSigned), gradeRequiredToSign(other.gradeRequiredToSign), gradeRequiredToExecute(other.gradeRequiredToExecute)
{ {
std::cout << CYAN << "Copy constructor called for Form " << this->name << RESET << std::endl;
} }
Form& Form::operator=(const Form& other) Form& Form::operator=(const Form& other)
{ {
(void)other; if (this != &other)
{
this->isSigned = other.isSigned;
}
return (*this); return (*this);
} }
Form::~Form() Form::~Form()
{ {
std::cout << RED << "Form " << this->name << " is destroyed!" << RESET << std::endl;
} }
std::string Form::getName() const std::string Form::getName() const
@ -77,11 +84,21 @@ void Form::beSigned(const Bureaucrat& bureaucrat)
} }
} }
const char* Form::GradeTooHighException::what() const noexcept
{
return ("Grade too hight\n");
}
const char* Form::GradeTooLowException::what() const noexcept
{
return ("Grade too low\n");
}
std::ostream& operator<<(std::ostream& os, const Form& form) std::ostream& operator<<(std::ostream& os, const Form& form)
{ {
os << "Form: " << form.getName() os << "Form: " << form.getName()
<< ", Status: " << (form.getIsSigned() ? "Signed" : "Not signed") << ", Status: " << (form.getIsSigned() ? "Signed" : "Not signed")
<< ", Grade Required to Sign: " << form.getGradeRequiredToSign() << ", Grade Required to Sign: " << form.getGradeRequiredToSign()
<< ", Grade Required to Execute: " << form.getGradeRequiredToExecute(); << ", Grade Required to Execute: " << form.getGradeRequiredToExecute();
return os; return (os);
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */ /* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/12 14:43:02 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 18:26:09 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,14 +23,13 @@ class Form
{ {
private: private:
const std::string name; const std::string name;
const
bool isSigned; bool isSigned;
const int gradeRequiredToSign; const int gradeRequiredToSign;
const int gradeRequiredToExecute; const int gradeRequiredToExecute;
public: public:
//constructueur //constructueur
Form(const::std::string& name); Form();
Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie // Constructeur par copie
Form(const Form& other); Form(const Form& other);
@ -50,19 +49,13 @@ public:
class GradeTooHighException : public std::exception class GradeTooHighException : public std::exception
{ {
public: public:
const char* what() const noexcept override const char* what() const noexcept override;
{
return ("Grade too high");
}
}; };
class GradeTooLowException : public std::exception class GradeTooLowException : public std::exception
{ {
public: public:
const char* what() const noexcept override const char* what() const noexcept override;
{
return ("Grade too low");
}
}; };
}; };

104
cpp05/ex02/AForm.cpp Normal file
View File

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/16 18:44:23 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "AForm.hpp"
AForm::AForm()
: name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1)
{
std::cout << GREEN << "AForm default constructor called!" << RESET << std::endl;
}
AForm::AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute)
: name(name), isSigned(false), gradeRequiredToSign(gradeRequiredToSign), gradeRequiredToExecute(gradeRequiredToExecute)
{
if (gradeRequiredToSign < 1 || gradeRequiredToExecute < 1)
{
throw GradeTooHighException();
}
else if (gradeRequiredToSign > 150 || gradeRequiredToExecute > 150)
{
throw GradeTooLowException();
}
}
AForm::AForm(const AForm& other)
: name(other.name), isSigned(other.isSigned), gradeRequiredToSign(other.gradeRequiredToSign), gradeRequiredToExecute(other.gradeRequiredToExecute)
{
std::cout << CYAN << "Copy constructor called for AForm " << this->name << RESET << std::endl;
}
AForm& AForm::operator=(const AForm& other)
{
if (this != &other)
{
this->isSigned = other.isSigned;
}
return (*this);
}
AForm::~AForm()
{
std::cout << RED << "AForm " << this->name << " is destroyed!" << RESET << std::endl;
}
std::string AForm::getName() const
{
return (name);
}
bool AForm::getIsSigned() const
{
return (isSigned);
}
int AForm::getGradeRequiredToSign() const
{
return (gradeRequiredToSign);
}
int AForm::getGradeRequiredToExecute() const
{
return (gradeRequiredToExecute);
}
void AForm::beSigned(const Bureaucrat& bureaucrat)
{
if (bureaucrat.getGrade() <= gradeRequiredToSign)
{
isSigned = true;
}
else
{
throw GradeTooLowException();
}
}
const char* AForm::GradeTooHighException::what() const noexcept
{
return ("Grade too hight\n");
}
const char* AForm::GradeTooLowException::what() const noexcept
{
return ("Grade too low\n");
}
std::ostream& operator<<(std::ostream& os, const AForm& aform)
{
os << "AForm: " << aform.getName()
<< ", Status: " << (aform.getIsSigned() ? "Signed" : "Not signed")
<< ", Grade Required to Sign: " << aform.getGradeRequiredToSign()
<< ", Grade Required to Execute: " << aform.getGradeRequiredToExecute();
return (os);
}

View File

@ -1,17 +1,17 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* Form.hpp :+: :+: :+: */ /* AForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */ /* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 15:05:06 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 18:50:37 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FORM_HPP #ifndef AFORM_HPP
#define FORM_HPP #define AFORM_HPP
#include "Bureaucrat.hpp" // Assurez-vous que cette classe est bien définie #include "Bureaucrat.hpp" // Assurez-vous que cette classe est bien définie
#include <string> #include <string>
@ -19,7 +19,7 @@
class Bureaucrat; class Bureaucrat;
class Form class AForm
{ {
private: private:
const std::string name; const std::string name;
@ -28,33 +28,38 @@ private:
const int gradeRequiredToExecute; const int gradeRequiredToExecute;
public: public:
Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); //constructueur
AForm();
AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie
AForm(const AForm& other);
// Opérateur d'affectation
virtual AForm& operator=(const AForm& other);
// Destructeur
virtual ~AForm();
std::string getName() const; std::string getName() const;
bool getIsSigned() const; bool getIsSigned() const;
int getGradeRequiredToSign() const; int getGradeRequiredToSign() const;
int getGradeRequiredToExecute() const; int getGradeRequiredToExecute() const;
void beSigned(const Bureaucrat& bureaucrat); virtual void beSigned(const Bureaucrat& bureaucrat);
class GradeTooHighException : public std::exception class GradeTooHighException : public std::exception
{ {
public: public:
const char* what() const noexcept override const char* what() const noexcept override;
{
return ("Grade too high");
}
}; };
class GradeTooLowException : public std::exception class GradeTooLowException : public std::exception
{ {
public: public:
const char* what() const noexcept override const char* what() const noexcept override;
{
return ("Grade too low");
}
}; };
}; };
std::ostream& operator<<(std::ostream& os, const Form& form); // Surcharge de l'opérateur d'insertion
std::ostream& operator<<(std::ostream& os, const AForm& aform);
#endif #endif

View File

@ -6,14 +6,19 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */ /* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:50:15 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 17:30:32 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include "Form.hpp"
Bureaucrat::Bureaucrat(const std::string &nom, int grade) : name(nom) // Constructeur par défaut
Bureaucrat::Bureaucrat() : name("Default"), grade(150)
{
std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl;
}
// Constructeur avec paramètres
Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade)
{ {
if (grade < 1) if (grade < 1)
{ {
@ -24,18 +29,39 @@ Bureaucrat::Bureaucrat(const std::string &nom, int grade) : name(nom)
throw GradeTooLowException(); throw GradeTooLowException();
} }
this->grade = grade; this->grade = grade;
std::cout << GREEN << "Bureaucrat " << this->name << " constructor called with grade " << this->grade << RESET << std::endl;
} }
// Constructeur de copie
Bureaucrat::Bureaucrat(const Bureaucrat& other) : name(other.name), grade(other.grade)
{
std::cout << CYAN << "Copy constructor called for Bureaucrat " << this->name << RESET << std::endl;
}
// Opérateur d'affectation
Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other)
{
if (this != &other)
{
// L'attribut name étant const, nous ne pouvons pas le modifier. Seul grade est copié.
this->grade = other.grade;
}
return (*this);
}
// Destructeur
Bureaucrat::~Bureaucrat()
{
std::cout << RED << "Bureaucrat " << this->name << " is destroyed!" << RESET << std::endl;
}
// Getter pour le nom
std::string Bureaucrat::getName() const std::string Bureaucrat::getName() const
{ {
return (name); return (this->name);
} }
// Getter pour le grade
int Bureaucrat::getGrade() const int Bureaucrat::getGrade() const
{ {
return (grade); return (this->grade);
} }
// Incrémente le grade
void Bureaucrat::incrementGrade() void Bureaucrat::incrementGrade()
{ {
if (grade <= 1) if (grade <= 1)
@ -44,7 +70,7 @@ void Bureaucrat::incrementGrade()
} }
--grade; --grade;
} }
// Décrémente le grade
void Bureaucrat::decrementGrade() void Bureaucrat::decrementGrade()
{ {
if (grade >= 150) if (grade >= 150)
@ -60,14 +86,25 @@ void Bureaucrat::signForm(Form& form)
{ {
form.beSigned(*this); form.beSigned(*this);
std::cout << this->name << " signed " << form.getName() << std::endl; std::cout << this->name << " signed " << form.getName() << std::endl;
} catch (std::exception& e) }
catch (std::exception& e)
{ {
std::cout << this->name << " couldnt sign " << form.getName() << " because " << e.what() << std::endl; std::cout << this->name << " couldnt sign " << form.getName() << " because " << e.what() << std::endl;
} }
} }
// Méthodes pour les exceptions
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat) const char* Bureaucrat::GradeTooHighException::what() const throw()
{ {
os << bureaucrat.getName() << MAGENTA << ", bureaucrat grade " << RESET << bureaucrat.getGrade(); return ("Grade too hight\n");
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade too low\n");
}
// Surcharge de l'opérateur <<
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat)
{
os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade();
return (os); return (os);
} }

View File

@ -6,66 +6,60 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:59:51 by fgras-ca ### ########.fr */ /* Updated: 2024/02/16 18:26:55 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef BUREAUCRAT_HPP #ifndef BUREAUCRAT_HPP
#define BUREAUCRAT_HPP #define BUREAUCRAT_HPP
#include "Form.hpp"
#include <string> #include <string>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include "AForm.hpp"
#define RESET "\033[0m" #define RESET "\033[0m"
#define BLACK "\033[30m" #define BLACK "\033[30m"
#define RED "\033[31m" #define RED "\033[31m"
#define GREEN "\033[32m" #define GREEN "\033[32m"
#define YELLOW "\033[33m" #define YELLOW "\033[33m"
#define BLUE "\033[34m" #define BLUE "\033[34m"
#define MAGENTA "\033[35m" #define MAGENTA "\033[35m"
#define CYAN "\033[36m" #define CYAN "\033[36m"
#define WHITE "\033[37m" #define WHITE "\033[37m"
class Bureaucrat class Bureaucrat
{ {
public: public:
class GradeTooHighException : public std::exception class GradeTooHighException : public std::exception
{ {
public: public:
const char* what() const throw() override const char* what() const throw() override;
{
return ("Grade trop élevé");
}
}; };
class GradeTooLowException : public std::exception class GradeTooLowException : public std::exception
{ {
public: public:
const char* what() const throw() override const char* what() const throw() override;
{
return ("Grade trop bas");
}
}; };
private: Bureaucrat();
const std::string name; Bureaucrat(const std::string& name, int grade);
int grade; Bureaucrat(const Bureaucrat& other);
Bureaucrat& operator=(const Bureaucrat& other);
public: ~Bureaucrat();
Bureaucrat(const std::string &nom, int grade); // Constructeur
Bureaucrat(const Bureaucrat& other) = default; // Constructeur par copie
Bureaucrat& operator=(const Bureaucrat& other) = default; // Opérateur d'affectation
~Bureaucrat() = default; // Destructeur
std::string getName() const; std::string getName() const;
int getGrade() const; int getGrade() const;
void incrementGrade(); void incrementGrade();
void decrementGrade(); void decrementGrade();
void signForm(Form& form); void signForm(Form& form);
private:
const std::string name;
int grade;
}; };
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat); std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);
#endif #endif

View File

@ -1,68 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 15:03:38 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute)
: name(name), isSigned(false), gradeRequiredToSign(gradeRequiredToSign), gradeRequiredToExecute(gradeRequiredToExecute)
{
if (gradeRequiredToSign < 1 || gradeRequiredToExecute < 1)
{
throw GradeTooHighException();
}
else if (gradeRequiredToSign > 150 || gradeRequiredToExecute > 150)
{
throw GradeTooLowException();
}
}
std::string Form::getName() const
{
return (name);
}
bool Form::getIsSigned() const
{
return (isSigned);
}
int Form::getGradeRequiredToSign() const
{
return (gradeRequiredToSign);
}
int Form::getGradeRequiredToExecute() const
{
return (gradeRequiredToExecute);
}
void Form::beSigned(const Bureaucrat& bureaucrat)
{
if (bureaucrat.getGrade() <= gradeRequiredToSign)
{
isSigned = true;
}
else
{
throw GradeTooLowException();
}
}
std::ostream& operator<<(std::ostream& os, const Form& form)
{
os << "Form: " << form.getName()
<< ", Status: " << (form.getIsSigned() ? "Signed" : "Not signed")
<< ", Grade Required to Sign: " << form.getGradeRequiredToSign()
<< ", Grade Required to Execute: " << form.getGradeRequiredToExecute();
return os;
}

View File

@ -6,7 +6,7 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ # # By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # # Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/11 14:58:41 by fgras-ca ### ########.fr # # Updated: 2024/02/16 18:36:25 by fgras-ca ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -26,7 +26,7 @@ ORANGE = \033[38;5;214m
NAME = Form NAME = Form
SRC = Bureaucrat.cpp \ SRC = Bureaucrat.cpp \
Form.cpp \ AForm.cpp \
main.cpp \ main.cpp \
OBJS = ${SRC:.cpp=.o} OBJS = ${SRC:.cpp=.o}