42Seoul/CPP Module 05

ex03

재윤 2024. 1. 26. 11:59
반응형
  1. forms을 작성하는 거는 귀찮아서 interns이 존재한다고 한다.
  • 인턴에게는 이름도 없고, 등급도 없고, 독특한 특성도 없습니다. 관료들이 관심을 갖는 유일한 것은 그들이 일을 하는 것입니다. → 멤버 변수가 없다라는 것임.
  • 그렇지만 중요한 makeForm 함수가 존재.
AForm *makeForm(const std::string &form, const std::string &target);
  • 이 함수 안에는 if/elseif/else 금지 → 함수 포인터 사용.
  • "Bender"를 대상으로 하는 RobotomyRequestForm을 생성.

 

intern

hpp

#ifndef INTERN_HPP
# define INTERN_HPP

#include "AForm.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"

class Intern 
{
private:
public:
	Intern(void);
	Intern(Intern const &intern);
	Intern &operator=(Intern const &intern);
	~Intern(void);

	AForm *createShrubbery(const std::string &target);
	AForm *createRobotomy(const std::string &target);
	AForm *createPresidentia(const std::string &target);

	AForm *makeForm(const std::string &form, const std::string &target);

	class FormNotExistException : public std::exception
	{
		const char *what() const throw();
	};
};
#endif

 

cpp

#include "Intern.hpp"

Intern::Intern(void)
{
	std::cout << "Intern Constructor called\\n";
}

Intern::~Intern(void)
{
	std::cout <<  "Intern Destructor called\\n";
}

Intern::Intern(Intern const &intern)
{
	(void) intern;
	std::cout <<  "Intern copy Constructor called\\n";
}

Intern& Intern::operator=(Intern const &intern)
{
	if (this == &intern)
		return (*this);
	std::cout <<  "Intern copy Constructor called\\n";
	return (*this);
}

const char *Intern::FormNotExistException::what(void) const throw()
{
	return "AForm doesn't exist";
}

AForm *Intern::createShrubbery(const std::string &target)
{
	return new ShrubberyCreationForm(target);
}

AForm *Intern::createRobotomy(const std::string &target)
{
	return new RobotomyRequestForm(target);
}

AForm *Intern::createPresidentia(const std::string &target)
{
	return new PresidentialPardonForm(target);
}

AForm *Intern::makeForm(const std::string &form, const std::string &target)
{
	const std::string formName[3] = {"shrubbery creation", "robotomy request", "presidential pardon"}; 
	AForm *(Intern::*writeForm[3])(const std::string &) = {
		&Intern::createShrubbery, &Intern::createRobotomy, &Intern::createPresidentia};
	AForm *result = NULL;

	try
	{
		for(int i = 0; i < 3; i++)
		{
			if (formName[i] == form)
			{
				result = (this->*writeForm[i])(target);
				std::cout << "Intern creates " << result->getName() << "\\n";
				return (result);
			}
		}
		throw Intern::FormNotExistException();
	}
	catch(const std::exception& e)
	{
		std::cerr << e.what() << '\\n';
	}
	return (NULL);
}

 

main.cpp

#include "Bureaucrat.hpp"
#include "AForm.hpp"
#include "ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "PresidentialPardonForm.hpp"
#include "Intern.hpp"

int main(void) {

    try {
        Intern intern;
        AForm* scf = intern.makeForm("shrubbery creation", "42seoul");
        std::cout << *scf << "\\n";
        delete scf;
        std::cout << "================ Shrubbery ================\\n";

        AForm *rrfe = intern.makeForm("robotomy request", "42seoul");
        std::cout << *rrfe << "\\n";
        delete rrfe;
        std::cout << "\\n================ Robotomy ================\\n";

        AForm *ppf = intern.makeForm("presidential pardon", "42seoul");
        std::cout << *ppf << "\\n";
        delete ppf;
        std::cout << "\\n================ Presidential ================\\n";

        AForm *unknown = intern.makeForm("Unknown", "42seoul");
        std::cout << unknown << "\\n";
        delete unknown;
        std::cout << "\\n================ Unknown ================\\n";

		{
			Intern someRandomIntern;
			AForm* rrf;

			rrf = someRandomIntern.makeForm("robotomy request" , "Bender");
			std::cout << *rrf << "\\n";
			delete rrf;

		}
		 std::cout << "\\n================ Main ================\\n";

    } catch (const std::exception &e) {
        std::cout << e.what() << "\\n";
    }
}
반응형

'42Seoul > CPP Module 05' 카테고리의 다른 글

ex02  (2) 2024.01.26
ex01  (0) 2024.01.26
ex00  (0) 2024.01.26