feat(ex03): it works

This commit is contained in:
Khaïs COLIN 2025-05-12 14:00:11 +02:00
parent 8f7b9c3068
commit 28784b50f0
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
9 changed files with 153 additions and 0 deletions

1
ex03/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
ex03

8
ex03/HumanA.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "HumanA.hpp"
#include <iostream>
HumanA::HumanA(std::string name, Weapon &weapon) : weapon(weapon), name(name) {}
void HumanA::attack(void) {
std::cout << name << " attacks with their " << weapon.getType() << std::endl;
}

17
ex03/HumanA.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef HUMANA_HPP
#define HUMANA_HPP
#include "Weapon.hpp"
class HumanA {
public:
HumanA(std::string name, Weapon &weapon);
void attack(void);
private:
Weapon &weapon;
std::string name;
};
#endif

15
ex03/HumanB.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "HumanB.hpp"
#include <iostream>
HumanB::HumanB(std::string name) : name(name) { this->weapon = NULL; }
void HumanB::setWeapon(Weapon &weapon) { this->weapon = &weapon; }
void HumanB::attack(void) {
std::string weaponType;
if (weapon != NULL)
weaponType = weapon->getType();
else
weaponType = "hands";
std::cout << name << " attacks with their " << weaponType << std::endl;
}

18
ex03/HumanB.hpp Normal file
View file

@ -0,0 +1,18 @@
#ifndef HUMANB_HPP
#define HUMANB_HPP
#include "Weapon.hpp"
class HumanB {
public:
HumanB(std::string name);
void setWeapon(Weapon &weapon);
void attack(void);
private:
Weapon *weapon;
std::string name;
};
#endif

41
ex03/Makefile Normal file
View file

@ -0,0 +1,41 @@
NAME = ex03
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 = \
Weapon.cpp \
HumanA.cpp \
HumanB.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

10
ex03/Weapon.cpp Normal file
View file

@ -0,0 +1,10 @@
#include "Weapon.hpp"
Weapon::Weapon(std::string type) { this->type = type; }
const std::string &Weapon::getType(void) {
const std::string &retvalue = type;
return retvalue;
}
void Weapon::setType(std::string type) { this->type = type; }

17
ex03/Weapon.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef WEAPON_HPP
#define WEAPON_HPP
#include <string>
class Weapon {
public:
Weapon(std::string type);
const std::string &getType(void);
void setType(std::string type);
private:
std::string type;
};
#endif

26
ex03/main.cpp Normal file
View file

@ -0,0 +1,26 @@
#include "HumanA.hpp"
#include "HumanB.hpp"
#include "Weapon.hpp"
int main() {
{
Weapon club = Weapon("crude spiked club");
HumanA bob("Bob", club);
bob.attack();
club.setType("some other type of club");
bob.attack();
}
{
Weapon club = Weapon("crude spiked club");
HumanB jim("Jim");
jim.setWeapon(club);
jim.attack();
club.setType("some other type of club");
jim.attack();
}
{
HumanB john("John");
john.attack();
}
return 0;
}