narnaud
3 years ago
9 changed files with 407 additions and 35 deletions
@ -0,0 +1,81 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Bureaucrat.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/26 15:59:50 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Bureaucrat.hpp" |
|||
|
|||
Bureaucrat::Bureaucrat (void) { |
|||
cout << "Bureaucrat default constructor called " << endl; |
|||
} |
|||
|
|||
Bureaucrat::Bureaucrat (const string name, int grade) |
|||
throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ |
|||
|
|||
if (grade < 1) |
|||
throw Bureaucrat::GradeTooHighException(); |
|||
if (grade > 150) |
|||
throw Bureaucrat::GradeTooLowException(); |
|||
_grade = grade; |
|||
cout << "Bureaucrat " << _name << " created grade: " << _grade << endl; |
|||
} |
|||
|
|||
Bureaucrat::Bureaucrat (Bureaucrat const & src) { |
|||
(void)src; |
|||
cout << "Bureaucrat copy constructor called" << endl; |
|||
} |
|||
|
|||
Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { |
|||
(void)src; |
|||
cout << "Bureaucrat assignment operator called" << endl; |
|||
return (*this); |
|||
} |
|||
|
|||
Bureaucrat::~Bureaucrat (void) { |
|||
cout << "Bureaucrat default destructor called << endl; |
|||
} |
|||
|
|||
const string Bureaucrat::getName(void) const{ |
|||
return (_name); |
|||
} |
|||
|
|||
int Bureaucrat::getGrade(void) const{ |
|||
return (_grade); |
|||
} |
|||
|
|||
void Bureaucrat::incrGrade(int diff) |
|||
throw (Bureaucrat::GradeTooHighException) { |
|||
int new_grade = _grade - diff; |
|||
if (new_grade < 1) |
|||
throw Bureaucrat::GradeTooHighException(); |
|||
_grade = new_grade; |
|||
} |
|||
|
|||
void Bureaucrat::decrGrade(int diff) |
|||
throw (Bureaucrat::GradeTooLowException) { |
|||
int new_grade = _grade + diff; |
|||
if (new_grade > 150) |
|||
throw Bureaucrat::GradeTooLowException(); |
|||
_grade = new_grade; |
|||
} |
|||
|
|||
const char* Bureaucrat::GradeTooHighException::what(void) const throw() { |
|||
return ("Grade was too high for a bureaucrat"); |
|||
} |
|||
|
|||
const char* Bureaucrat::GradeTooLowException::what(void) const throw() { |
|||
return ("Grade was too low for a bureaucrat"); |
|||
} |
|||
|
|||
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { |
|||
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; |
|||
return (out); |
|||
} |
|||
|
@ -0,0 +1,55 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Bureaucrat.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/26 16:35:38 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
#include <string> |
|||
#include <iostream> |
|||
#include <stdexcept> |
|||
using std::cout; |
|||
using std::endl; |
|||
using std::string; |
|||
|
|||
class Bureaucrat{ |
|||
const string _name; |
|||
int _grade; |
|||
public: |
|||
|
|||
class GradeTooHighException: virtual public std::exception |
|||
{ |
|||
public: |
|||
const char *what(void) const throw (); |
|||
}; |
|||
class GradeTooLowException: virtual public std::exception |
|||
{ |
|||
public: |
|||
const char *what(void) const throw (); |
|||
}; |
|||
|
|||
Bureaucrat(void); |
|||
Bureaucrat(const string name, int grade) |
|||
throw (GradeTooHighException, GradeTooLowException); |
|||
Bureaucrat(Bureaucrat const & src); |
|||
~Bureaucrat(void); |
|||
Bureaucrat & operator= (Bureaucrat const & src); |
|||
|
|||
const string getName(void) const; |
|||
int getGrade(void) const; |
|||
void incrGrade(int diff = 1) |
|||
throw (GradeTooHighException); |
|||
void decrGrade(int diff = 1) |
|||
throw (GradeTooLowException); |
|||
|
|||
}; |
|||
|
|||
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); |
@ -0,0 +1,37 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Form.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Form.hpp" |
|||
|
|||
Form::Form(const string name, const int signGrade, const int exeGrade) |
|||
throw (Form::GradeTooHighException, Form::GradeTooLowException): _name(name){ |
|||
if (signGrade < 1 || exeGrade < 1) |
|||
throw Form::GradeTooHighException; |
|||
if (signGrade > 150 || exeGrade > 150) |
|||
throw Form::GradeTooLowException; |
|||
_signGrade = signGrade; |
|||
_exeGrade = exeGrade; |
|||
} |
|||
|
|||
const string Form::getName(void) const { |
|||
return (_name); |
|||
} |
|||
|
|||
const int Form::getSignGrade(void) const { |
|||
return (_signGrade); |
|||
} |
|||
|
|||
const int Form::getExeGrade(void) const { |
|||
return (_exeGrade); |
|||
} |
|||
|
|||
|
@ -0,0 +1,52 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Form.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Bureaucrat.hpp" |
|||
#include <string> |
|||
#include <iostream> |
|||
#include <stdexcept> |
|||
using std::cout; |
|||
using std::endl; |
|||
using std::string; |
|||
|
|||
class Form { |
|||
const string _name; |
|||
const int _signGrade; |
|||
const int _exeGrade; |
|||
bool _isSigned; |
|||
public: |
|||
class GradeTooHighException: virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw (); |
|||
} |
|||
class GradeTooLowException: virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw (); |
|||
} |
|||
Form(void); |
|||
Form(const string name, const int signGrade,const int exeGrade) |
|||
throw (GradeTooHighException, GradeTooLowException); |
|||
Form & operator=(Form const &f); |
|||
Form(Form const &f); |
|||
~Form(); |
|||
const string getName(void) const; |
|||
const int getSignGrade(void) const; |
|||
const int getExeGrade(void) const; |
|||
beSigned(Bureaucrat); |
|||
} |
|||
|
|||
std::ostream &operator<< (std::ostream &out, const Form &f) { |
|||
out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade(); |
|||
return (out); |
|||
} |
@ -0,0 +1,20 @@ |
|||
NAME = bureau |
|||
SRCS = main.cpp Bureaucrat.cpp Form.cpp |
|||
OBJS= $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
|
|||
.PHONY : all clean fclean re |
@ -0,0 +1,64 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/26 16:34:45 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Form.hpp" |
|||
|
|||
|
|||
void create_too_low(void) { |
|||
try { |
|||
Bureaucrat b("trash", 151); |
|||
} catch (std::exception &e) { |
|||
cout << "Grade 151 creation: " << e.what() << endl; |
|||
} |
|||
} |
|||
|
|||
void create_too_high(void) { |
|||
try { |
|||
Bureaucrat b("god", 0); |
|||
} catch (std::exception &e) { |
|||
cout << "Grade 0 creation: " << e.what() << endl; |
|||
} |
|||
} |
|||
|
|||
void first_evolve(Bureaucrat &b) { |
|||
try { |
|||
b.incrGrade(); |
|||
} catch (std::exception &e) { |
|||
cout << "Grade 1 increase: "<< e.what() << endl; |
|||
} |
|||
} |
|||
|
|||
void last_dismiss(Bureaucrat &b) { |
|||
try { |
|||
b.decrGrade(); |
|||
} catch (std::exception &e) { |
|||
cout << "Grade 150 decrease: "<< e.what() << endl; |
|||
} |
|||
} |
|||
|
|||
|
|||
int main(void) |
|||
{ |
|||
Bureaucrat first("first", 1); |
|||
Bureaucrat last("last", 150); |
|||
|
|||
cout << first; |
|||
cout << last; |
|||
|
|||
create_too_low(); |
|||
create_too_high(); |
|||
first_evolve(first); |
|||
last_dismiss(last); |
|||
|
|||
cout << first; |
|||
cout << last; |
|||
} |
Loading…
Reference in new issue