d03 check ok pour leaks et correction warning copy contructor avec g++
This commit is contained in:
@@ -50,6 +50,9 @@ $(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
|||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
$(CC) $(OBJS) -o $@ $(LIBS)
|
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||||
|
|
||||||
|
leaks: $(NAME)
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS)
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
|||||||
BIN
d03/ex00/action
BIN
d03/ex00/action
Binary file not shown.
@@ -52,6 +52,9 @@ $(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
|||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
$(CC) $(OBJS) -o $@ $(LIBS)
|
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||||
|
|
||||||
|
leaks: $(NAME)
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS)
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ ScavTrap::ScavTrap( std::string name ) : ClapTrap(name) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScavTrap::ScavTrap( ScavTrap const & src ) {
|
// base class better be explicitly called (g++ warning)
|
||||||
|
// https://stackoverflow.com/questions/43612772/base-class-class-a-should-be-explicitly-initialized-in-the-copy-constructor
|
||||||
|
ScavTrap::ScavTrap( ScavTrap const & src ) : ClapTrap(src) {
|
||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
|
|||||||
BIN
d03/ex01/robots
BIN
d03/ex01/robots
Binary file not shown.
@@ -13,7 +13,7 @@ FragTrap::FragTrap( std::string name ) : ClapTrap(name) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FragTrap::FragTrap( FragTrap const & src ) {
|
FragTrap::FragTrap( FragTrap const & src ) : ClapTrap(src) {
|
||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ $(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
|||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
$(CC) $(OBJS) -o $@ $(LIBS)
|
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||||
|
|
||||||
|
leaks: $(NAME)
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS)
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ ScavTrap::ScavTrap( std::string name ) : ClapTrap(name) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScavTrap::ScavTrap( ScavTrap const & src ) {
|
ScavTrap::ScavTrap( ScavTrap const & src ) : ClapTrap(src) {
|
||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
|
|||||||
BIN
d03/ex02/robots
BIN
d03/ex02/robots
Binary file not shown.
@@ -15,7 +15,10 @@ DiamondTrap::DiamondTrap( std::string name )
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiamondTrap::DiamondTrap( DiamondTrap const & src ) {
|
DiamondTrap::DiamondTrap( DiamondTrap const & src )
|
||||||
|
: ClapTrap(src)
|
||||||
|
, FragTrap(src)
|
||||||
|
, ScavTrap(src) {
|
||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ FragTrap::FragTrap( std::string name ) : ClapTrap(name) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FragTrap::FragTrap( FragTrap const & src ) {
|
FragTrap::FragTrap( FragTrap const & src ) : ClapTrap(src) {
|
||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
|
|||||||
@@ -59,6 +59,9 @@ $(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
|||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
$(CC) $(OBJS) -o $@ $(LIBS)
|
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||||
|
|
||||||
|
leaks: $(NAME)
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS)
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ ScavTrap::ScavTrap( std::string name ) : ClapTrap(name) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScavTrap::ScavTrap( ScavTrap const & src ) {
|
ScavTrap::ScavTrap( ScavTrap const & src ) : ClapTrap(src) {
|
||||||
_class = _dClass;
|
_class = _dClass;
|
||||||
*this = src;
|
*this = src;
|
||||||
_number = getTotalNumber();
|
_number = getTotalNumber();
|
||||||
|
|||||||
BIN
d03/ex03/robots
BIN
d03/ex03/robots
Binary file not shown.
@@ -1,78 +0,0 @@
|
|||||||
######################################
|
|
||||||
# COMMANDS #
|
|
||||||
######################################
|
|
||||||
CXX = c++ -c
|
|
||||||
LINK = c++
|
|
||||||
MKDIR = mkdir -p
|
|
||||||
RM = rm -rf
|
|
||||||
|
|
||||||
######################################
|
|
||||||
# EXECUTABLE #
|
|
||||||
######################################
|
|
||||||
NAME = ex01.out
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# DIRECTORIES #
|
|
||||||
#######################################
|
|
||||||
SRC_DIR = srcs/
|
|
||||||
OBJ_DIR = objs/
|
|
||||||
PRV_DIR = private/
|
|
||||||
|
|
||||||
######################################
|
|
||||||
# SOURCE FILES #
|
|
||||||
######################################
|
|
||||||
SRC = \
|
|
||||||
${addprefix class/, \
|
|
||||||
ClapTrap.cpp \
|
|
||||||
ScavTrap.cpp \
|
|
||||||
} \
|
|
||||||
main.cpp
|
|
||||||
|
|
||||||
######################################
|
|
||||||
# OBJECT FILES #
|
|
||||||
######################################
|
|
||||||
OBJ = ${SRC:.cpp=.o}
|
|
||||||
OBJ := ${addprefix ${OBJ_DIR}, ${OBJ}}
|
|
||||||
|
|
||||||
DEP = ${OBJ:.o=.d}
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# FLAGS #
|
|
||||||
#######################################
|
|
||||||
CPPFLAGS = -Wall -Wextra -Werror
|
|
||||||
CPPFLAGS += -std=c++98
|
|
||||||
CPPFLAGS += -MMD -MP
|
|
||||||
CPPFLAGS += -I${PRV_DIR}
|
|
||||||
|
|
||||||
LDFLAGS =
|
|
||||||
|
|
||||||
ifeq (${DEBUG}, 1)
|
|
||||||
CPPFLAGS += -g
|
|
||||||
CPPFLAGS += -DDEBUG=1
|
|
||||||
endif
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# RULES #
|
|
||||||
#######################################
|
|
||||||
${NAME}: ${OBJ}
|
|
||||||
${LINK} ${OBJ} ${LDFLAGS} ${OUTPUT_OPTION}
|
|
||||||
|
|
||||||
all: ${NAME}
|
|
||||||
|
|
||||||
-include ${DEP}
|
|
||||||
|
|
||||||
${OBJ_DIR}%.o: ${SRC_DIR}%.cpp
|
|
||||||
@${MKDIR} ${@D}
|
|
||||||
${CXX} ${CPPFLAGS} $< ${OUTPUT_OPTION}
|
|
||||||
|
|
||||||
clean:
|
|
||||||
${RM} ${OBJ_DIR} ${NAME} vgcore.*
|
|
||||||
|
|
||||||
fclean:
|
|
||||||
${RM} ${OBJ_DIR} ${NAME} vgcore.*
|
|
||||||
|
|
||||||
re: clean all
|
|
||||||
|
|
||||||
fre: fclean all
|
|
||||||
|
|
||||||
.PHONY: all clean fclean re fre
|
|
||||||
Binary file not shown.
@@ -1,3 +0,0 @@
|
|||||||
objs/class/ClapTrap.o: srcs/class/ClapTrap.cpp private/class/ClapTrap.hpp
|
|
||||||
|
|
||||||
private/class/ClapTrap.hpp:
|
|
||||||
Binary file not shown.
@@ -1,6 +0,0 @@
|
|||||||
objs/class/ScavTrap.o: srcs/class/ScavTrap.cpp private/class/ScavTrap.hpp \
|
|
||||||
private/class/ClapTrap.hpp
|
|
||||||
|
|
||||||
private/class/ScavTrap.hpp:
|
|
||||||
|
|
||||||
private/class/ClapTrap.hpp:
|
|
||||||
Binary file not shown.
@@ -1,6 +0,0 @@
|
|||||||
objs/main.o: srcs/main.cpp private/class/ScavTrap.hpp \
|
|
||||||
private/class/ClapTrap.hpp
|
|
||||||
|
|
||||||
private/class/ScavTrap.hpp:
|
|
||||||
|
|
||||||
private/class/ClapTrap.hpp:
|
|
||||||
Binary file not shown.
@@ -1,59 +0,0 @@
|
|||||||
#ifndef CLAPTRAP_HPP
|
|
||||||
# define CLAPTRAP_HPP
|
|
||||||
|
|
||||||
# include <iostream>
|
|
||||||
|
|
||||||
# ifndef DEBUG
|
|
||||||
# define DEBUG 0
|
|
||||||
# endif
|
|
||||||
|
|
||||||
class ClapTrap
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
// Attributes
|
|
||||||
static std::string const _defaultName;
|
|
||||||
static unsigned int const _defaultHitPoints;
|
|
||||||
static unsigned int const _defaultEnergyPoints;
|
|
||||||
static unsigned int const _defaultAttackDamages;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
// Attributes
|
|
||||||
std::string _name;
|
|
||||||
unsigned int _hitPoints;
|
|
||||||
unsigned int _energyPoints;
|
|
||||||
unsigned int _attackDamages;
|
|
||||||
|
|
||||||
// Constructors
|
|
||||||
ClapTrap(
|
|
||||||
std::string const &name,
|
|
||||||
unsigned int const hitPoints,
|
|
||||||
unsigned int const energyPoints,
|
|
||||||
unsigned int const attackDamages);
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Constructors
|
|
||||||
ClapTrap(std::string const &name = ClapTrap::_defaultName);
|
|
||||||
ClapTrap(ClapTrap const &src);
|
|
||||||
|
|
||||||
// Destructors
|
|
||||||
virtual ~ClapTrap(void);
|
|
||||||
|
|
||||||
// Accessors
|
|
||||||
std::string const &getName(void) const;
|
|
||||||
|
|
||||||
unsigned int getHitPoints(void) const;
|
|
||||||
unsigned int getEnergyPoints(void) const;
|
|
||||||
unsigned int getAttackDamages(void) const;
|
|
||||||
|
|
||||||
// Member functions
|
|
||||||
virtual void attack(std::string const &target);
|
|
||||||
void beRepaired(unsigned int const amount);
|
|
||||||
void takeDamage(unsigned int const amount);
|
|
||||||
|
|
||||||
// Operators
|
|
||||||
ClapTrap &operator=(ClapTrap const &rhs);
|
|
||||||
};
|
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &o, ClapTrap const &rhs);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
#ifndef SCAVTRAP_HPP
|
|
||||||
# define SCAVTRAP_HPP
|
|
||||||
|
|
||||||
# include "ClapTrap.hpp"
|
|
||||||
|
|
||||||
# ifndef DEBUG
|
|
||||||
# define DEBUG 0
|
|
||||||
# endif
|
|
||||||
|
|
||||||
class ScavTrap : public ClapTrap
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
// Attributes
|
|
||||||
bool _activeMode;
|
|
||||||
|
|
||||||
static std::string const _defaultName;
|
|
||||||
static unsigned int const _defaultHitPoints;
|
|
||||||
static unsigned int const _defaultEnergyPoints;
|
|
||||||
static unsigned int const _defaultAttackDamages;
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Constructors
|
|
||||||
ScavTrap(std::string const &name = ScavTrap::_defaultName);
|
|
||||||
ScavTrap(ScavTrap const &src);
|
|
||||||
|
|
||||||
// Destructors
|
|
||||||
virtual ~ScavTrap(void);
|
|
||||||
|
|
||||||
// Member functions
|
|
||||||
virtual void attack(std::string const &target);
|
|
||||||
void guardGate(void);
|
|
||||||
|
|
||||||
// Operators
|
|
||||||
ScavTrap &operator=(ScavTrap const &rhs);
|
|
||||||
};
|
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &o, ScavTrap const &rhs);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,235 +0,0 @@
|
|||||||
#include "class/ClapTrap.hpp"
|
|
||||||
|
|
||||||
// ************************************************************************** //
|
|
||||||
// Constructors //
|
|
||||||
// ************************************************************************** //
|
|
||||||
|
|
||||||
ClapTrap::ClapTrap(std::string const &name) :
|
|
||||||
_name(name),
|
|
||||||
_hitPoints(ClapTrap::_defaultHitPoints),
|
|
||||||
_energyPoints(ClapTrap::_defaultEnergyPoints),
|
|
||||||
_attackDamages(ClapTrap::_defaultAttackDamages)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Creating ClapTrap "
|
|
||||||
<< this->_name
|
|
||||||
<< " (" << this->_hitPoints << ")"
|
|
||||||
<< " (" << this->_energyPoints << ")"
|
|
||||||
<< " (" << this->_attackDamages << ")"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClapTrap::ClapTrap(ClapTrap const &src) :
|
|
||||||
_name(src._name),
|
|
||||||
_hitPoints(src._hitPoints),
|
|
||||||
_energyPoints(src._energyPoints),
|
|
||||||
_attackDamages(src._attackDamages)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Creating ClapTrap "
|
|
||||||
<< this->_name
|
|
||||||
<< " (" << this->_hitPoints << ")"
|
|
||||||
<< " (" << this->_energyPoints << ")"
|
|
||||||
<< " (" << this->_attackDamages << ")"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClapTrap::ClapTrap(
|
|
||||||
std::string const &name,
|
|
||||||
unsigned int const hitPoints,
|
|
||||||
unsigned int const energyPoints,
|
|
||||||
unsigned int const attackDamages) :
|
|
||||||
_name(name),
|
|
||||||
_hitPoints(hitPoints),
|
|
||||||
_energyPoints(energyPoints),
|
|
||||||
_attackDamages(attackDamages)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Creating ClapTrap "
|
|
||||||
<< this->_name
|
|
||||||
<< " (" << this->_hitPoints << ")"
|
|
||||||
<< " (" << this->_energyPoints << ")"
|
|
||||||
<< " (" << this->_attackDamages << ")"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
|
||||||
// Destructors //
|
|
||||||
// ************************************************************************* //
|
|
||||||
|
|
||||||
ClapTrap::~ClapTrap(void)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Destroying ClapTrap "
|
|
||||||
<< this->_name
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
|
||||||
// Accessors //
|
|
||||||
// ************************************************************************* //
|
|
||||||
|
|
||||||
std::string const &ClapTrap::getName(void) const
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::getName()"
|
|
||||||
<< std::endl;
|
|
||||||
return this->_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int ClapTrap::getHitPoints(void) const
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::getHitPoints()"
|
|
||||||
<< std::endl;
|
|
||||||
return this->_hitPoints;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int ClapTrap::getEnergyPoints(void) const
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::getEnergyPoints()"
|
|
||||||
<< std::endl;
|
|
||||||
return this->_energyPoints;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int ClapTrap::getAttackDamages(void) const
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::getAttackDamages()"
|
|
||||||
<< std::endl;
|
|
||||||
return this->_attackDamages;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
|
||||||
// Public Member Functions //
|
|
||||||
// ************************************************************************* //
|
|
||||||
|
|
||||||
void ClapTrap::attack(std::string const &target)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::attack()"
|
|
||||||
<< std::endl;
|
|
||||||
std::cout
|
|
||||||
<< "ClapTrap "
|
|
||||||
<< this->_name;
|
|
||||||
if (this->_hitPoints && this->_energyPoints)
|
|
||||||
std::cout
|
|
||||||
<< " deals "
|
|
||||||
<< this->_attackDamages
|
|
||||||
<< " damages to ";
|
|
||||||
else if (!this->_hitPoints)
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough hit points to attack ";
|
|
||||||
else
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough energy points to attack ";
|
|
||||||
std::cout
|
|
||||||
<< target
|
|
||||||
<< std::endl;
|
|
||||||
this->_energyPoints -= !!this->_energyPoints;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClapTrap::beRepaired(unsigned int const amount)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::beRepaired()"
|
|
||||||
<< std::endl;
|
|
||||||
std::cout
|
|
||||||
<< "ClapTrap "
|
|
||||||
<< this->_name;
|
|
||||||
if (this->_hitPoints && this->_energyPoints)
|
|
||||||
{
|
|
||||||
std::cout
|
|
||||||
<< " repairs itself for an amount of "
|
|
||||||
<< amount;
|
|
||||||
this->_hitPoints += amount;
|
|
||||||
}
|
|
||||||
else if (!this->_hitPoints)
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough hit points to repair itself";
|
|
||||||
else
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough energy points to repair itself";
|
|
||||||
std::cout << std::endl;
|
|
||||||
this->_energyPoints -= !!this->_energyPoints;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClapTrap::takeDamage(unsigned int const amount)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::takeDamage()"
|
|
||||||
<< std::endl;
|
|
||||||
std::cout
|
|
||||||
<< "ClapTrap "
|
|
||||||
<< this->_name;
|
|
||||||
if (this->_hitPoints && this->_energyPoints)
|
|
||||||
{
|
|
||||||
std::cout
|
|
||||||
<< " takes "
|
|
||||||
<< amount
|
|
||||||
<< " damages";
|
|
||||||
if (this->_hitPoints < amount)
|
|
||||||
this->_hitPoints = 0;
|
|
||||||
else
|
|
||||||
this->_hitPoints -= amount;
|
|
||||||
}
|
|
||||||
else if (!this->_hitPoints)
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough hit points to take damages";
|
|
||||||
else
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough energy points to take damages";
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
|
||||||
// Operators //
|
|
||||||
// ************************************************************************* //
|
|
||||||
|
|
||||||
ClapTrap &ClapTrap::operator=(ClapTrap const &rhs)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ClapTrap::operator=()"
|
|
||||||
<< std::endl;
|
|
||||||
if (this != &rhs)
|
|
||||||
{
|
|
||||||
this->_name = rhs._name;
|
|
||||||
this->_hitPoints = rhs._hitPoints;
|
|
||||||
this->_energyPoints = rhs._energyPoints;
|
|
||||||
this->_attackDamages = rhs._attackDamages;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &o, ClapTrap const &rhs)
|
|
||||||
{
|
|
||||||
o
|
|
||||||
<< "ClapTrap:" << std::endl
|
|
||||||
<< "\t" "name: " << rhs.getName() << std::endl
|
|
||||||
<< "\t" "hitPoints: " << rhs.getHitPoints() << std::endl
|
|
||||||
<< "\t" "energyPoints: " << rhs.getEnergyPoints() << std::endl
|
|
||||||
<< "\t" "attackDamages: " << rhs.getAttackDamages() << std::endl;
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************** //
|
|
||||||
// Private Attributes //
|
|
||||||
// ************************************************************************** //
|
|
||||||
|
|
||||||
std::string const ClapTrap::_defaultName = std::string("defaultName");
|
|
||||||
unsigned int const ClapTrap::_defaultHitPoints = 10;
|
|
||||||
unsigned int const ClapTrap::_defaultEnergyPoints = 10;
|
|
||||||
unsigned int const ClapTrap::_defaultAttackDamages = 0;
|
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
#include "class/ScavTrap.hpp"
|
|
||||||
|
|
||||||
// ************************************************************************** //
|
|
||||||
// Constructors //
|
|
||||||
// ************************************************************************** //
|
|
||||||
|
|
||||||
ScavTrap::ScavTrap(std::string const &name) :
|
|
||||||
ClapTrap(
|
|
||||||
name,
|
|
||||||
ScavTrap::_defaultHitPoints,
|
|
||||||
ScavTrap::_defaultEnergyPoints,
|
|
||||||
ScavTrap::_defaultAttackDamages),
|
|
||||||
_activeMode(false)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< std::boolalpha
|
|
||||||
<< "Creating ScavTrap "
|
|
||||||
<< this->_name
|
|
||||||
<< " (" << this->_hitPoints << ")"
|
|
||||||
<< " (" << this->_energyPoints << ")"
|
|
||||||
<< " (" << this->_attackDamages << ")"
|
|
||||||
<< " (" << this->_activeMode << ")"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ScavTrap::ScavTrap(ScavTrap const &src) :
|
|
||||||
ClapTrap(
|
|
||||||
src._name,
|
|
||||||
src._hitPoints,
|
|
||||||
src._energyPoints,
|
|
||||||
src._attackDamages)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< std::boolalpha
|
|
||||||
<< "Creating ScavTrap "
|
|
||||||
<< this->_name
|
|
||||||
<< " (" << this->_hitPoints << ")"
|
|
||||||
<< " (" << this->_energyPoints << ")"
|
|
||||||
<< " (" << this->_attackDamages << ")"
|
|
||||||
<< " (" << this->_activeMode << ")"
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
|
||||||
// Destructors //
|
|
||||||
// ************************************************************************* //
|
|
||||||
|
|
||||||
ScavTrap::~ScavTrap(void)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Destroying ScavTrap "
|
|
||||||
<< this->_name
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
|
||||||
// Public Member Functions //
|
|
||||||
// ************************************************************************* //
|
|
||||||
|
|
||||||
void ScavTrap::attack(std::string const &target)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ScavTrap::attack()"
|
|
||||||
<< std::endl;
|
|
||||||
std::cout
|
|
||||||
<< "ScavTrap "
|
|
||||||
<< this->_name;
|
|
||||||
if (this->_hitPoints && this->_energyPoints)
|
|
||||||
std::cout
|
|
||||||
<< " deals "
|
|
||||||
<< this->_attackDamages
|
|
||||||
<< " damages to ";
|
|
||||||
else if (!this->_hitPoints)
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough hit points to attack ";
|
|
||||||
else
|
|
||||||
std::cout
|
|
||||||
<< " hasn't enough energy points to attack ";
|
|
||||||
std::cout
|
|
||||||
<< target
|
|
||||||
<< std::endl;
|
|
||||||
this->_energyPoints -= !!this->_energyPoints;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ScavTrap::guardGate(void)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ScavTrap::guardGate()"
|
|
||||||
<< std::endl;
|
|
||||||
std::cout
|
|
||||||
<< "ScavTrap "
|
|
||||||
<< this->_name;
|
|
||||||
if (!this->_activeMode)
|
|
||||||
std::cout
|
|
||||||
<< " enters ";
|
|
||||||
else
|
|
||||||
std::cout
|
|
||||||
<< " leaves ";
|
|
||||||
std::cout
|
|
||||||
<< "Gate keeper mode"
|
|
||||||
<< std::endl;
|
|
||||||
this->_activeMode ^= true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
|
||||||
// Operators //
|
|
||||||
// ************************************************************************* //
|
|
||||||
|
|
||||||
ScavTrap &ScavTrap::operator=(ScavTrap const &rhs)
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
std::cout
|
|
||||||
<< "Calling ScavTrap::operator=()"
|
|
||||||
<< std::endl;
|
|
||||||
if (this != &rhs)
|
|
||||||
{
|
|
||||||
this->_name = rhs._name;
|
|
||||||
this->_hitPoints = rhs._hitPoints;
|
|
||||||
this->_energyPoints = rhs._energyPoints;
|
|
||||||
this->_attackDamages = rhs._attackDamages;
|
|
||||||
this->_activeMode = rhs._activeMode;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &o, ScavTrap const &rhs)
|
|
||||||
{
|
|
||||||
o << "ScavTrap:" << std::endl
|
|
||||||
<< "\t" "name: " << rhs.getName() << std::endl
|
|
||||||
<< "\t" "hitPoints: " << rhs.getHitPoints() << std::endl
|
|
||||||
<< "\t" "energyPoints: " << rhs.getEnergyPoints() << std::endl
|
|
||||||
<< "\t" "attackDamages: " << rhs.getAttackDamages() << std::endl;
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ************************************************************************** //
|
|
||||||
// Private Attributes //
|
|
||||||
// ************************************************************************** //
|
|
||||||
|
|
||||||
std::string const ScavTrap::_defaultName = std::string("defaultName");
|
|
||||||
unsigned int const ScavTrap::_defaultHitPoints = 100;
|
|
||||||
unsigned int const ScavTrap::_defaultEnergyPoints = 50;
|
|
||||||
unsigned int const ScavTrap::_defaultAttackDamages = 20;
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
#include <cstdlib>
|
|
||||||
#include "class/ScavTrap.hpp"
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
ClapTrap robot1("robot1");
|
|
||||||
ScavTrap robot2("robot2");
|
|
||||||
ScavTrap robot3("robot3");
|
|
||||||
ClapTrap robot4("robot4");
|
|
||||||
|
|
||||||
std::cout << "\nassignement 1:\n";
|
|
||||||
ScavTrap robotmp1("robot5");
|
|
||||||
robotmp1.guardGate();
|
|
||||||
std::cout << "copy:\n";
|
|
||||||
ClapTrap robot6(robotmp1); // PBM : it says it's a ScavTrap but it has no guardGate()...
|
|
||||||
// robot6.guardGate();
|
|
||||||
std::cout << "END assignement 1:\n\n";
|
|
||||||
|
|
||||||
//std::cout << "assignement 2:\n";
|
|
||||||
// ScavTrap robotmp2("robot7");
|
|
||||||
// ScavTrap robot8(robotmp2);
|
|
||||||
// robot8.guardGate();
|
|
||||||
//
|
|
||||||
//std::cout << "assignement 3:\n";
|
|
||||||
// ClapTrap robotmp3("robot9"); // PBM : assignation doesn't work...
|
|
||||||
//// ScavTrap robot10(robotmp3);
|
|
||||||
//// robot10.guardGate();
|
|
||||||
//
|
|
||||||
//std::cout << "assignement 4:\n";
|
|
||||||
// ClapTrap robotmp4("robot11");
|
|
||||||
// ClapTrap robot12(robotmp4);
|
|
||||||
//// robot12.guardGate();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ClapTrap ct(std::string("T800"));
|
|
||||||
ScavTrap st(std::string("T1000"));
|
|
||||||
|
|
||||||
std::cout << ct << std::endl;
|
|
||||||
std::cout << st << std::endl;
|
|
||||||
|
|
||||||
st = ScavTrap(std::string("TX"));
|
|
||||||
|
|
||||||
std::cout << st << std::endl;
|
|
||||||
|
|
||||||
st.attack(std::string("Sarah CONNOR"));
|
|
||||||
|
|
||||||
st.guardGate();
|
|
||||||
st.guardGate();
|
|
||||||
st.guardGate();
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
#include "Base.hpp"
|
|
||||||
|
|
||||||
/*
|
|
||||||
* default/parametric constructor
|
|
||||||
*/
|
|
||||||
|
|
||||||
Base::Base( void ) {
|
|
||||||
std::cout << "base default constructor\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* destructor
|
|
||||||
*/
|
|
||||||
|
|
||||||
Base::~Base( void ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* copy constructor
|
|
||||||
*/
|
|
||||||
|
|
||||||
Base::Base( Base const & src ) {
|
|
||||||
std::cout << "base copy constructor\n";
|
|
||||||
*this = src;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* assignement operator
|
|
||||||
*/
|
|
||||||
|
|
||||||
Base & Base::operator=( Base const & rhs ) {
|
|
||||||
std::cout << "base assignations operator\n";
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Base::Base(int i) {
|
|
||||||
std::cout << "base parameters constructor\n";
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
#ifndef BASE_HPP
|
|
||||||
# define BASE_HPP
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
class Base {
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
Base( void ); // default/parametric constructor
|
|
||||||
Base( Base const & src ); // copy constructor
|
|
||||||
~Base( void ); // destructor
|
|
||||||
|
|
||||||
Base & operator=( Base const & rhs ); // assignement operator
|
|
||||||
|
|
||||||
Base(int i);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Binary file not shown.
@@ -1,39 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
class Base {
|
|
||||||
public:
|
|
||||||
Base () : _n(1) {}
|
|
||||||
Base & operator=(Base const & rhs) {
|
|
||||||
_n = rhs.getN();
|
|
||||||
std::cout << "base assignation operator\n";
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
int getN() const {
|
|
||||||
return _n;
|
|
||||||
}
|
|
||||||
void putN(int i) {
|
|
||||||
_n = i;
|
|
||||||
}
|
|
||||||
protected:
|
|
||||||
int _n;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Derived : public Base {
|
|
||||||
public:
|
|
||||||
Derived & operator=(Derived const & rhs) {
|
|
||||||
Base::operator=(rhs);
|
|
||||||
std::cout << "derived assignation operator\n";
|
|
||||||
return *this;}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main () {
|
|
||||||
|
|
||||||
Derived foo1;
|
|
||||||
Derived foo2;
|
|
||||||
foo2.putN(2);
|
|
||||||
std::cout << foo1.getN() << " " << foo2.getN() << "\n";
|
|
||||||
foo2 = foo1;
|
|
||||||
std::cout << foo1.getN() << " " << foo2.getN() << "\n";
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user