This commit is contained in:
Angel Ortigosa Perez
2025-09-07 10:03:17 +02:00
commit 859690063b
30 changed files with 846 additions and 0 deletions

23
ex03/HumanA/HumanA.cpp Normal file
View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HumanA.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "HumanA.hpp"
HumanA::HumanA(const std::string &name, Weapon &weapon)
: name(name), weapon(weapon) {}
void HumanA::attack() const
{
std::cout << this->name << " attacks with their "
<< this->weapon.getType()
<< std::endl;
}

31
ex03/HumanA/HumanA.hpp Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HumanA.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HUMANA_HPP
# define HUMANA_HPP
# include <iostream>
# include "../Weapon/Weapon.hpp"
class HumanA
{
private:
std::string name;
Weapon &weapon;
public:
HumanA(const std::string &name, Weapon &weapon);
void attack() const;
};
#endif

34
ex03/HumanB/HumanB.cpp Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HumanB.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "HumanB.hpp"
HumanB::HumanB(const std::string &name)
: name(name), weapon(NULL) {}
void HumanB::attack() const
{
if (weapon)
{
std::cout << this->name << " attacks with their "
<< this->weapon->getType()
<< std::endl;
} else {
std::cout << this->name << " has no weapon"
<< std::endl;
}
}
void HumanB::setWeapon(Weapon &w)
{
this->weapon = &w;
}

32
ex03/HumanB/HumanB.hpp Normal file
View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HumanB.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HUMANB_HPP
# define HUMANB_HPP
# include <iostream>
# include "../Weapon/Weapon.hpp"
class HumanB
{
private:
std::string name;
Weapon *weapon;
public:
HumanB(const std::string &name);
void setWeapon(Weapon &weapon);
void attack() const;
};
#endif

26
ex03/Makefile Normal file
View File

@@ -0,0 +1,26 @@
NAME=violence
SRCS=main.cpp Weapon/Weapon.cpp HumanA/HumanA.cpp HumanB/HumanB.cpp
OBJS=$(SRCS:.cpp=.o)
CC=g++
CFLAGS=-Wall -Wextra -Werror -std=c++98
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

26
ex03/Weapon/Weapon.cpp Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Weapon.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Weapon.hpp"
Weapon::Weapon(const std::string &type) : type(type) {}
const std::string& Weapon::getType() const
{
return (this->type);
}
void Weapon::setType(const std::string &str)
{
this->type = str;
}

31
ex03/Weapon/Weapon.hpp Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Weapon.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef WEAPON_HPP
# define WEAPON_HPP
# include <iostream>
class Weapon
{
private:
std::string type;
public:
Weapon(const std::string &type);
const std::string& getType() const;
void setType(const std::string &str);
};
#endif

37
ex03/main.cpp Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:18:16 by aortigos #+# #+# */
/* Updated: 2025/09/06 14:29:53 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Weapon/Weapon.hpp"
#include "HumanA/HumanA.hpp"
#include "HumanB/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.attack();
jim.setWeapon(club);
jim.attack();
}
return (0);
}