init: ex03

This commit is contained in:
Camille Chauvet 2023-05-24 14:41:02 +00:00
parent e67f872720
commit f063902349
8 changed files with 174 additions and 0 deletions

25
ex03/Makefile Normal file
View File

@ -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

17
ex03/src/HumanA.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "HumanA.hpp"
#include "Weapon.hpp"
#include <string>
#include <iostream>
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;
}

19
ex03/src/HumanA.hpp Normal file
View File

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

29
ex03/src/HumanB.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "HumanB.hpp"
#include "Weapon.hpp"
#include <cstddef>
#include <string>
#include <iostream>
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;
}

19
ex03/src/HumanB.hpp Normal file
View File

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

24
ex03/src/Weapon.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "Weapon.hpp"
#include <string>
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;
}

18
ex03/src/Weapon.hpp Normal file
View File

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

23
ex03/src/main.cpp Normal file
View File

@ -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;
}