feat: finishing the exercice 00 of the cpp05

This commit is contained in:
Raphael 2025-03-16 22:03:16 +01:00
parent c7a0cde073
commit 19ffc5d295
5 changed files with 107 additions and 8 deletions

1
.gitignore vendored
View file

@ -19,3 +19,4 @@ Fixed
open open
polymorphism polymorphism
*.replace *.replace
bureaucrat

View file

@ -6,10 +6,89 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */ /* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */
/* Updated: 2025/03/16 14:51:22 by rparodi ### ########.fr */ /* Updated: 2025/03/16 22:00:36 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include <iostream>
#include <ostream>
/**
* @brief Constructor by default of Bureaucrat
*/
Bureaucrat::Bureaucrat(void) : _name("Roger"), _grade(150) {
std::cout << "[Bureaucrat] Default constructor called" << std::endl;
}
/**
* @brief Better constructor easier to test
*
* @param name the name of the Bureaucrat
* @param grade grade to start the carreer
*/
Bureaucrat::Bureaucrat(std::string name, u_int8_t grade) : _name(name) {
std::cout << "[Bureaucrat] Smart constructor called" << std::endl;
if (grade < 1) {
throw GradeTooHighException();
} else if (grade > 150) {
throw GradeTooLowException();
} else {
this->_grade = grade;
}
}
Bureaucrat::Bureaucrat(Bureaucrat const & copy) : _name(copy._name), _grade(copy._grade) {
std::cout << "[Bureaucrat] Copy constructor called" << std::endl;
}
/**
* @brief Constructor with the operator '=' (Overload)
*
* @param assign Bureaucrat to copie
* @return It's a constructor
*/
Bureaucrat& Bureaucrat::operator=(Bureaucrat const & assign) {
std::cout << "[Bureaucrat] Assign constructor called" << std::endl;
this->_grade = assign._grade;
this->_name = assign._name;
return *this;
}
/**
* @brief Destructor for the Bureaucrat
*/
Bureaucrat::~Bureaucrat() {
std::cout << "[Bureaucrat] Desctuctor called" << std::endl;
}
/**
* @brief Getter for the grade
*
* @return grade (unsigned int 8bits)
*/
u_int8_t Bureaucrat::getGrade() const {
return (this->_grade);
}
/**
* @brief Getter for the name
*
* @return name (string from standard)
*/
std::string Bureaucrat::getName() const {
return (this->_name);
}
/**
* @brief Overload of the '<<' operator to print
*
* @param output previous output
* @param toPrint Bureaucrat to print
* @return the output with the Bureaucrat
*/
std::ostream& operator<<(std::ostream& output, Bureaucrat const &toPrint) {
output << toPrint.getName() << ", bureaucrat grade " << toPrint.getGrade() << ".";
return (output);
}

View file

@ -6,23 +6,38 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 14:48:33 by rparodi #+# #+# */ /* Created: 2025/03/16 14:48:33 by rparodi #+# #+# */
/* Updated: 2025/03/16 14:52:32 by rparodi ### ########.fr */ /* Updated: 2025/03/16 22:01:02 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include <exception>
#include <string> #include <string>
#include <iostream>
class Bureaucrat { class Bureaucrat {
public: public:
Bureaucrat(void); Bureaucrat(void);
Bureaucrat(std::string name, u_int8_t grade);
Bureaucrat(Bureaucrat const &copy); Bureaucrat(Bureaucrat const &copy);
Bureaucrat& operator=(Bureaucrat const &assign); Bureaucrat& operator=(Bureaucrat const &assign);
~Bureaucrat(void); ~Bureaucrat(void);
std::string getName(void) const; std::string getName(void) const;
int getGrade(void) const; u_int8_t getGrade(void) const;
private: class GradeTooHighException : public std::exception {
std::string const _name; virtual const char *what() const throw() {
int _grade; return ("Error:\n> Grade is too high (have to be lower than 1)");
}
}; };
class GradeTooLowException : public std::exception {
virtual const char *what() const throw() {
return ("Error:\n> Grade is too low (have to be higher than 150)");
}
};
private:
std::string _name;
u_int8_t _grade;
};
std::ostream& operator<<(std::ostream& os, Bureaucrat const &bureaucrat);

View file

@ -12,7 +12,7 @@
# Variables # Variables
# Name # Name
NAME = polymorphism NAME = bureaucrat
# Commands # Commands
CXX = c++ CXX = c++

View file

@ -6,12 +6,16 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 14:47:26 by rparodi #+# #+# */ /* Created: 2025/03/16 14:47:26 by rparodi #+# #+# */
/* Updated: 2025/03/16 14:48:29 by rparodi ### ########.fr */ /* Updated: 2025/03/16 21:53:58 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
int main(void) { int main(void) {
try {
Bureaucrat b1("Roger", 0);
} catch (std::exception & e) {
std::cerr << e.what() << std::endl;
}
} }