This commit is contained in:
Ladebeze66 2024-02-14 14:43:54 +01:00
parent 04480a56eb
commit 8ad95969ba
8 changed files with 175 additions and 125 deletions

View File

@ -57,6 +57,7 @@
"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"
}
} }

View File

@ -6,13 +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 13:58:43 by fgras-ca ### ########.fr */ /* Updated: 2024/02/14 14:38:10 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.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!\n" << 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)
{ {
@ -23,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)
@ -43,18 +70,28 @@ void Bureaucrat::incrementGrade()
} }
--grade; --grade;
} }
// Décrémente le grade
void Bureaucrat::decrementGrade() void Bureaucrat::decrementGrade()
{ {
if (grade >= 150) if (grade >= 150) {
{
throw GradeTooLowException(); throw GradeTooLowException();
} }
++grade; ++grade;
} }
// 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,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/11 14:01:58 by fgras-ca ### ########.fr */ /* Updated: 2024/02/14 14:32:41 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,53 +17,48 @@
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#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");
} Bureaucrat();
}; Bureaucrat(const std::string& name, int grade);
Bureaucrat(const Bureaucrat& other);
Bureaucrat& operator=(const Bureaucrat& other);
~Bureaucrat();
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
private: private:
const std::string name; const std::string name;
int grade; int grade;
public:
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;
int getGrade() const;
void incrementGrade();
void decrementGrade();
}; };
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat); std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);
#endif #endif

BIN
cpp05/ex00/Bureaucrat.o Normal file

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/11 14:08:26 by fgras-ca ### ########.fr */ /* Updated: 2024/02/14 14:37:10 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/14 14:38:10 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!\n" << 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,30 +70,28 @@ void Bureaucrat::incrementGrade()
} }
--grade; --grade;
} }
// Décrémente le grade
void Bureaucrat::decrementGrade() void Bureaucrat::decrementGrade()
{ {
if (grade >= 150) if (grade >= 150) {
{
throw GradeTooLowException(); throw GradeTooLowException();
} }
++grade; ++grade;
} }
// Méthodes pour les exceptions
void Bureaucrat::signForm(Form& form) const char* Bureaucrat::GradeTooHighException::what() const throw()
{ {
try return "Grade too hight\n";
{
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;
}
} }
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat) const char* Bureaucrat::GradeTooLowException::what() const throw()
{ {
os << bureaucrat.getName() << MAGENTA << ", bureaucrat grade " << RESET << bureaucrat.getGrade(); 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,59 @@
/* 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/14 14:32:41 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>
#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");
} Bureaucrat();
}; Bureaucrat(const std::string& name, int grade);
Bureaucrat(const Bureaucrat& other);
Bureaucrat& operator=(const Bureaucrat& other);
~Bureaucrat();
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
private: private:
const std::string name; const std::string name;
int grade; int grade;
public:
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;
int getGrade() const;
void incrementGrade();
void decrementGrade();
void signForm(Form& form);
}; };
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat); std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);
#endif #endif

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/11 18:03:35 by fgras-ca ### ########.fr */ /* Updated: 2024/02/12 14:43:02 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@
class Bureaucrat; class Bureaucrat;
class AForm class Form
{ {
private: private:
const std::string name; const std::string name;
@ -30,14 +30,14 @@ private:
public: public:
//constructueur //constructueur
AForm(const::std::string& name); Form(const::std::string& name);
AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie // Constructeur par copie
AForm(const AForm& other); Form(const Form& other);
// Opérateur d'affectation // Opérateur d'affectation
AForm& operator=(const AForm& other); Form& operator=(const Form& other);
// Destructeur // Destructeur
~AForm(); ~Form();
std::string getName() const; std::string getName() const;
@ -67,6 +67,6 @@ public:
}; };
// Surcharge de l'opérateur d'insertion // Surcharge de l'opérateur d'insertion
std::ostream& operator<<(std::ostream& os, const AForm& form); std::ostream& operator<<(std::ostream& os, const Form& form);
#endif #endif