From 45f1bceaf7f432e55a143e550a853581482b5e45 Mon Sep 17 00:00:00 2001 From: Adam JOLY Date: Tue, 5 Nov 2024 18:07:40 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(Unnecessary?= =?UTF-8?q?=20violence):=20Ex03=20finished?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex03/HumanA.cpp | 7 +++++-- ex03/HumanA.hpp | 8 +++++--- ex03/HumanB.cpp | 10 ++++++---- ex03/HumanB.hpp | 10 ++++++---- ex03/Makefile | 7 +++++-- ex03/Weapon.cpp | 8 +++++--- ex03/Weapon.hpp | 6 ++++-- ex03/main.cpp | 24 ++++++++++++++++++++++-- 8 files changed, 58 insertions(+), 22 deletions(-) diff --git a/ex03/HumanA.cpp b/ex03/HumanA.cpp index f89332a..b9fceae 100644 --- a/ex03/HumanA.cpp +++ b/ex03/HumanA.cpp @@ -6,13 +6,16 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/04 19:41:23 by adjoly #+# #+# */ -/* Updated: 2024/11/04 19:42:58 by adjoly ### ########.fr */ +/* Updated: 2024/11/05 17:50:39 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "HumanA.hpp" +#include "Weapon.hpp" void HumanA::attack(void) { - std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl; + std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl; } + +HumanA::HumanA(std::string name, Weapon &weapon) : _name(name), _weapon(weapon) { } diff --git a/ex03/HumanA.hpp b/ex03/HumanA.hpp index bbdf8ce..c3e5162 100644 --- a/ex03/HumanA.hpp +++ b/ex03/HumanA.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/04 19:36:00 by adjoly #+# #+# */ -/* Updated: 2024/11/04 19:40:02 by adjoly ### ########.fr */ +/* Updated: 2024/11/05 17:51:09 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,11 @@ class HumanA { private: - std::string name; - Weapon weapon; + std::string _name; + Weapon &_weapon; public: void attack(void); + + HumanA(std::string name, Weapon &weapon); }; diff --git a/ex03/HumanB.cpp b/ex03/HumanB.cpp index ecc6fa5..2c123de 100644 --- a/ex03/HumanB.cpp +++ b/ex03/HumanB.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/04 19:43:18 by adjoly #+# #+# */ -/* Updated: 2024/11/04 19:50:18 by adjoly ### ########.fr */ +/* Updated: 2024/11/05 17:54:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,11 @@ #include void HumanB::attack(void) { - std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl; + std::cout << this->_name << " attacks with their " << this->_weapon->getType() << std::endl; } -void HumanB::setWeapon(Weapon newWeapon) { - this->weapon = newWeapon; +void HumanB::setWeapon(Weapon &newWeapon) { + this->_weapon = &newWeapon; } + +HumanB::HumanB(std::string name) : _name(name), _weapon(NULL) { } diff --git a/ex03/HumanB.hpp b/ex03/HumanB.hpp index 6c347b9..5256079 100644 --- a/ex03/HumanB.hpp +++ b/ex03/HumanB.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/04 19:39:47 by adjoly #+# #+# */ -/* Updated: 2024/11/04 19:50:19 by adjoly ### ########.fr */ +/* Updated: 2024/11/05 17:53:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,12 @@ class HumanB { private: - std::string name; - Weapon weapon; + std::string _name; + Weapon *_weapon; public: void attack(void); - void setWeapon(Weapon newWeapon); + void setWeapon(Weapon &newWeapon); + + HumanB(std::string name); }; diff --git a/ex03/Makefile b/ex03/Makefile index 61e7448..362dc15 100644 --- a/ex03/Makefile +++ b/ex03/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2024/11/04 19:17:30 by adjoly ### ########.fr # +# Updated: 2024/11/05 17:11:17 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -16,7 +16,10 @@ CC = c++ OBJSDIR = obj/ -SRCS = main.cpp +SRCS = main.cpp \ + HumanA.cpp \ + HumanB.cpp \ + Weapon.cpp OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) diff --git a/ex03/Weapon.cpp b/ex03/Weapon.cpp index 4546d85..291670c 100644 --- a/ex03/Weapon.cpp +++ b/ex03/Weapon.cpp @@ -6,16 +6,18 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/04 19:23:35 by adjoly #+# #+# */ -/* Updated: 2024/11/04 19:34:02 by adjoly ### ########.fr */ +/* Updated: 2024/11/05 17:57:08 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "Weapon.hpp" void Weapon::setType(std::string newType) { - this->type = newType; + this->_type = newType; } std::string Weapon::getType(void) { - return (this->type); + return (this->_type); } + +Weapon::Weapon(std::string type) : _type(type) { } diff --git a/ex03/Weapon.hpp b/ex03/Weapon.hpp index 61fce9f..ef436bf 100644 --- a/ex03/Weapon.hpp +++ b/ex03/Weapon.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/04 19:21:24 by adjoly #+# #+# */ -/* Updated: 2024/11/04 19:23:25 by adjoly ### ########.fr */ +/* Updated: 2024/11/05 17:56:50 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,11 @@ class Weapon { private: - std::string type; + std::string _type; public: std::string getType(void); void setType(std::string newType); + + Weapon(std::string type); }; diff --git a/ex03/main.cpp b/ex03/main.cpp index c7c6a2a..77b65b8 100644 --- a/ex03/main.cpp +++ b/ex03/main.cpp @@ -6,10 +6,30 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/04 19:20:57 by adjoly #+# #+# */ -/* Updated: 2024/11/04 19:21:10 by adjoly ### ########.fr */ +/* Updated: 2024/11/05 17:43:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -int main(void) { +#include "HumanA.hpp" +#include "HumanB.hpp" +#include +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(); + } + return 0; }