diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-11-17 13:52:48 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-11-17 13:52:48 +0100 |
| commit | 331c3596660fed6c8b04fdfd0a89435ccffaaf20 (patch) | |
| tree | 4196c94d9084a3d0ed45f3ec798d713639cbfc8a /cpp05/ex00 | |
| parent | 4eecb32ffcf48c42672782fbcd53781a22e95ebf (diff) | |
| download | piscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.tar.gz piscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.tar.bz2 piscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.zip | |
Fixing cpp05/ex00-02
Diffstat (limited to 'cpp05/ex00')
| -rw-r--r-- | cpp05/ex00/Bureaucrat.cpp | 84 | ||||
| -rw-r--r-- | cpp05/ex00/Bureaucrat.hpp | 27 | ||||
| -rw-r--r-- | cpp05/ex00/main.cpp | 25 |
3 files changed, 72 insertions, 64 deletions
diff --git a/cpp05/ex00/Bureaucrat.cpp b/cpp05/ex00/Bureaucrat.cpp index 5737939..927d467 100644 --- a/cpp05/ex00/Bureaucrat.cpp +++ b/cpp05/ex00/Bureaucrat.cpp @@ -6,67 +6,66 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 18:08:13 by charles #+# #+# */ -/* Updated: 2020/10/19 13:26:35 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 10:23:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat.hpp" -Bureaucrat::Bureaucrat(Bureaucrat const& other) -{ - *this = other; -} +Bureaucrat::Bureaucrat(Bureaucrat const& other) { *this = other; } Bureaucrat& Bureaucrat::operator=(Bureaucrat const& other) { + m_name = other.m_name; m_grade = other.m_grade; return *this; } -Bureaucrat::~Bureaucrat() -{} +Bureaucrat::~Bureaucrat() {} Bureaucrat::Bureaucrat(std::string const& name, int grade) : m_name(name), m_grade(grade) { + checkGrade(); } -std::string const& Bureaucrat::getName() const +std::string const& Bureaucrat::getName() const { return m_name; } +int Bureaucrat::getGrade() const { return m_grade; } + +void Bureaucrat::incrementGrade() { - return m_name; + m_grade--; + checkGrade(); } -int Bureaucrat::getGrade() const +void Bureaucrat::decrementGrade() { - return m_grade; + m_grade++; + checkGrade(); } -void Bureaucrat::incrementGrade() +void Bureaucrat::checkGrade() { - if (m_grade <= 1) + if (m_grade > 150) + throw Bureaucrat::GradeTooLowException(); + if (m_grade < 1) throw Bureaucrat::GradeTooHighException(); - else - m_grade--; } -void Bureaucrat::decrementGrade() +std::ostream& operator<<(std::ostream& out, Bureaucrat const& b) { - if (m_grade >= 150) - throw Bureaucrat::GradeTooLowException(); - else - m_grade++; + std::cout << b.getName() << ", bureaucrat grade " << b.getGrade() << std::endl; + return out; } /////////////////////////////////////////////////////////////////////////////// -// Exceptions +// Exception grade too high /////////////////////////////////////////////////////////////////////////////// -Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() -{} +Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() {} Bureaucrat::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other) - : std::exception(other) -{} + : std::exception(other) {} Bureaucrat::GradeTooHighException& Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other) @@ -75,20 +74,18 @@ Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other) return *this; } -Bureaucrat::GradeTooHighException::~GradeTooHighException() -{} +Bureaucrat::GradeTooHighException::~GradeTooHighException() throw() {} -char const* Bureaucrat::GradeTooHighException::what() const throw() -{ - return "Grade is too high"; -} +char const* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high"; } + +/////////////////////////////////////////////////////////////////////////////// +// Exception grade too low +/////////////////////////////////////////////////////////////////////////////// -Bureaucrat::GradeTooLowException::GradeTooLowException() : std::exception() -{} +Bureaucrat::GradeTooLowException::GradeTooLowException() : std::exception() {} Bureaucrat::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other) - : std::exception(other) -{} + : std::exception(other) {} Bureaucrat::GradeTooLowException& Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other) @@ -97,19 +94,8 @@ Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other) return *this; } -Bureaucrat::GradeTooLowException::~GradeTooLowException() -{} - -char const* Bureaucrat::GradeTooLowException::what() const throw() -{ - return "Grade is too low"; -} +Bureaucrat::GradeTooLowException::~GradeTooLowException() throw() {} -Bureaucrat::Bureaucrat() -{} +char const* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low"; } -std::ostream& operator<<(std::ostream& out, Bureaucrat const& b) -{ - std::cout << b.getName() << ", bureaucrat grade " << b.getGrade() << std::endl; - return out; -} +Bureaucrat::Bureaucrat() {} diff --git a/cpp05/ex00/Bureaucrat.hpp b/cpp05/ex00/Bureaucrat.hpp index 7734725..222d219 100644 --- a/cpp05/ex00/Bureaucrat.hpp +++ b/cpp05/ex00/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 18:04:32 by charles #+# #+# */ -/* Updated: 2020/10/19 13:26:06 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 10:28:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,18 @@ public: ~Bureaucrat(); Bureaucrat(std::string const& name, int grade); - std::string const& getName() const; - int getGrade() const; - void incrementGrade(); - void decrementGrade(); + + std::string const& getName() const; + int getGrade() const; + void incrementGrade(); + void decrementGrade(); + +private: + Bureaucrat(); + void checkGrade(); + + std::string m_name; + int m_grade; class GradeTooHighException : public std::exception { @@ -35,7 +43,7 @@ public: GradeTooHighException(); GradeTooHighException(GradeTooHighException const& other); GradeTooHighException& operator=(GradeTooHighException const& other); - ~GradeTooHighException(); + ~GradeTooHighException() throw(); virtual char const* what() const throw(); }; @@ -45,15 +53,10 @@ public: GradeTooLowException(); GradeTooLowException(GradeTooLowException const& other); GradeTooLowException& operator=(GradeTooLowException const& other); - ~GradeTooLowException(); + ~GradeTooLowException() throw(); 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); diff --git a/cpp05/ex00/main.cpp b/cpp05/ex00/main.cpp index ebfed5a..90092cf 100644 --- a/cpp05/ex00/main.cpp +++ b/cpp05/ex00/main.cpp @@ -6,7 +6,7 @@ /* 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 */ +/* Updated: 2020/11/17 11:51:33 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,24 @@ int main() { - Bureaucrat a("jean", 140); - Bureaucrat b("didier", 10); + std::cout << "############### CREATION" << std::endl; + try { Bureaucrat bu("YO", 0); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { Bureaucrat bu("YO2", 151); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + Bureaucrat yep("YEP", 1); + Bureaucrat yep2(yep); + Bureaucrat yep3("SHOULD NOT BE PRINTED", 42); + yep3 = yep; + std::cout << yep; + std::cout << yep2; + std::cout << yep3; + + std::cout << std::endl; + std::cout << "############### DECREMENT" << std::endl; + Bureaucrat a("jean", 140); while (true) { try @@ -31,6 +46,9 @@ int main() } } + std::cout << std::endl; + std::cout << "############### INCREMENT" << std::endl; + Bureaucrat b("didier", 10); while (true) { try @@ -44,5 +62,6 @@ int main() break; } } + return 0; } |
