feat(ex05): it works

This commit is contained in:
Khaïs COLIN 2025-05-12 16:18:41 +02:00
parent 006ac94c7f
commit fd476b2c63
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
5 changed files with 133 additions and 0 deletions

1
ex05/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
ex05

62
ex05/Harl.cpp Normal file
View file

@ -0,0 +1,62 @@
#include "Harl.hpp"
#include <iostream>
typedef void (Harl::*HarlComplaint)(void);
int getLevelIdx(std::string level) {
static const std::string levels[] = {
"debug",
"info",
"warning",
"error",
};
for (int i = 0; i < 4; i++) {
if (level == levels[i])
return i;
}
return -1;
}
void Harl::complain(std::string level) {
static const HarlComplaint dispatcher[] = {
&Harl::debug,
&Harl::info,
&Harl::warning,
&Harl::error,
};
int idx = getLevelIdx(level);
if (idx < 0 || idx > 3)
wrongLevel();
else {
HarlComplaint complaint = dispatcher[idx];
(this->*complaint)();
}
}
void Harl::debug() {
std::cout
<< "I love having extra bacon for my "
"7XL-double-cheese-triple-pickle-special-ketchup burger. I really do!"
<< std::endl;
}
void Harl::info() {
std::cout
<< "I cannot believe adding extra bacon costs more money. You didnt put "
"enough bacon in my burger! If you did, I wouldnt be asking for more!"
<< std::endl;
}
void Harl::warning() {
std::cout
<< "I think I deserve to have some extra bacon for free. Ive been "
"coming for years, whereas you started working here just last month."
<< std::endl;
}
void Harl::error() {
std::cout << "This is unacceptable! I want to speak to the manager now."
<< std::endl;
}
void Harl::wrongLevel() { std::cout << "Wait, I'm confused..." << std::endl; }

19
ex05/Harl.hpp Normal file
View file

@ -0,0 +1,19 @@
#ifndef HARL_HPP
#define HARL_HPP
#include <string>
class Harl {
public:
void complain(std::string level);
private:
void debug(void);
void info(void);
void warning(void);
void error(void);
void wrongLevel(void);
};
#endif

39
ex05/Makefile Normal file
View file

@ -0,0 +1,39 @@
NAME = ex05
ifeq ($(CPPFLAGS),)
CPPFLAGS = -Wall -Wextra -Werror -std=c++98 -g
endif
ifeq ($(CXX),)
CXX = c++
endif
# g++ is the default on 42 computers
ifeq ($(CXX),g++)
CXX = c++
endif
srcs = \
Harl.cpp \
main_objs = main.o $(srcs:.cpp=.o)
all_objs = $(main_objs)
deps = $(all_objs:.o=.d)
all: $(NAME)
-include $(deps)
$(NAME): $(main_objs)
$(CXX) $(CPPFLAGS) -o $@ $^
%.o: %.cpp
$(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp
$(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d
clean:
find . -name '*.o' -print -delete
find . -name '*.d' -print -delete
fclean: clean
rm -f $(NAME)
re:
+make fclean
+make all

12
ex05/main.cpp Normal file
View file

@ -0,0 +1,12 @@
#include "Harl.hpp"
int main(void) {
Harl harl;
harl.complain("Debug");
harl.complain("debug");
harl.complain("info");
harl.complain("warning");
harl.complain("error");
return 0;
}