diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-19 13:59:12 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-19 13:59:12 +0200 |
| commit | c426fe76ec5925e8c94aae3db04e5e1f1ce1585e (patch) | |
| tree | 1cdbbe15c1ed92f0df3b7041550270690575f1a2 /cpp05/ex02 | |
| parent | 60c4c5309af87480fb32f3815bc02031eff43e9b (diff) | |
| download | piscine_cpp-c426fe76ec5925e8c94aae3db04e5e1f1ce1585e.tar.gz piscine_cpp-c426fe76ec5925e8c94aae3db04e5e1f1ce1585e.tar.bz2 piscine_cpp-c426fe76ec5925e8c94aae3db04e5e1f1ce1585e.zip | |
Adding boilerplate cpp05/ex02
Diffstat (limited to 'cpp05/ex02')
| -rw-r--r-- | cpp05/ex02/Bureaucrat.cpp | 115 | ||||
| -rw-r--r-- | cpp05/ex02/Bureaucrat.hpp | 63 | ||||
| -rw-r--r-- | cpp05/ex02/Form.cpp | 108 | ||||
| -rw-r--r-- | cpp05/ex02/Form.hpp | 65 | ||||
| -rw-r--r-- | cpp05/ex02/PresidentialPardonFrom.cpp | 0 | ||||
| -rw-r--r-- | cpp05/ex02/PresidentialPardonFrom.hpp | 29 | ||||
| -rw-r--r-- | cpp05/ex02/RobotomyRequestForm.cpp | 0 | ||||
| -rw-r--r-- | cpp05/ex02/RobotomyRequestForm.hpp | 29 | ||||
| -rw-r--r-- | cpp05/ex02/ShrubberyCreationForm.cpp | 31 | ||||
| -rw-r--r-- | cpp05/ex02/ShrubberyCreationForm.hpp | 34 | ||||
| -rw-r--r-- | cpp05/ex02/main.cpp | 48 |
11 files changed, 522 insertions, 0 deletions
diff --git a/cpp05/ex02/Bureaucrat.cpp b/cpp05/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..c9c206b --- /dev/null +++ b/cpp05/ex02/Bureaucrat.cpp @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:08:13 by charles #+# #+# */ +/* Updated: 2020/10/19 13:30:24 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat(Bureaucrat const& other) +{ + *this = other; +} + +Bureaucrat& Bureaucrat::operator=(Bureaucrat const& other) +{ + m_grade = other.m_grade; + return *this; +} + +Bureaucrat::~Bureaucrat() {} + +Bureaucrat::Bureaucrat(std::string const& name, int grade) + : m_name(name), m_grade(grade) +{} + +std::string const& Bureaucrat::getName() const { return m_name; } + +int Bureaucrat::getGrade() const { return m_grade; } + +void Bureaucrat::incrementGrade() +{ + if (m_grade <= 1) + throw Bureaucrat::GradeTooHighException(); + else + m_grade--; +} + +void Bureaucrat::decrementGrade() +{ + if (m_grade >= 150) + throw Bureaucrat::GradeTooLowException(); + else + m_grade++; +} + +void Bureaucrat::signForm(Form& form) +{ + try + { + form.beSigned(); + std::cout << m_name << " signs " << form << std::endl; + } + catch (std::exception as &e) + std::cout << m_name << " cannot sign " << form + << " because " << e.what() << std::endl; +} + +/////////////////////////////////////////////////////////////////////////////// +// Exceptions +/////////////////////////////////////////////////////////////////////////////// + +Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() +{} + +Bureaucrat::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other) + : std::exception(other) +{} + +Bureaucrat::GradeTooHighException& +Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Bureaucrat::GradeTooHighException::~GradeTooHighException() {} + +char const* Bureaucrat::GradeTooHighException::what() const throw() +{ + return "Grade is too high"; +} + +Bureaucrat::GradeTooLowException::GradeTooLowException() : std::exception() {} + +Bureaucrat::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other) + : std::exception(other) +{} + +Bureaucrat::GradeTooLowException& +Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Bureaucrat::GradeTooLowException::~GradeTooLowException() {} + +char const* Bureaucrat::GradeTooLowException::what() const throw() +{ + return "Grade is too low"; +} + +Bureaucrat::Bureaucrat() : m_name(""), m_grade(0) {} + +std::ostream& operator<<(std::ostream& out, Bureaucrat const& b) +{ + std::cout << b.getName() << ", bureaucrat grade " << b.getGrade() << std::endl; + return out; +} diff --git a/cpp05/ex02/Bureaucrat.hpp b/cpp05/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..aabf557 --- /dev/null +++ b/cpp05/ex02/Bureaucrat.hpp @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:04:32 by charles #+# #+# */ +/* Updated: 2020/10/19 13:23:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUREAUCRAT_HPP +# define BUREAUCRAT_HPP + +# include <iostream> +# include <exception> + +class Bureaucrat +{ +public: + Bureaucrat(Bureaucrat const& other); + Bureaucrat& operator=(Bureaucrat const& other); + ~Bureaucrat(); + + Bureaucrat(std::string const& name, int grade); + + std::string const& getName() const; + int getGrade() const; + void incrementGrade(); + void decrementGrade(); + void signForm(Form& form); + + class GradeTooHighException : public std::exception + { + public: + GradeTooHighException(); + GradeTooHighException(GradeTooHighException const& other); + GradeTooHighException& operator=(GradeTooHighException const& other); + ~GradeTooHighException(); + virtual char const* what() const throw(); + }; + + class GradeTooLowException : public std::exception + { + public: + GradeTooLowException(); + GradeTooLowException(GradeTooLowException const& other); + GradeTooLowException& operator=(GradeTooLowException const& other); + ~GradeTooLowException(); + virtual char const* what() const throw(); + }; + +private: + Bureaucrat(); + + std::string const m_name; + int m_grade; +}; + +std::ostream& operator<<(std::ostream& out, Bureaucrat const& b); + +#endif diff --git a/cpp05/ex02/Form.cpp b/cpp05/ex02/Form.cpp new file mode 100644 index 0000000..12b0a95 --- /dev/null +++ b/cpp05/ex02/Form.cpp @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 19:26:39 by charles #+# #+# */ +/* Updated: 2020/10/19 13:20:29 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" + +Form::Form() + : m_name(""), + m_gradeSign(1), + m_gradeExecute(1) +{} + +Form::Form(Form const& other) + : m_name(other.m_name), + m_signed(other.m_signed), + m_gradeSign(other.m_gradeSign), + m_gradeExecute(other.m_gradeExecute) +{} + +Form& Form::operator=(Form const& other) +{ + m_signed = other.m_signed; + return *this; +} + +Form::~Form() {} + +Form::Form(std::string const& name) + : m_name(name), + m_signed(false), + m_gradeSign(1), + m_gradeExecute(1) +{} + +std::string const& Form::getName() const { return m_name; } +bool Form::getSigned() const { return m_signed; } +int Form::getGradeSign() const { return m_gradeSign; } +int Form::getGradeExecute() const { return m_gradeExecute; } + +void Form::beSigned(Bureaucrat const& b) +{ + if (b.getGrade() >= m_gradeSign) + m_signed = true; +} + +/////////////////////////////////////////////////////////////////////////////// +// Exceptions +/////////////////////////////////////////////////////////////////////////////// + +Form::GradeTooHighException::GradeTooHighException() : std::exception() {} + +Form::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other) + : std::exception(other) +{} + +Form::GradeTooHighException& +Form::GradeTooHighException::operator=(GradeTooHighException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Form::GradeTooHighException::~GradeTooHighException() +{} + +char const* Form::GradeTooHighException::what() const throw() +{ + return "Grade is too high for form"; +} + +Form::GradeTooLowException::GradeTooLowException() : std::exception() +{} + +Form::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other) + : std::exception(other) +{} + +Form::GradeTooLowException& +Form::GradeTooLowException::operator=(GradeTooLowException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Form::GradeTooLowException::~GradeTooLowException() +{} + +char const* Form::GradeTooLowException::what() const throw() +{ + return "Grade is too low for form"; +} + +std::ostream& operator<<(std::ostream& out, Form const& f) +{ + out << f.getName() << " is " + << (f.getSigned() ? "" : "not ") << "signed and needs at least" + << f.getGradeSign() << " to be signed and " + << f.getGradeExecute() << " to be executed" << std::endl; + return out; +} diff --git a/cpp05/ex02/Form.hpp b/cpp05/ex02/Form.hpp new file mode 100644 index 0000000..3be16f6 --- /dev/null +++ b/cpp05/ex02/Form.hpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 19:19:45 by charles #+# #+# */ +/* Updated: 2020/10/19 13:16:01 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FORM_HPP +# define FORM_HPP + +# include "Bureaucrat.hpp" + +class Form +{ +public: + Form(Form const& other); + void operator=(Form const& other); + ~Form(); + + Form(std::string const& name); + + std::string const& getName() const; + bool getSigned() const; + int getGradeSign() const; + int getGradeExecute() const; + + void beSigned(Bureaucrat const& b); + + class GradeTooHighException : public std::exception + { + public: + GradeTooHighException(); + GradeTooHighException(GradeTooHighException const& other); + GradeTooHighException& operator=(GradeTooHighException const& other); + ~GradeTooHighException(); + virtual char const* what() const throw(); + }; + + class GradeTooLowException : public std::exception + { + public: + GradeTooLowException(); + GradeTooLowException(GradeTooLowException const& other); + GradeTooLowException& operator=(GradeTooLowException const& other); + ~GradeTooLowException(); + virtual char const* what() const throw(); + }; + +private: + Form(); + + std::string const m_name; + bool m_signed; + int const m_gradeSign; + int const m_gradeExecute; +}; + +std::ostream& operator<<(std::ostream& out, Form const& f); + +#endif diff --git a/cpp05/ex02/PresidentialPardonFrom.cpp b/cpp05/ex02/PresidentialPardonFrom.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/cpp05/ex02/PresidentialPardonFrom.cpp diff --git a/cpp05/ex02/PresidentialPardonFrom.hpp b/cpp05/ex02/PresidentialPardonFrom.hpp new file mode 100644 index 0000000..7f86d29 --- /dev/null +++ b/cpp05/ex02/PresidentialPardonFrom.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonFrom.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:42:46 by cacharle #+# #+# */ +/* Updated: 2020/10/19 13:44:33 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PRESIDENTIALPARDONFROM_HPP +# define PRESIDENTIALPARDONFROM_HPP + +# include "Form.hpp" + +class PresidentialPardonFrom : public Form +{ +public: + PresidentialPardonFrom(); + PresidentialPardonFrom(const PresidentialPardonFrom& other); + PresidentialPardonFrom& operator=(const PresidentialPardonFrom& other); + ~PresidentialPardonFrom(); + +private: +}; + +#endif diff --git a/cpp05/ex02/RobotomyRequestForm.cpp b/cpp05/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/cpp05/ex02/RobotomyRequestForm.cpp diff --git a/cpp05/ex02/RobotomyRequestForm.hpp b/cpp05/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..5a722de --- /dev/null +++ b/cpp05/ex02/RobotomyRequestForm.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:43:37 by cacharle #+# #+# */ +/* Updated: 2020/10/19 13:44:17 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ROBOTOMYREQUESTFORM_HPP +# define ROBOTOMYREQUESTFORM_HPP + +# include "Form.hpp" + +class RobotomyRequestForm : public Form +{ +public: + RobotomyRequestForm(); + RobotomyRequestForm(const RobotomyRequestForm& other); + RobotomyRequestForm& operator=(const RobotomyRequestForm& other); + ~RobotomyRequestForm(); + +private: +}; + +#endif diff --git a/cpp05/ex02/ShrubberyCreationForm.cpp b/cpp05/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..e138824 --- /dev/null +++ b/cpp05/ex02/ShrubberyCreationForm.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ +/* Updated: 2020/10/19 13:55:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" + +ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& other) + : m_target(other.m_target) +{} + +ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm& other) +{ + Form::operator=(*this, other); + m_target = other.m_target; + return *this; +} + +ShrubberyCreationForm::~ShrubberyCreationForm() {} + +ShrubberyCreationForm::ShrubberyCreationForm(std::string const& target) + m_target(target) {} + +ShrubberyCreationForm::ShrubberyCreationForm() : m_target("") {} diff --git a/cpp05/ex02/ShrubberyCreationForm.hpp b/cpp05/ex02/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..54fe93c --- /dev/null +++ b/cpp05/ex02/ShrubberyCreationForm.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:43:27 by cacharle #+# #+# */ +/* Updated: 2020/10/19 13:46:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SHRUBBERYCREATIONFORM_HPP +# define SHRUBBERYCREATIONFORM_HPP + +# include <string> +# include "Form.hpp" + +class ShrubberyCreationForm : public Form +{ +public: + ShrubberyCreationForm(const ShrubberyCreationForm& other); + ShrubberyCreationForm& operator=(const ShrubberyCreationForm& other); + ~ShrubberyCreationForm(); + + ShrubberyCreationForm(std::string const& target); + +private: + ShrubberyCreationForm(); + + std::string m_target; +}; + +#endif diff --git a/cpp05/ex02/main.cpp b/cpp05/ex02/main.cpp new file mode 100644 index 0000000..ebfed5a --- /dev/null +++ b/cpp05/ex02/main.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:13:05 by charles #+# #+# */ +/* Updated: 2020/04/14 18:43:15 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +int main() +{ + Bureaucrat a("jean", 140); + Bureaucrat b("didier", 10); + + while (true) + { + try + { + a.decrementGrade(); + std::cout << a; + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + break; + } + } + + while (true) + { + try + { + b.incrementGrade(); + std::cout << b; + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + break; + } + } + return 0; +} |
