aboutsummaryrefslogtreecommitdiff
path: root/cpp05
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
parent4eecb32ffcf48c42672782fbcd53781a22e95ebf (diff)
downloadpiscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.tar.gz
piscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.tar.bz2
piscine_cpp-331c3596660fed6c8b04fdfd0a89435ccffaaf20.zip
Fixing cpp05/ex00-02
Diffstat (limited to 'cpp05')
-rw-r--r--cpp05/ex00/Bureaucrat.cpp84
-rw-r--r--cpp05/ex00/Bureaucrat.hpp27
-rw-r--r--cpp05/ex00/main.cpp25
-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
-rw-r--r--cpp05/ex02/Bureaucrat.cpp104
-rw-r--r--cpp05/ex02/Bureaucrat.hpp26
-rw-r--r--cpp05/ex02/Form.cpp98
-rw-r--r--cpp05/ex02/Form.hpp36
-rw-r--r--cpp05/ex02/PresidentialPardonFrom.cpp35
-rw-r--r--cpp05/ex02/PresidentialPardonFrom.hpp9
-rw-r--r--cpp05/ex02/RobotomyRequestForm.cpp38
-rw-r--r--cpp05/ex02/RobotomyRequestForm.hpp9
-rw-r--r--cpp05/ex02/ShrubberyCreationForm.cpp34
-rw-r--r--cpp05/ex02/ShrubberyCreationForm.hpp4
-rw-r--r--cpp05/ex02/main.cpp41
-rw-r--r--cpp05/ex03/Intern.cpp23
-rw-r--r--cpp05/ex03/Intern.hpp28
21 files changed, 620 insertions, 340 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;
}
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;
}
diff --git a/cpp05/ex02/Bureaucrat.cpp b/cpp05/ex02/Bureaucrat.cpp
index c9c206b..64bbd25 100644
--- a/cpp05/ex02/Bureaucrat.cpp
+++ b/cpp05/ex02/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 13:37:49 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,73 @@ 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::executeForm(Form& form)
+{
+ try
+ {
+ form.execute(*this);
+ std::cout << m_name << " executes " << form.getName() << std::endl;
+ }
+ catch (std::exception &e)
+ {
+ std::cout << m_name << " cannot execute " << form.getName() << " " << 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 +100,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 +120,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() : m_name(""), m_grade(0) {}
+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/ex02/Bureaucrat.hpp b/cpp05/ex02/Bureaucrat.hpp
index aabf557..8e44af0 100644
--- a/cpp05/ex02/Bureaucrat.hpp
+++ b/cpp05/ex02/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 13:36:56 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,9 @@
# include <iostream>
# include <exception>
+# include "Form.hpp"
+
+class Form;
class Bureaucrat
{
@@ -25,19 +28,29 @@ 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);
+ void executeForm(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 +60,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/ex02/Form.cpp b/cpp05/ex02/Form.cpp
index 12b0a95..8f3afc0 100644
--- a/cpp05/ex02/Form.cpp
+++ b/cpp05/ex02/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 13:35:10 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,56 @@ 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::execute(Bureaucrat const& executor) const
+{
+ if (executor.getGrade() <= m_gradeExecute && m_signed)
+ executeUnsafe();
+ 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 +103,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 +123,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/ex02/Form.hpp b/cpp05/ex02/Form.hpp
index 3be16f6..a371426 100644
--- a/cpp05/ex02/Form.hpp
+++ b/cpp05/ex02/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 12:56:36 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,27 @@ public:
void beSigned(Bureaucrat const& b);
+ void execute(Bureaucrat const& executor) const;
+
+protected:
+ virtual void executeUnsafe() const = 0;
+
+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 +65,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/ex02/PresidentialPardonFrom.cpp b/cpp05/ex02/PresidentialPardonFrom.cpp
index e69de29..46e2d3c 100644
--- a/cpp05/ex02/PresidentialPardonFrom.cpp
+++ b/cpp05/ex02/PresidentialPardonFrom.cpp
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* PresidentialPardonFrom.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 */
+/* */
+/* ************************************************************************** */
+
+#include "PresidentialPardonFrom.hpp"
+
+PresidentialPardonFrom::PresidentialPardonFrom(std::string const& target)
+ : Form("presidential pardon", 25, 5), m_target(target) {}
+
+PresidentialPardonFrom::PresidentialPardonFrom(const PresidentialPardonFrom& other)
+ : Form(other) { *this = other; }
+
+PresidentialPardonFrom& PresidentialPardonFrom::operator=(const PresidentialPardonFrom& other)
+{
+ Form::operator=(other);
+ m_target = other.m_target;
+ return *this;
+}
+
+PresidentialPardonFrom::~PresidentialPardonFrom() {}
+
+void PresidentialPardonFrom::executeUnsafe() const
+{
+ std::cout << m_target << " has been pardoned by Zafod Beeblebrox" << std::endl;
+}
+
+PresidentialPardonFrom::PresidentialPardonFrom() : Form("", 0, 0) {}
diff --git a/cpp05/ex02/PresidentialPardonFrom.hpp b/cpp05/ex02/PresidentialPardonFrom.hpp
index 7f86d29..3993685 100644
--- a/cpp05/ex02/PresidentialPardonFrom.hpp
+++ b/cpp05/ex02/PresidentialPardonFrom.hpp
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/19 13:42:46 by cacharle #+# #+# */
-/* Updated: 2020/10/19 13:44:33 by cacharle ### ########.fr */
+/* Updated: 2020/11/17 12:57:57 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,12 +18,17 @@
class PresidentialPardonFrom : public Form
{
public:
- PresidentialPardonFrom();
PresidentialPardonFrom(const PresidentialPardonFrom& other);
PresidentialPardonFrom& operator=(const PresidentialPardonFrom& other);
~PresidentialPardonFrom();
+ PresidentialPardonFrom(std::string const& target);
+
private:
+ PresidentialPardonFrom();
+ virtual void executeUnsafe() const;
+
+ std::string m_target;
};
#endif
diff --git a/cpp05/ex02/RobotomyRequestForm.cpp b/cpp05/ex02/RobotomyRequestForm.cpp
index e69de29..113bcd1 100644
--- a/cpp05/ex02/RobotomyRequestForm.cpp
+++ b/cpp05/ex02/RobotomyRequestForm.cpp
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* RobotomyRequestForm.cpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */
+/* Updated: 2020/11/17 13:27:05 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "RobotomyRequestForm.hpp"
+
+RobotomyRequestForm::RobotomyRequestForm(std::string const& target)
+ : Form("robotomy request", 145, 137), m_target(target) {}
+
+RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other)
+ : Form(other) { *this = other; }
+
+RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& other)
+{
+ Form::operator=(other);
+ m_target = other.m_target;
+ return *this;
+}
+
+RobotomyRequestForm::~RobotomyRequestForm() {}
+
+void RobotomyRequestForm::executeUnsafe() const
+{
+ if (rand() % 100 <= 50)
+ std::cout << m_target << " has successfully been robotomized" << std::endl;
+ else
+ std::cout << m_target << " couldn't be robotomized" << std::endl;
+}
+
+RobotomyRequestForm::RobotomyRequestForm() : Form("", 0, 0) {}
diff --git a/cpp05/ex02/RobotomyRequestForm.hpp b/cpp05/ex02/RobotomyRequestForm.hpp
index 5a722de..a851b04 100644
--- a/cpp05/ex02/RobotomyRequestForm.hpp
+++ b/cpp05/ex02/RobotomyRequestForm.hpp
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/19 13:43:37 by cacharle #+# #+# */
-/* Updated: 2020/10/19 13:44:17 by cacharle ### ########.fr */
+/* Updated: 2020/11/17 13:27:32 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,12 +18,17 @@
class RobotomyRequestForm : public Form
{
public:
- RobotomyRequestForm();
RobotomyRequestForm(const RobotomyRequestForm& other);
RobotomyRequestForm& operator=(const RobotomyRequestForm& other);
~RobotomyRequestForm();
+ RobotomyRequestForm(std::string const& target);
+
private:
+ RobotomyRequestForm();
+ virtual void executeUnsafe() const;
+
+ std::string m_target;
};
#endif
diff --git a/cpp05/ex02/ShrubberyCreationForm.cpp b/cpp05/ex02/ShrubberyCreationForm.cpp
index e138824..aa9d5a4 100644
--- a/cpp05/ex02/ShrubberyCreationForm.cpp
+++ b/cpp05/ex02/ShrubberyCreationForm.cpp
@@ -6,26 +6,46 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */
-/* Updated: 2020/10/19 13:55:53 by cacharle ### ########.fr */
+/* Updated: 2020/11/17 13:23:52 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
+ShrubberyCreationForm::ShrubberyCreationForm(std::string const& target)
+ : Form("shrubbery creation", 145, 137), m_target(target) {}
+
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& other)
- : m_target(other.m_target)
-{}
+ : Form(other) { *this = other; }
ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm& other)
{
- Form::operator=(*this, other);
+ Form::operator=(other);
m_target = other.m_target;
return *this;
}
ShrubberyCreationForm::~ShrubberyCreationForm() {}
-ShrubberyCreationForm::ShrubberyCreationForm(std::string const& target)
- m_target(target) {}
+void ShrubberyCreationForm::executeUnsafe() const
+{
+ std::ofstream file(m_target + "_shrubbery");
+ if (!file.is_open())
+ {
+ std::cerr << "Error: " << m_target + "_shrubbery" << ": " << std::strerror(errno) << std::endl;
+ return;
+ }
+ file <<
+ " ## "
+ " #### "
+ " ###### "
+ " ########## "
+ " ############## "
+ " ######ntm####### "
+ " ################### "
+ " ##################### "
+ " |___| ";
+ file.close();
+}
-ShrubberyCreationForm::ShrubberyCreationForm() : m_target("") {}
+ShrubberyCreationForm::ShrubberyCreationForm() : Form("", 0, 0) {}
diff --git a/cpp05/ex02/ShrubberyCreationForm.hpp b/cpp05/ex02/ShrubberyCreationForm.hpp
index 54fe93c..8c3b7e6 100644
--- a/cpp05/ex02/ShrubberyCreationForm.hpp
+++ b/cpp05/ex02/ShrubberyCreationForm.hpp
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/19 13:43:27 by cacharle #+# #+# */
-/* Updated: 2020/10/19 13:46:42 by cacharle ### ########.fr */
+/* Updated: 2020/11/17 13:08:33 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
# define SHRUBBERYCREATIONFORM_HPP
# include <string>
+# include <fstream>
# include "Form.hpp"
class ShrubberyCreationForm : public Form
@@ -27,6 +28,7 @@ public:
private:
ShrubberyCreationForm();
+ virtual void executeUnsafe() const;
std::string m_target;
};
diff --git a/cpp05/ex02/main.cpp b/cpp05/ex02/main.cpp
index ebfed5a..9c14d05 100644
--- a/cpp05/ex02/main.cpp
+++ b/cpp05/ex02/main.cpp
@@ -6,43 +6,28 @@
/* 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 13:38:07 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
+#include <fstream>
#include "Bureaucrat.hpp"
int main()
{
- Bureaucrat a("jean", 140);
- Bureaucrat b("didier", 10);
-
- while (true)
+ int seed;
+ std::ifstream devRandom("/dev/random");
+ if (devRandom.is_open())
{
- try
- {
- a.decrementGrade();
- std::cout << a;
- }
- catch (std::exception& e)
- {
- std::cout << e.what() << std::endl;
- break;
- }
+ devRandom.read((char*)&seed, sizeof(int));
+ devRandom.close();
}
+ else
+ seed = time(NULL);
+ srand(seed);
+
+
+
- while (true)
- {
- try
- {
- b.incrementGrade();
- std::cout << b;
- }
- catch (std::exception& e)
- {
- std::cout << e.what() << std::endl;
- break;
- }
- }
return 0;
}
diff --git a/cpp05/ex03/Intern.cpp b/cpp05/ex03/Intern.cpp
new file mode 100644
index 0000000..bb3e188
--- /dev/null
+++ b/cpp05/ex03/Intern.cpp
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* Intern.cpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/11/17 13:41:09 by cacharle #+# #+# */
+/* Updated: 2020/11/17 13:48:01 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "Intern.hpp"
+
+Intern::Intern() {}
+Intern::Intern(const Intern& other) {}
+Intern& Intern::operator=(const Intern& other) {}
+Intern::~Intern() {}
+
+Form *Intern::makeForm(std::string const& name, std::string const& target)
+{
+
+}
diff --git a/cpp05/ex03/Intern.hpp b/cpp05/ex03/Intern.hpp
new file mode 100644
index 0000000..60c2ac8
--- /dev/null
+++ b/cpp05/ex03/Intern.hpp
@@ -0,0 +1,28 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* Intern.hpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/11/17 13:39:59 by cacharle #+# #+# */
+/* Updated: 2020/11/17 13:48:08 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef INTERN_HPP
+# define INTERN_HPP
+
+class Intern
+{
+public:
+ Intern();
+ Intern(const Intern& other);
+ Intern& operator=(const Intern& other);
+ ~Intern();
+
+ Form *makeForm(std::string const& name, std::string const& target);
+private:
+};
+
+#endif