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

@@ -6,13 +6,19 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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)
{
@@ -23,18 +29,39 @@ Bureaucrat::Bureaucrat(const std::string &nom, int grade) : name(nom)
throw GradeTooLowException();
}
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
{
return (name);
return (this->name);
}
// Getter pour le grade
int Bureaucrat::getGrade() const
{
return (grade);
return (this->grade);
}
// Incrémente le grade
void Bureaucrat::incrementGrade()
{
if (grade <= 1)
@@ -43,18 +70,28 @@ void Bureaucrat::incrementGrade()
}
--grade;
}
// Décrémente le grade
void Bureaucrat::decrementGrade()
{
if (grade >= 150)
{
if (grade >= 150) {
throw GradeTooLowException();
}
++grade;
}
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
// Méthodes pour les exceptions
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);
}

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
class Bureaucrat
{
public:
class GradeTooHighException : public std::exception
class GradeTooHighException : public std::exception
{
public:
const char* what() const throw() override
{
return ("Grade trop élevé");
}
};
public:
const char* what() const throw() override;
};
class GradeTooLowException : public std::exception
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw() override
{
return ("Grade trop bas");
}
};
public:
const char* what() const throw() override;
};
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:
const std::string name;
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();
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

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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */