diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..a607e6e --- /dev/null +++ b/ex03/Makefile @@ -0,0 +1,25 @@ +CXX = c++ +CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0 +SRCDIR = src +OBJDIR = obj +BINDIR = bin +EXECUTABLE = Weapon + +SRCS = $(wildcard $(SRCDIR)/*.cpp) +OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) + +all: $(BINDIR)/$(EXECUTABLE) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(BINDIR)/$(EXECUTABLE): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR)/*.o + +fclean: clean + rm -fr $(BINDIR)/$(EXECUTABLE) + +re: fclean all diff --git a/ex03/src/HumanA.cpp b/ex03/src/HumanA.cpp new file mode 100644 index 0000000..048ac4c --- /dev/null +++ b/ex03/src/HumanA.cpp @@ -0,0 +1,17 @@ +#include "HumanA.hpp" +#include "Weapon.hpp" +#include +#include + +HumanA::HumanA(std::string name, Weapon &weapon) : weapon(weapon) +{ + this->name = name; +} + +HumanA::~HumanA() +{} + +void HumanA::attack() +{ + std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl; +} diff --git a/ex03/src/HumanA.hpp b/ex03/src/HumanA.hpp new file mode 100644 index 0000000..f293276 --- /dev/null +++ b/ex03/src/HumanA.hpp @@ -0,0 +1,19 @@ +#ifndef HUMANA_HPP +# define HUMANA_HPP +# include "Weapon.hpp" +# include + +class HumanA +{ + private: + Weapon &weapon; + std::string name; + + public: + HumanA(std::string name, Weapon &weapon); + ~HumanA(); + + void attack(); +}; + +#endif diff --git a/ex03/src/HumanB.cpp b/ex03/src/HumanB.cpp new file mode 100644 index 0000000..adeafa8 --- /dev/null +++ b/ex03/src/HumanB.cpp @@ -0,0 +1,29 @@ +#include "HumanB.hpp" +#include "Weapon.hpp" +#include +#include +#include + +HumanB::HumanB(std::string name) +{ + this->weapon = NULL; + this->name = name; +} + +HumanB::~HumanB() +{} + +void HumanB::attack() +{ + if (this->weapon == NULL) + { + std::cerr << this->name << " can't attack without weapon" << std::endl; + return; + } + std::cout << this->name << " attacks with their " << this->weapon->getType() << std::endl; +} + +void HumanB::setWeapon(Weapon& new_weapon) +{ + this->weapon = &new_weapon; +} diff --git a/ex03/src/HumanB.hpp b/ex03/src/HumanB.hpp new file mode 100644 index 0000000..aec5569 --- /dev/null +++ b/ex03/src/HumanB.hpp @@ -0,0 +1,19 @@ +#ifndef HUMANB_HPP +# define HUMANB_HPP +# include "Weapon.hpp" +# include + +class HumanB +{ + private: + Weapon* weapon; + std::string name; + + public: + HumanB(std::string name); + ~HumanB(); + + void setWeapon(Weapon& new_weapon); + void attack(); +}; +#endif diff --git a/ex03/src/Weapon.cpp b/ex03/src/Weapon.cpp new file mode 100644 index 0000000..d7a4661 --- /dev/null +++ b/ex03/src/Weapon.cpp @@ -0,0 +1,24 @@ +#include "Weapon.hpp" +#include + +Weapon::Weapon(std::string type) +{ + this->type = type; +} + +Weapon::Weapon() +{ +} + +Weapon::~Weapon() +{} + +std::string Weapon::getType() +{ + return (this->type); +} + +void Weapon::setType(std::string new_type) +{ + this->type = new_type; +} diff --git a/ex03/src/Weapon.hpp b/ex03/src/Weapon.hpp new file mode 100644 index 0000000..364e48a --- /dev/null +++ b/ex03/src/Weapon.hpp @@ -0,0 +1,18 @@ +#ifndef WEAPON_HPP +# define WEAPON_HPP +# include + +class Weapon +{ + private: + std::string type; + + public: + Weapon(std::string type); + Weapon(); + ~Weapon(); + + std::string getType(); + void setType(std::string new_type); +}; +#endif diff --git a/ex03/src/main.cpp b/ex03/src/main.cpp new file mode 100644 index 0000000..8cdec94 --- /dev/null +++ b/ex03/src/main.cpp @@ -0,0 +1,23 @@ +#include "Weapon.hpp" +#include "HumanA.hpp" +#include "HumanB.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(); + } + return 0; +}