ex03 finished

This commit is contained in:
aortigos
2026-02-18 21:28:38 +01:00
parent 7539e90e6a
commit 57eefd3b37
3 changed files with 73 additions and 11 deletions

View File

@@ -23,16 +23,13 @@ Intern::Intern()
Intern::Intern(const Intern &other)
{
*this = other;
(void)other; // No atributes for copying
// std::cout << "Intern copy constructor called" << std::endl;
}
Intern& Intern::operator=(const Intern &other)
{
if (this != &other)
{
// Copy attributes here
}
(void)other; // No atributes for copying
// std::cout << "Intern copy assignment operator called" << std::endl;
return (*this);
}
@@ -41,3 +38,54 @@ Intern::~Intern()
{
// std::cout << "Intern destructor called" << std::endl;
}
//////////////////
// Creators //
//////////////////
AForm* Intern::createShrubbery(const std::string &target)
{
return new ShrubberyCreationForm(target);
}
AForm* Intern::createRobotomy(const std::string &target)
{
return new RobotomyRequestForm(target);
}
AForm* Intern::createPardon(const std::string &target)
{
return new PresidentialPardonForm(target);
}
AForm* Intern::makeForm(const std::string &formName, const std::string &target)
{
std::string formNames[3] = {
"shrubbery creation",
"robotomy request",
"presidential pardon"
};
typedef AForm* (Intern::*FormCreator)(const std::string&);
FormCreator creators[3] = {
&Intern::createShrubbery,
&Intern::createShrubbery,
&Intern::createPardon
};
for (int i = 0; i < 3; i++)
{
if (formName == formNames[i])
{
std::cout << "Intern creates "
<< formName
<< std::endl;
return (this->*creators[i])(target);
}
}
std::cout << "Intern cannot create \"" << formName
<< "\" because it doesn't exist"
<< std::endl;
return (NULL);
}

View File

@@ -14,16 +14,26 @@
# define INTERN_HPP
# include <iostream>
# include <string>
# include "../AForm/AForm.hpp"
# include "../ShrubberyCreationForm/ShrubberyCreationForm.hpp"
# include "../RobotomyRequestForm/RobotomyRequestForm.hpp"
# include "../PresidentialPardonForm/PresidentialPardonForm.hpp"
class Intern
{
private:
AForm* createShrubbery(const std::string &target);
AForm* createRobotomy(const std::string &target);
AForm* createPardon(const std::string &target);
public:
Intern();
Intern(const Intern &other);
Intern& operator=(const Intern &other);
~Intern();
AForm* makeForm(const std::string &formName, const std::string &target);
};
#endif

View File

@@ -12,6 +12,7 @@
#include "Bureaucrat/Bureaucrat.hpp"
#include "AForm/AForm.hpp"
#include "Intern/Intern.hpp"
#include "ShrubberyCreationForm/ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm/RobotomyRequestForm.hpp"
#include "PresidentialPardonForm/PresidentialPardonForm.hpp"
@@ -22,15 +23,18 @@ int main()
// RobotomyRequestForm
srand(time(NULL));
std::cout << "---- ex01 tests ----" << std::endl;
RobotomyRequestForm form("home");
std::cout << "---- ex03 tests ----" << std::endl;
Bureaucrat angel("Angel", 1);
Intern intern;
try {
Bureaucrat angel("Angel", 1);
AForm* form = intern.makeForm("shrubbery creation", "home");
angel.executeForm(form);
angel.signForm(form);
angel.executeForm(form);
angel.executeForm(*form);
angel.signForm(*form);
angel.executeForm(*form);
delete form;
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}