From 7ac812044bfe771178752a52d70b18bb297c1891 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 14 Apr 2020 20:42:26 +0200 Subject: cpp07 done, cpp06 ex00 and ex01 start --- cpp05/ex00/Bureaucrat.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++ cpp05/ex00/Bureaucrat.hpp | 61 ++++++++++++++++++++++++++ cpp05/ex00/main.cpp | 48 +++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 cpp05/ex00/Bureaucrat.cpp create mode 100644 cpp05/ex00/Bureaucrat.hpp create mode 100644 cpp05/ex00/main.cpp (limited to 'cpp05/ex00') diff --git a/cpp05/ex00/Bureaucrat.cpp b/cpp05/ex00/Bureaucrat.cpp new file mode 100644 index 0000000..69ca053 --- /dev/null +++ b/cpp05/ex00/Bureaucrat.cpp @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:08:13 by charles #+# #+# */ +/* Updated: 2020/04/14 18:44:49 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat(Bureaucrat const& other) +{ + *this = other; +} + +void Bureaucrat::operator=(Bureaucrat const& other) +{ + m_grade = other.m_grade; +} + +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++; +} + +Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() +{} + +Bureaucrat::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other) + : std::exception(other) +{} + +void Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other) +{ + std::exception::operator=(other); +} + +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) +{} + +void Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other) +{ + std::exception::operator=(other); +} + +Bureaucrat::GradeTooLowException::~GradeTooLowException() +{} + +char const* Bureaucrat::GradeTooLowException::what() const throw() +{ + return "Grade is too low"; +} + +Bureaucrat::Bureaucrat() +{} + +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/ex00/Bureaucrat.hpp b/cpp05/ex00/Bureaucrat.hpp new file mode 100644 index 0000000..8cb328e --- /dev/null +++ b/cpp05/ex00/Bureaucrat.hpp @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:04:32 by charles #+# #+# */ +/* Updated: 2020/04/14 18:42:13 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUREAUCRAT_HPP +# define BUREAUCRAT_HPP + +# include +# include + +class Bureaucrat +{ +public: + Bureaucrat(Bureaucrat const& other); + void operator=(Bureaucrat const& other); + ~Bureaucrat(); + + Bureaucrat(std::string const& name, int grade); + std::string const& getName() const; + int getGrade() const; + void incrementGrade(); + void decrementGrade(); + + class GradeTooHighException : public std::exception + { + public: + GradeTooHighException(); + GradeTooHighException(GradeTooHighException const& other); + void operator=(GradeTooHighException const& other); + ~GradeTooHighException(); + virtual char const* what() const throw(); + }; + + class GradeTooLowException : public std::exception + { + public: + GradeTooLowException(); + GradeTooLowException(GradeTooLowException const& other); + void 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/ex00/main.cpp b/cpp05/ex00/main.cpp new file mode 100644 index 0000000..ebfed5a --- /dev/null +++ b/cpp05/ex00/main.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} -- cgit