From 28784b50f0b59185e5e2b3a9d3f35287008b240d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 12 May 2025 14:00:11 +0200 Subject: [PATCH] feat(ex03): it works --- ex03/.gitignore | 1 + ex03/HumanA.cpp | 8 ++++++++ ex03/HumanA.hpp | 17 +++++++++++++++++ ex03/HumanB.cpp | 15 +++++++++++++++ ex03/HumanB.hpp | 18 ++++++++++++++++++ ex03/Makefile | 41 +++++++++++++++++++++++++++++++++++++++++ ex03/Weapon.cpp | 10 ++++++++++ ex03/Weapon.hpp | 17 +++++++++++++++++ ex03/main.cpp | 26 ++++++++++++++++++++++++++ 9 files changed, 153 insertions(+) create mode 100644 ex03/.gitignore create mode 100644 ex03/HumanA.cpp create mode 100644 ex03/HumanA.hpp create mode 100644 ex03/HumanB.cpp create mode 100644 ex03/HumanB.hpp create mode 100644 ex03/Makefile create mode 100644 ex03/Weapon.cpp create mode 100644 ex03/Weapon.hpp create mode 100644 ex03/main.cpp diff --git a/ex03/.gitignore b/ex03/.gitignore new file mode 100644 index 0000000..b3c7394 --- /dev/null +++ b/ex03/.gitignore @@ -0,0 +1 @@ +ex03 diff --git a/ex03/HumanA.cpp b/ex03/HumanA.cpp new file mode 100644 index 0000000..5816d6e --- /dev/null +++ b/ex03/HumanA.cpp @@ -0,0 +1,8 @@ +#include "HumanA.hpp" +#include + +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; +} diff --git a/ex03/HumanA.hpp b/ex03/HumanA.hpp new file mode 100644 index 0000000..7d63fea --- /dev/null +++ b/ex03/HumanA.hpp @@ -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 diff --git a/ex03/HumanB.cpp b/ex03/HumanB.cpp new file mode 100644 index 0000000..0ed85b3 --- /dev/null +++ b/ex03/HumanB.cpp @@ -0,0 +1,15 @@ +#include "HumanB.hpp" +#include + +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; +} diff --git a/ex03/HumanB.hpp b/ex03/HumanB.hpp new file mode 100644 index 0000000..b8f813f --- /dev/null +++ b/ex03/HumanB.hpp @@ -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 diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..e8feef3 --- /dev/null +++ b/ex03/Makefile @@ -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 diff --git a/ex03/Weapon.cpp b/ex03/Weapon.cpp new file mode 100644 index 0000000..bfeab1c --- /dev/null +++ b/ex03/Weapon.cpp @@ -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; } diff --git a/ex03/Weapon.hpp b/ex03/Weapon.hpp new file mode 100644 index 0000000..8dc5e95 --- /dev/null +++ b/ex03/Weapon.hpp @@ -0,0 +1,17 @@ +#ifndef WEAPON_HPP +#define WEAPON_HPP + +#include + +class Weapon { +public: + Weapon(std::string type); + + const std::string &getType(void); + void setType(std::string type); + +private: + std::string type; +}; + +#endif diff --git a/ex03/main.cpp b/ex03/main.cpp new file mode 100644 index 0000000..9500bb1 --- /dev/null +++ b/ex03/main.cpp @@ -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; +}