debut d01 ex03

This commit is contained in:
hugogogo
2022-02-05 19:14:56 +01:00
parent e6929e8e12
commit c1990815e2
11 changed files with 306 additions and 0 deletions

15
d01/ex03/HumanA.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "HumanA.hpp"
HumanA::HumanA( std::string name, Weapon weapon ) {
this->_name = name;
this->_weapon = &weapon ;
}
HumanA::~HumanA() {}
void HumanA::attack( void ) {
std::cout << this->_name << " attacks with their " << this->_weapon->getType() << std::endl;
}

25
d01/ex03/HumanA.hpp Normal file
View File

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

21
d01/ex03/HumanB.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include "HumanB.hpp"
HumanB::HumanB( std::string name ) {
this->_name = name;
}
HumanB::~HumanB() {}
void setWeapon( Weapon weapon ) {
this->_weapon = &weapon;
}
void HumanB::attack( void ) {
std::cout << this->_name << " attacks with their " << this->_weapon->getType() << std::endl;
}

26
d01/ex03/HumanB.hpp Normal file
View File

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

66
d01/ex03/Makefile Normal file
View File

@@ -0,0 +1,66 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value . name is case sensitive #
# VARIABLES . or name = value \ . use VPATH only for .c #
# . value . or .cpp #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = war
CC = clang++
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 -g3
VPATH = $(D_SRCS)
LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = .
SRCS = main.cpp \
Weapon.cpp \
HumanA.cpp \
HumanB.cpp
D_HEADERS = .
HEADERS = Weapon.hpp \
HumanA.hpp \
HumanB.hpp
D_OBJS = builds
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
RM_D_OBJS = rm -rf $(D_OBJS)
ifeq "$(D_OBJS)" "."
RM_D_OBJS =
endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
# RULES . recipe . $< : 1st prerequisite #
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
mkdir $@
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
$(RM_D_OBJS)
re: fclean all
.PHONY : all clean fclean re bonus run valgrind

20
d01/ex03/Weapon.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "Weapon.hpp"
Weapon::Weapon( std::string type ) {
this->_type = type;
}
Weapon::~Weapon() {}
std::string Weapon::getType( void ) {
return this->_type;
}
void Weapon::setType( std::string type ) {
this->_type = type;
}

23
d01/ex03/Weapon.hpp Normal file
View File

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

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

@@ -0,0 +1,23 @@
#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();
}
return 0;
}