diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-11-17 19:02:35 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-11-17 19:02:35 +0100 |
| commit | 452e6bfa7bb4bca75dc4659bf9d707101b411977 (patch) | |
| tree | f4ee27a96437d6d17a89bbbbc135ae45f9f3faae /cpp05/ex02 | |
| parent | a92013c92bfcd50b0e2561280c9eaa604843ade0 (diff) | |
| download | piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.tar.gz piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.tar.bz2 piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.zip | |
Added cpp05/ex02 main, Added cpp05/ex03
Diffstat (limited to 'cpp05/ex02')
| -rw-r--r-- | cpp05/ex02/Form.cpp | 26 | ||||
| -rw-r--r-- | cpp05/ex02/Form.hpp | 13 | ||||
| -rw-r--r-- | cpp05/ex02/PresidentialPardonForm.cpp (renamed from cpp05/ex02/PresidentialPardonFrom.cpp) | 18 | ||||
| -rw-r--r-- | cpp05/ex02/PresidentialPardonForm.hpp (renamed from cpp05/ex02/PresidentialPardonFrom.hpp) | 20 | ||||
| -rw-r--r-- | cpp05/ex02/RobotomyRequestForm.cpp | 4 | ||||
| -rw-r--r-- | cpp05/ex02/ShrubberyCreationForm.cpp | 20 | ||||
| -rw-r--r-- | cpp05/ex02/main.cpp | 158 |
7 files changed, 212 insertions, 47 deletions
diff --git a/cpp05/ex02/Form.cpp b/cpp05/ex02/Form.cpp index 8f3afc0..9208f7f 100644 --- a/cpp05/ex02/Form.cpp +++ b/cpp05/ex02/Form.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 19:26:39 by charles #+# #+# */ -/* Updated: 2020/11/17 13:35:10 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:09:53 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,9 @@ void Form::beSigned(Bureaucrat const& b) void Form::execute(Bureaucrat const& executor) const { - if (executor.getGrade() <= m_gradeExecute && m_signed) + if (!m_signed) + throw Form::NoSignatureException(); + if (executor.getGrade() <= m_gradeExecute) executeUnsafe(); else throw Form::GradeTooLowException(); @@ -126,3 +128,23 @@ Form::GradeTooLowException::operator=(GradeTooLowException const& other) Form::GradeTooLowException::~GradeTooLowException() throw() {} char const* Form::GradeTooLowException::what() const throw() { return "Grade is too low for form"; } + +/////////////////////////////////////////////////////////////////////////////// +// Exception signature +/////////////////////////////////////////////////////////////////////////////// + +Form::NoSignatureException::NoSignatureException() : std::exception() {} + +Form::NoSignatureException::NoSignatureException(NoSignatureException const& other) + : std::exception(other) {} + +Form::NoSignatureException& +Form::NoSignatureException::operator=(NoSignatureException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Form::NoSignatureException::~NoSignatureException() throw() {} + +char const* Form::NoSignatureException::what() const throw() { return "No signature"; } diff --git a/cpp05/ex02/Form.hpp b/cpp05/ex02/Form.hpp index a371426..78828da 100644 --- a/cpp05/ex02/Form.hpp +++ b/cpp05/ex02/Form.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 19:19:45 by charles #+# #+# */ -/* Updated: 2020/11/17 12:56:36 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:09:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,6 +68,17 @@ private: ~GradeTooLowException() throw(); virtual char const* what() const throw(); }; + + class NoSignatureException : public std::exception + { + public: + NoSignatureException(); + NoSignatureException(NoSignatureException const& other); + NoSignatureException& operator=(NoSignatureException const& other); + ~NoSignatureException() throw(); + virtual char const* what() const throw(); + }; + }; std::ostream& operator<<(std::ostream& out, Form const& f); diff --git a/cpp05/ex02/PresidentialPardonFrom.cpp b/cpp05/ex02/PresidentialPardonForm.cpp index 46e2d3c..3855841 100644 --- a/cpp05/ex02/PresidentialPardonFrom.cpp +++ b/cpp05/ex02/PresidentialPardonForm.cpp @@ -1,35 +1,35 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* PresidentialPardonFrom.cpp :+: :+: :+: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/17 13:01:25 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:04:10 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:02:14 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "PresidentialPardonFrom.hpp" +#include "PresidentialPardonForm.hpp" -PresidentialPardonFrom::PresidentialPardonFrom(std::string const& target) +PresidentialPardonForm::PresidentialPardonForm(std::string const& target) : Form("presidential pardon", 25, 5), m_target(target) {} -PresidentialPardonFrom::PresidentialPardonFrom(const PresidentialPardonFrom& other) +PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other) : Form(other) { *this = other; } -PresidentialPardonFrom& PresidentialPardonFrom::operator=(const PresidentialPardonFrom& other) +PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& other) { Form::operator=(other); m_target = other.m_target; return *this; } -PresidentialPardonFrom::~PresidentialPardonFrom() {} +PresidentialPardonForm::~PresidentialPardonForm() {} -void PresidentialPardonFrom::executeUnsafe() const +void PresidentialPardonForm::executeUnsafe() const { std::cout << m_target << " has been pardoned by Zafod Beeblebrox" << std::endl; } -PresidentialPardonFrom::PresidentialPardonFrom() : Form("", 0, 0) {} +PresidentialPardonForm::PresidentialPardonForm() : Form("", 0, 0) {} diff --git a/cpp05/ex02/PresidentialPardonFrom.hpp b/cpp05/ex02/PresidentialPardonForm.hpp index 3993685..fcf8a70 100644 --- a/cpp05/ex02/PresidentialPardonFrom.hpp +++ b/cpp05/ex02/PresidentialPardonForm.hpp @@ -1,31 +1,31 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* PresidentialPardonFrom.hpp :+: :+: :+: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:42:46 by cacharle #+# #+# */ -/* Updated: 2020/11/17 12:57:57 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:02:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef PRESIDENTIALPARDONFROM_HPP -# define PRESIDENTIALPARDONFROM_HPP +#ifndef PRESIDENTIALPARDONFORM_HPP +# define PRESIDENTIALPARDONFORM_HPP # include "Form.hpp" -class PresidentialPardonFrom : public Form +class PresidentialPardonForm : public Form { public: - PresidentialPardonFrom(const PresidentialPardonFrom& other); - PresidentialPardonFrom& operator=(const PresidentialPardonFrom& other); - ~PresidentialPardonFrom(); + PresidentialPardonForm(const PresidentialPardonForm& other); + PresidentialPardonForm& operator=(const PresidentialPardonForm& other); + ~PresidentialPardonForm(); - PresidentialPardonFrom(std::string const& target); + PresidentialPardonForm(std::string const& target); private: - PresidentialPardonFrom(); + PresidentialPardonForm(); virtual void executeUnsafe() const; std::string m_target; diff --git a/cpp05/ex02/RobotomyRequestForm.cpp b/cpp05/ex02/RobotomyRequestForm.cpp index 113bcd1..ed05d5f 100644 --- a/cpp05/ex02/RobotomyRequestForm.cpp +++ b/cpp05/ex02/RobotomyRequestForm.cpp @@ -6,14 +6,14 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:27:05 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:20:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "RobotomyRequestForm.hpp" RobotomyRequestForm::RobotomyRequestForm(std::string const& target) - : Form("robotomy request", 145, 137), m_target(target) {} + : Form("robotomy request", 72, 45), m_target(target) {} RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other) : Form(other) { *this = other; } diff --git a/cpp05/ex02/ShrubberyCreationForm.cpp b/cpp05/ex02/ShrubberyCreationForm.cpp index 70bc570..262f166 100644 --- a/cpp05/ex02/ShrubberyCreationForm.cpp +++ b/cpp05/ex02/ShrubberyCreationForm.cpp @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ -/* Updated: 2020/11/17 16:36:09 by charles ### ########.fr */ +/* Updated: 2020/11/17 17:15:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,15 +36,15 @@ void ShrubberyCreationForm::executeUnsafe() const return; } file << - " ## " - " #### " - " ###### " - " ########## " - " ############## " - " ######ntm####### " - " ################### " - " ##################### " - " |___| "; + " ## \n" + " #### \n" + " ###### \n" + " ########## \n" + " ############## \n" + " ######ntm####### \n" + " ################### \n" + " ##################### \n" + " |___| \n"; file.close(); } diff --git a/cpp05/ex02/main.cpp b/cpp05/ex02/main.cpp index ae272c9..0f3a893 100644 --- a/cpp05/ex02/main.cpp +++ b/cpp05/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 18:13:05 by charles #+# #+# */ -/* Updated: 2020/11/17 16:35:40 by charles ### ########.fr */ +/* Updated: 2020/11/17 17:50:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,9 @@ #include <ctime> #include <cstdlib> #include "Bureaucrat.hpp" +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" int main() { @@ -78,32 +81,161 @@ int main() } } - // Bureaucrat c("baraucrat", 10); - // std::cout << "############### SIGN FORM" << std::endl; - // Form f4("foo", 150, 1); c.signForm(f4); - // Form f3("foo", 10, 1); c.signForm(f3); - // Form f2("foo", 9, 1); c.signForm(f2); - // Form f1("foo", 1, 1); c.signForm(f1); - } + { + std::cout << "############### SHRUBERRY SIGN FORM" << std::endl; + ShrubberyCreationForm sh("shi"); + Bureaucrat c1("foo", 150); c1.signForm(sh); + Bureaucrat c2("foo", 146); c2.signForm(sh); + Bureaucrat c3("foo", 145); c3.signForm(sh); + Bureaucrat c4("foo", 1); c4.signForm(sh); + } + { + std::cout << "############### PRESIDENTIAL SIGN FORM" << std::endl; + PresidentialPardonForm pr("fu"); + Bureaucrat c1("foo", 150); c1.signForm(pr); + Bureaucrat c2("foo", 26); c2.signForm(pr); + Bureaucrat c3("foo", 25); c3.signForm(pr); + Bureaucrat c4("foo", 1); c4.signForm(pr); + } + { + std::cout << "############### ROBOTOMY SIGN FORM" << std::endl; + RobotomyRequestForm ro("mi"); + Bureaucrat c1("foo", 150); c1.signForm(ro); + Bureaucrat c2("foo", 73); c2.signForm(ro); + Bureaucrat c3("foo", 72); c3.signForm(ro); + Bureaucrat c4("foo", 1); c4.signForm(ro); + } + { + std::cout << "############### SHRUBERRY EXECUTE FORM" << std::endl; + ShrubberyCreationForm sh("shi"); + sh.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(sh); + Bureaucrat c2("foo", 138); c2.executeForm(sh); + Bureaucrat c3("foo", 137); c3.executeForm(sh); + Bureaucrat c4("foo", 1); c4.executeForm(sh); + } + { + std::cout << "############### PRESIDENTIAL SIGN FORM" << std::endl; + PresidentialPardonForm pr("fu"); + pr.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(pr); + Bureaucrat c2("foo", 6); c2.executeForm(pr); + Bureaucrat c3("foo", 5); c3.executeForm(pr); + Bureaucrat c4("foo", 1); c4.executeForm(pr); + } + { + std::cout << "############### ROBOTOMY SIGN FORM" << std::endl; + RobotomyRequestForm ro("mi"); + ro.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(ro); + Bureaucrat c2("foo", 46); c2.executeForm(ro); + Bureaucrat c3("foo", 45); c3.executeForm(ro); + Bureaucrat c4("foo", 1); c4.executeForm(ro); + } + } + std::cout << std::endl; { - std::cout << "================= SHRUBERRY CREATION =================" << std::endl; - + std::cout << "================= SHRUBBERY CREATION =================" << std::endl; + ShrubberyCreationForm sh("home"); + ShrubberyCreationForm sh2(sh); + ShrubberyCreationForm sh3("SHOULD NOT BE PRINTED"); + sh3 = sh; + std::cout << sh; + std::cout << sh2; + std::cout << sh3; + + try { sh.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { sh.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 145)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 146)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << sh; + + try { sh.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 137)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 138)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } std::cout << std::endl; - { std::cout << "================= PRESIDENTIAL PARDON =================" << std::endl; + PresidentialPardonForm pr("Didier"); + PresidentialPardonForm pr2(pr); + PresidentialPardonForm pr3("SHOULD NOT BE PRINTED"); + pr3 = pr; + std::cout << pr; + std::cout << pr2; + std::cout << pr3; + + try { pr.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { pr.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 25)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 26)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << pr; + + try { pr.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 5)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 6)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } std::cout << std::endl; - { std::cout << "================= ROBOTOMY REQUEST =================" << std::endl; + RobotomyRequestForm ro("Jonathan"); + RobotomyRequestForm ro2(ro); + RobotomyRequestForm ro3("SHOULD NOT BE PRINTED"); + ro3 = ro; + std::cout << ro; + std::cout << ro2; + std::cout << ro3; + + try { ro.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { ro.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 72)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 73)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << ro; + + try { ro.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 45)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 46)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } - return 0; } |
