From: Lukas Jiriste Date: Fri, 11 Oct 2024 10:31:45 +0000 (+0200) Subject: Add solution to ex04 (without Makefile) X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=326d9b1d90f14e39267672c4e2cff283cbc8c87f;p=42%2FCPP_Module_01 Add solution to ex04 (without Makefile) --- diff --git a/ex05/Harl.cpp b/ex05/Harl.cpp new file mode 100644 index 0000000..b060fd9 --- /dev/null +++ b/ex05/Harl.cpp @@ -0,0 +1,76 @@ +#include "Harl.h" +#include +#include +#include + +Harl::Harl() +{ + m_levels[0] = "debug"; + m_speech[0] = &Harl::debug; + m_levels[1] = "info"; + m_speech[1] = &Harl::info; + m_levels[2] = "warning"; + m_speech[2] = &Harl::warning; + m_levels[3] = "error"; + m_speech[3] = &Harl::error; +} + +Harl::Harl(const Harl &harl) +{ + *this = harl; +} + +Harl::~Harl() +{ +} + +Harl &Harl::operator=(const Harl &harl) +{ + m_levels[0] = harl.m_levels[0]; + m_speech[0] = harl.m_speech[0]; + m_levels[1] = harl.m_levels[1]; + m_speech[1] = harl.m_speech[1]; + m_levels[2] = harl.m_levels[2]; + m_speech[2] = harl.m_speech[2]; + m_levels[3] = harl.m_levels[3]; + m_speech[3] = harl.m_speech[3]; + return (*this); +} + +void Harl::complain(std::string level) +{ + void (Harl::*selected)(void); + + selected = NULL; + for (size_t i(0); i < 4; ++i) + if (m_levels[i] == level) + selected = m_speech[i]; + if (selected) + (this->*selected)(); + else + std::cout << "Harl does not know this level.\n"; +} + +void Harl::debug(void) +{ + std::cout << "I love having extra bacon for my " + "7XL-double-cheese-triple-pickle-special-ketchup burger. I really do!\n"; +} + +void Harl::info(void) +{ + std::cout << "I cannot believe adding extra bacon costs more money. " + "You didn’t put enough bacon in my burger! " + "If you did, I wouldn’t be asking for more!\n"; +} + +void Harl::warning(void) +{ + std::cout << "I think I deserve to have some extra bacon for free. " + "I’ve been coming for ears whereas you started working here since last month.\n"; +} + +void Harl::error(void) +{ + std::cout << "This is unacceptable! I want to speak to the manager now.\n"; +} diff --git a/ex05/Harl.h b/ex05/Harl.h new file mode 100644 index 0000000..3e710bd --- /dev/null +++ b/ex05/Harl.h @@ -0,0 +1,26 @@ +#ifndef HARL_H +# define HARL_H + +# include + +class Harl +{ + private: + void debug(void); + void info(void); + void warning(void); + void error(void); + + void (Harl::*m_speech[4])(void); + std::string m_levels[4]; + + public: + Harl(); + Harl(const Harl &harl); + ~Harl(); + + Harl &operator=(const Harl &harl); + void complain(std::string level); +}; + +#endif // HARL_H diff --git a/ex05/main.cpp b/ex05/main.cpp new file mode 100644 index 0000000..7c58bf5 --- /dev/null +++ b/ex05/main.cpp @@ -0,0 +1,15 @@ +#include "Harl.h" + +int main(void) +{ + Harl harl; + + harl.complain("info"); + harl.complain("debug"); + harl.complain("warning"); + harl.complain("error"); + harl.complain("debug"); + harl.complain("info"); + harl.complain("error"); + return (0); +}