aboutsummaryrefslogtreecommitdiff
path: root/cpp05/ex01
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-11-17 13:52:48 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-11-17 13:52:48 +0100
commit331c3596660fed6c8b04fdfd0a89435ccffaaf20 (patch)
tree4196c94d9084a3d0ed45f3ec798d713639cbfc8a /cpp05/ex01
parent4eecb32ffcf48c42672782fbcd53781a22e95ebf (diff)
downloadpiscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.tar.gz
piscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.tar.bz2
piscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.zip
Fixing cpp05/ex00-02
Diffstat (limited to 'cpp05/ex01')
-rw-r--r--cpp05/ex01/Bureaucrat.cpp91
-rw-r--r--cpp05/ex01/Bureaucrat.hpp24
-rw-r--r--cpp05/ex01/Form.cpp90
-rw-r--r--cpp05/ex01/Form.hpp31
-rw-r--r--cpp05/ex01/main.cpp103
5 files changed, 210 insertions, 129 deletions
diff --git a/cpp05/ex01/Bureaucrat.cpp b/cpp05/ex01/Bureaucrat.cpp
index c9c206b..1024d54 100644
--- a/cpp05/ex01/Bureaucrat.cpp
+++ b/cpp05/ex01/Bureaucrat.cpp
@@ -6,19 +6,17 @@
/* 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 */
+/* Updated: 2020/11/17 12:08:02 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;
}
@@ -27,50 +25,60 @@ 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; }
+{
+ checkGrade();
+}
-int Bureaucrat::getGrade() const { return m_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--;
+ m_grade--;
+ checkGrade();
}
void Bureaucrat::decrementGrade()
{
- if (m_grade >= 150)
- throw Bureaucrat::GradeTooLowException();
- else
- m_grade++;
+ m_grade++;
+ checkGrade();
}
void Bureaucrat::signForm(Form& form)
{
try
{
- form.beSigned();
- std::cout << m_name << " signs " << form << std::endl;
+ form.beSigned(*this);
+ std::cout << m_name << " signs " << form.getName() << std::endl;
+ }
+ catch (std::exception &e)
+ {
+ std::cout << m_name << " cannot sign " << form.getName() << " " << e.what() << std::endl;
}
- catch (std::exception as &e)
- std::cout << m_name << " cannot sign " << form
- << " because " << e.what() << std::endl;
+}
+
+void Bureaucrat::checkGrade()
+{
+ if (m_grade > 150)
+ throw Bureaucrat::GradeTooLowException();
+ if (m_grade < 1)
+ throw Bureaucrat::GradeTooHighException();
+}
+
+std::ostream& operator<<(std::ostream& out, Bureaucrat const& b)
+{
+ 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)
@@ -79,18 +87,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(GradeTooLowException const& other)
- : std::exception(other)
-{}
+ : std::exception(other) {}
Bureaucrat::GradeTooLowException&
Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other)
@@ -99,17 +107,8 @@ Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other)
return *this;
}
-Bureaucrat::GradeTooLowException::~GradeTooLowException() {}
+Bureaucrat::GradeTooLowException::~GradeTooLowException() throw() {}
-char const* Bureaucrat::GradeTooLowException::what() const throw()
-{
- return "Grade is too low";
-}
+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;
-}
+Bureaucrat::Bureaucrat() {}
diff --git a/cpp05/ex01/Bureaucrat.hpp b/cpp05/ex01/Bureaucrat.hpp
index aabf557..1886a76 100644
--- a/cpp05/ex01/Bureaucrat.hpp
+++ b/cpp05/ex01/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:23:52 by cacharle ### ########.fr */
+/* Updated: 2020/11/17 11:32:12 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,9 @@
# include <iostream>
# include <exception>
+# include "Form.hpp"
+
+class Form;
class Bureaucrat
{
@@ -25,19 +28,27 @@ public:
Bureaucrat(std::string const& name, int grade);
- std::string const& getName() const;
+ std::string const& getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
+
void signForm(Form& form);
+private:
+ Bureaucrat();
+ void checkGrade();
+
+ std::string m_name;
+ int m_grade;
+
class GradeTooHighException : public std::exception
{
public:
GradeTooHighException();
GradeTooHighException(GradeTooHighException const& other);
GradeTooHighException& operator=(GradeTooHighException const& other);
- ~GradeTooHighException();
+ ~GradeTooHighException() throw();
virtual char const* what() const throw();
};
@@ -47,15 +58,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/ex01/Form.cpp b/cpp05/ex01/Form.cpp
index 12b0a95..254772a 100644
--- a/cpp05/ex01/Form.cpp
+++ b/cpp05/ex01/Form.cpp
@@ -6,17 +6,12 @@
/* 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 */
+/* Updated: 2020/11/17 12:10:55 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),
@@ -25,6 +20,7 @@ Form::Form(Form const& other)
m_gradeExecute(other.m_gradeExecute)
{}
+// The other attributes are const (asked by subject)
Form& Form::operator=(Form const& other)
{
m_signed = other.m_signed;
@@ -33,12 +29,14 @@ Form& Form::operator=(Form const& other)
Form::~Form() {}
-Form::Form(std::string const& name)
+Form::Form(std::string const& name, int gradeSign, int gradeExecute)
: m_name(name),
m_signed(false),
- m_gradeSign(1),
- m_gradeExecute(1)
-{}
+ m_gradeSign(gradeSign),
+ m_gradeExecute(gradeExecute)
+{
+ checkGrade();
+}
std::string const& Form::getName() const { return m_name; }
bool Form::getSigned() const { return m_signed; }
@@ -47,19 +45,48 @@ int Form::getGradeExecute() const { return m_gradeExecute; }
void Form::beSigned(Bureaucrat const& b)
{
- if (b.getGrade() >= m_gradeSign)
+ if (b.getGrade() <= m_gradeSign)
m_signed = true;
+ else
+ throw Form::GradeTooLowException();
}
+void Form::checkGrade()
+{
+ if (m_gradeSign > 150)
+ throw Form::GradeTooLowException();
+ if (m_gradeSign < 1)
+ throw Form::GradeTooHighException();
+ if (m_gradeExecute > 150)
+ throw Form::GradeTooLowException();
+ if (m_gradeExecute < 1)
+ throw Form::GradeTooHighException();
+}
+
+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;
+}
+// compilation error if const members are not initialized
+Form::Form()
+ : m_name(""),
+ m_signed(false),
+ m_gradeSign(0),
+ m_gradeExecute(0)
+{}
+
///////////////////////////////////////////////////////////////////////////////
-// Exceptions
+// Exception grade too high
///////////////////////////////////////////////////////////////////////////////
Form::GradeTooHighException::GradeTooHighException() : std::exception() {}
Form::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other)
- : std::exception(other)
-{}
+ : std::exception(other) {}
Form::GradeTooHighException&
Form::GradeTooHighException::operator=(GradeTooHighException const& other)
@@ -68,20 +95,18 @@ Form::GradeTooHighException::operator=(GradeTooHighException const& other)
return *this;
}
-Form::GradeTooHighException::~GradeTooHighException()
-{}
+Form::GradeTooHighException::~GradeTooHighException() throw() {}
-char const* Form::GradeTooHighException::what() const throw()
-{
- return "Grade is too high for form";
-}
+char const* Form::GradeTooHighException::what() const throw() { return "Grade is too high for form"; }
-Form::GradeTooLowException::GradeTooLowException() : std::exception()
-{}
+///////////////////////////////////////////////////////////////////////////////
+// Exception grade too high
+///////////////////////////////////////////////////////////////////////////////
+
+Form::GradeTooLowException::GradeTooLowException() : std::exception() {}
Form::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other)
- : std::exception(other)
-{}
+ : std::exception(other) {}
Form::GradeTooLowException&
Form::GradeTooLowException::operator=(GradeTooLowException const& other)
@@ -90,19 +115,6 @@ Form::GradeTooLowException::operator=(GradeTooLowException const& other)
return *this;
}
-Form::GradeTooLowException::~GradeTooLowException()
-{}
-
-char const* Form::GradeTooLowException::what() const throw()
-{
- return "Grade is too low for form";
-}
+Form::GradeTooLowException::~GradeTooLowException() throw() {}
-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;
-}
+char const* Form::GradeTooLowException::what() const throw() { return "Grade is too low for form"; }
diff --git a/cpp05/ex01/Form.hpp b/cpp05/ex01/Form.hpp
index 3be16f6..1ac76d1 100644
--- a/cpp05/ex01/Form.hpp
+++ b/cpp05/ex01/Form.hpp
@@ -6,23 +6,27 @@
/* 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 */
+/* Updated: 2020/11/17 11:39:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FORM_HPP
# define FORM_HPP
+# include <iostream>
+# include <exception>
# include "Bureaucrat.hpp"
+class Bureaucrat;
+
class Form
{
public:
Form(Form const& other);
- void operator=(Form const& other);
+ Form& operator=(Form const& other);
~Form();
- Form(std::string const& name);
+ Form(std::string const& name, int gradeSign, int gradeExecute);
std::string const& getName() const;
bool getSigned() const;
@@ -31,13 +35,22 @@ public:
void beSigned(Bureaucrat const& b);
+private:
+ Form();
+ void checkGrade();
+
+ std::string const m_name;
+ bool m_signed;
+ int const m_gradeSign;
+ int const m_gradeExecute;
+
class GradeTooHighException : public std::exception
{
public:
GradeTooHighException();
GradeTooHighException(GradeTooHighException const& other);
GradeTooHighException& operator=(GradeTooHighException const& other);
- ~GradeTooHighException();
+ ~GradeTooHighException() throw();
virtual char const* what() const throw();
};
@@ -47,17 +60,9 @@ public:
GradeTooLowException();
GradeTooLowException(GradeTooLowException const& other);
GradeTooLowException& operator=(GradeTooLowException const& other);
- ~GradeTooLowException();
+ ~GradeTooLowException() throw();
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);
diff --git a/cpp05/ex01/main.cpp b/cpp05/ex01/main.cpp
index ebfed5a..3200b0f 100644
--- a/cpp05/ex01/main.cpp
+++ b/cpp05/ex01/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 12:10:38 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,35 +14,94 @@
int main()
{
- Bureaucrat a("jean", 140);
- Bureaucrat b("didier", 10);
-
- while (true)
{
- try
+ std::cout << "=============== BUREAUCRAT ===============" << std::endl;
+ 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 << "############### DECREMENT" << std::endl;
+ Bureaucrat a("jean", 140);
+ while (true)
{
- a.decrementGrade();
- std::cout << a;
+ try
+ {
+ a.decrementGrade();
+ std::cout << a;
+ }
+ catch (std::exception& e)
+ {
+ std::cout << e.what() << std::endl;
+ break;
+ }
}
- catch (std::exception& e)
+
+ std::cout << "############### INCREMENT" << std::endl;
+ Bureaucrat b("didier", 10);
+ while (true)
{
- std::cout << e.what() << std::endl;
- break;
+ try
+ {
+ b.incrementGrade();
+ std::cout << b;
+ }
+ catch (std::exception& e)
+ {
+ std::cout << e.what() << std::endl;
+ break;
+ }
}
+
+ 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);
}
- while (true)
+ std::cout << std::endl;
+
{
- try
- {
- b.incrementGrade();
- std::cout << b;
- }
- catch (std::exception& e)
- {
- std::cout << e.what() << std::endl;
- break;
- }
+ std::cout << "=============== FORM ===============" << std::endl;
+ Form f("FormBonjour", 32, 42);
+ std::cout << f;
+ Form f2(f);
+ std::cout << f2;
+ Form f3("Const", 1, 2);
+ f3 = f;
+ std::cout << f3;
+
+ try { Form f("YO", 0, 1); }
+ catch (std::exception& e) { std::cout << e.what() << std::endl; }
+ try { Form f("YO2", 151, 1); }
+ catch (std::exception& e) { std::cout << e.what() << std::endl; }
+ try { Form f("YO", 1, 0); }
+ catch (std::exception& e) { std::cout << e.what() << std::endl; }
+ try { Form f("YO2", 1, 151); }
+ catch (std::exception& e) { std::cout << e.what() << std::endl; }
+
+ std::cout << "############### BE SIGNED" << std::endl;
+ try { f.beSigned(Bureaucrat("foo", 1)); std::cout << f; }
+ catch (std::exception& e) { std::cout << "SHOULD NO PRINT " << e.what() << std::endl; }
+ try { f.beSigned(Bureaucrat("foo", 32)); std::cout << f; }
+ catch (std::exception& e) { std::cout << "SHOULD NO PRINT " << e.what() << std::endl; }
+
+ try { f.beSigned(Bureaucrat("foo", 33)); }
+ catch (std::exception& e) { std::cout << e.what() << std::endl; }
+ try { f.beSigned(Bureaucrat("foo", 150)); }
+ catch (std::exception& e) { std::cout << e.what() << std::endl; }
}
+
return 0;
}