cpp03 add last ex

This commit is contained in:
Alexandre POMMIER 2022-08-03 18:24:07 +02:00
parent 66a17e5611
commit 31f0f66ffc
22 changed files with 748 additions and 43 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:49 by apommier #+# #+# */ /* Created: 2022/07/13 05:22:49 by apommier #+# #+# */
/* Updated: 2022/08/01 13:12:47 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:18:50 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,9 +14,18 @@
//constructor destructor and assignation //constructor destructor and assignation
ClapTrap::ClapTrap()
{
std::cout << "ClapTrap Default constructor called" << std::endl;
this->_name = "UnNamed";
this->_hitPoints = 10;
this->_energyPoints = 10;
this->_attackDamage = 0;
}
ClapTrap::ClapTrap(std::string name) ClapTrap::ClapTrap(std::string name)
{ {
std::cout << "ClapTrap " << name << " Default constructor called" << std::endl; std::cout << "ClapTrap " << name << " Name constructor called" << std::endl;
this->_name = name; this->_name = name;
this->_hitPoints = 10; this->_hitPoints = 10;
this->_energyPoints = 10; this->_energyPoints = 10;
@ -91,10 +100,12 @@ void ClapTrap::takeDamage(unsigned int amount)
std::cout << "ClapTrap " << this->_name << " is dead stop hitting a dead corpse" << std::endl; std::cout << "ClapTrap " << this->_name << " is dead stop hitting a dead corpse" << std::endl;
else if (this->_hitPoints - amount <= 0) else if (this->_hitPoints - amount <= 0)
std::cout << "ClapTrap " << this->_name << " died" << std::endl; std::cout << "ClapTrap " << this->_name << " died" << std::endl;
this->_hitPoints -= amount;
if (this->_hitPoints > 0) if (this->_hitPoints > 0)
{
this->_hitPoints -= amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl; std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
} }
}
void ClapTrap::beRepaired(unsigned int amount) void ClapTrap::beRepaired(unsigned int amount)
{ {
@ -105,8 +116,10 @@ void ClapTrap::beRepaired(unsigned int amount)
else else
{ {
std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl; std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl;
if (this->_hitPoints < 2147483647 && 2147483647 - this->_hitPoints >= (long)amount)
this->_hitPoints += amount; this->_hitPoints += amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl; std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
this->_energyPoints--; this->_energyPoints--;
} }
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:53 by apommier #+# #+# */ /* Created: 2022/07/13 05:22:53 by apommier #+# #+# */
/* Updated: 2022/08/01 13:23:46 by apommier ### ########.fr */ /* Updated: 2022/08/03 17:23:38 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,8 +32,9 @@ class ClapTrap {
int getEnergyPoints(void) const; int getEnergyPoints(void) const;
int getAttackDamage(void) const; int getAttackDamage(void) const;
private: protected:
ClapTrap();
std::string _name; std::string _name;
int _hitPoints; int _hitPoints;
int _energyPoints; int _energyPoints;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:49 by apommier #+# #+# */ /* Created: 2022/07/13 05:22:49 by apommier #+# #+# */
/* Updated: 2022/08/01 13:13:03 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:18:50 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,9 +14,18 @@
//constructor destructor and assignation //constructor destructor and assignation
ClapTrap::ClapTrap()
{
std::cout << "ClapTrap Default constructor called" << std::endl;
this->_name = "UnNamed";
this->_hitPoints = 10;
this->_energyPoints = 10;
this->_attackDamage = 0;
}
ClapTrap::ClapTrap(std::string name) ClapTrap::ClapTrap(std::string name)
{ {
std::cout << "ClapTrap " << name << " Default constructor called" << std::endl; std::cout << "ClapTrap " << name << " Name constructor called" << std::endl;
this->_name = name; this->_name = name;
this->_hitPoints = 10; this->_hitPoints = 10;
this->_energyPoints = 10; this->_energyPoints = 10;
@ -91,10 +100,12 @@ void ClapTrap::takeDamage(unsigned int amount)
std::cout << "ClapTrap " << this->_name << " is dead stop hitting a dead corpse" << std::endl; std::cout << "ClapTrap " << this->_name << " is dead stop hitting a dead corpse" << std::endl;
else if (this->_hitPoints - amount <= 0) else if (this->_hitPoints - amount <= 0)
std::cout << "ClapTrap " << this->_name << " died" << std::endl; std::cout << "ClapTrap " << this->_name << " died" << std::endl;
this->_hitPoints -= amount;
if (this->_hitPoints > 0) if (this->_hitPoints > 0)
{
this->_hitPoints -= amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl; std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
} }
}
void ClapTrap::beRepaired(unsigned int amount) void ClapTrap::beRepaired(unsigned int amount)
{ {
@ -105,8 +116,10 @@ void ClapTrap::beRepaired(unsigned int amount)
else else
{ {
std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl; std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl;
if (this->_hitPoints < 2147483647 && 2147483647 - this->_hitPoints >= (long)amount)
this->_hitPoints += amount; this->_hitPoints += amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl; std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
this->_energyPoints--; this->_energyPoints--;
} }
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:53 by apommier #+# #+# */ /* Created: 2022/07/13 05:22:53 by apommier #+# #+# */
/* Updated: 2022/08/01 13:23:39 by apommier ### ########.fr */ /* Updated: 2022/08/03 17:23:38 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,7 @@ class ClapTrap {
protected: protected:
ClapTrap();
std::string _name; std::string _name;
int _hitPoints; int _hitPoints;
int _energyPoints; int _energyPoints;

View File

@ -6,21 +6,30 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:51 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:51 by apommier #+# #+# */
/* Updated: 2022/08/03 14:27:09 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:08:53 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ScavTrap.hpp" #include "ScavTrap.hpp"
ScavTrap::ScavTrap(std::string name) : ClapTrap(name) ScavTrap::ScavTrap()
{ {
std::cout << "ScavTrap " << this->_name << " Default constructor called" << std::endl; std::cout << "ScavTrap Default constructor called" << std::endl;
this->_hitPoints = 100; this->_hitPoints = 100;
this->_energyPoints = 50; this->_energyPoints = 50;
this->_attackDamage = 20; this->_attackDamage = 20;
} }
ScavTrap::ScavTrap(const ScavTrap& copy) : ClapTrap(copy) ScavTrap::ScavTrap(std::string name)
{
std::cout << "ScavTrap Name constructor called" << std::endl;
this->_name = name;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}
ScavTrap::ScavTrap(const ScavTrap& copy)
{ {
std::cout << "ScavTrap Copy constructor called" << std::endl; std::cout << "ScavTrap Copy constructor called" << std::endl;
this->_name = copy.getName(); this->_name = copy.getName();

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:53 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:53 by apommier #+# #+# */
/* Updated: 2022/08/03 14:23:58 by apommier ### ########.fr */ /* Updated: 2022/08/03 17:25:11 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,12 @@
#include "ClapTrap.hpp" #include "ClapTrap.hpp"
class ScavTrap : public ClapTrap class ScavTrap : virtual public ClapTrap
{ {
public: public:
ScavTrap(std::string name); ScavTrap(std::string name);
ScavTrap(const ScavTrap& copy); ScavTrap(const ScavTrap& copy);
~ScavTrap(); ~ScavTrap();
@ -29,8 +30,9 @@ class ScavTrap : public ClapTrap
void attack(const std::string& target); void attack(const std::string& target);
void guardGate(); void guardGate();
private: protected:
ScavTrap();
}; };
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:49 by apommier #+# #+# */ /* Created: 2022/07/13 05:22:49 by apommier #+# #+# */
/* Updated: 2022/08/01 13:13:20 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:18:50 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,9 +14,18 @@
//constructor destructor and assignation //constructor destructor and assignation
ClapTrap::ClapTrap()
{
std::cout << "ClapTrap Default constructor called" << std::endl;
this->_name = "UnNamed";
this->_hitPoints = 10;
this->_energyPoints = 10;
this->_attackDamage = 0;
}
ClapTrap::ClapTrap(std::string name) ClapTrap::ClapTrap(std::string name)
{ {
std::cout << "ClapTrap " << name << " Default constructor called" << std::endl; std::cout << "ClapTrap " << name << " Name constructor called" << std::endl;
this->_name = name; this->_name = name;
this->_hitPoints = 10; this->_hitPoints = 10;
this->_energyPoints = 10; this->_energyPoints = 10;
@ -91,10 +100,12 @@ void ClapTrap::takeDamage(unsigned int amount)
std::cout << "ClapTrap " << this->_name << " is dead stop hitting a dead corpse" << std::endl; std::cout << "ClapTrap " << this->_name << " is dead stop hitting a dead corpse" << std::endl;
else if (this->_hitPoints - amount <= 0) else if (this->_hitPoints - amount <= 0)
std::cout << "ClapTrap " << this->_name << " died" << std::endl; std::cout << "ClapTrap " << this->_name << " died" << std::endl;
this->_hitPoints -= amount;
if (this->_hitPoints > 0) if (this->_hitPoints > 0)
{
this->_hitPoints -= amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl; std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
} }
}
void ClapTrap::beRepaired(unsigned int amount) void ClapTrap::beRepaired(unsigned int amount)
{ {
@ -105,6 +116,7 @@ void ClapTrap::beRepaired(unsigned int amount)
else else
{ {
std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl; std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl;
if (this->_hitPoints < 2147483647 && 2147483647 - this->_hitPoints >= (long)amount)
this->_hitPoints += amount; this->_hitPoints += amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl; std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
this->_energyPoints--; this->_energyPoints--;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:53 by apommier #+# #+# */ /* Created: 2022/07/13 05:22:53 by apommier #+# #+# */
/* Updated: 2022/08/01 13:23:33 by apommier ### ########.fr */ /* Updated: 2022/08/03 17:23:38 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,7 @@ class ClapTrap {
protected: protected:
ClapTrap();
std::string _name; std::string _name;
int _hitPoints; int _hitPoints;
int _energyPoints; int _energyPoints;

View File

@ -6,15 +6,23 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 01:24:31 by apommier #+# #+# */ /* Created: 2022/07/14 01:24:31 by apommier #+# #+# */
/* Updated: 2022/08/03 15:27:21 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:07:52 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "FragTrap.hpp" #include "FragTrap.hpp"
FragTrap::FragTrap()
{
std::cout << "FragTrap Default constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 100;
this->_attackDamage = 30;
}
FragTrap::FragTrap(std::string name) : ClapTrap(name) FragTrap::FragTrap(std::string name) : ClapTrap(name)
{ {
std::cout << "FragTrap " << this->_name << " Default constructor called" << std::endl; std::cout << "FragTrap " << this->_name << " Name constructor called" << std::endl;
this->_hitPoints = 100; this->_hitPoints = 100;
this->_energyPoints = 100; this->_energyPoints = 100;
this->_attackDamage = 30; this->_attackDamage = 30;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 01:24:34 by apommier #+# #+# */ /* Created: 2022/07/14 01:24:34 by apommier #+# #+# */
/* Updated: 2022/08/03 14:23:47 by apommier ### ########.fr */ /* Updated: 2022/08/03 17:24:51 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,12 @@
#include "ClapTrap.hpp" #include "ClapTrap.hpp"
class FragTrap : public ClapTrap class FragTrap : virtual public ClapTrap
{ {
public: public:
FragTrap(std::string name); FragTrap(std::string name);
FragTrap(const FragTrap& copy); FragTrap(const FragTrap& copy);
~FragTrap(); ~FragTrap();
@ -29,8 +30,9 @@ class FragTrap : public ClapTrap
void attack(const std::string& target); void attack(const std::string& target);
void highFivesGuys(void); void highFivesGuys(void);
private: protected:
FragTrap();
}; };

View File

@ -6,21 +6,30 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:51 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:51 by apommier #+# #+# */
/* Updated: 2022/08/03 14:27:02 by apommier ### ########.fr */ /* Updated: 2022/08/03 18:08:53 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ScavTrap.hpp" #include "ScavTrap.hpp"
ScavTrap::ScavTrap(std::string name) : ClapTrap(name) ScavTrap::ScavTrap()
{ {
std::cout << "ScavTrap " << this->_name << " Default constructor called" << std::endl; std::cout << "ScavTrap Default constructor called" << std::endl;
this->_hitPoints = 100; this->_hitPoints = 100;
this->_energyPoints = 50; this->_energyPoints = 50;
this->_attackDamage = 20; this->_attackDamage = 20;
} }
ScavTrap::ScavTrap(const ScavTrap& copy) : ClapTrap(copy) ScavTrap::ScavTrap(std::string name)
{
std::cout << "ScavTrap Name constructor called" << std::endl;
this->_name = name;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}
ScavTrap::ScavTrap(const ScavTrap& copy)
{ {
std::cout << "ScavTrap Copy constructor called" << std::endl; std::cout << "ScavTrap Copy constructor called" << std::endl;
this->_name = copy.getName(); this->_name = copy.getName();

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:53 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:53 by apommier #+# #+# */
/* Updated: 2022/08/03 14:23:43 by apommier ### ########.fr */ /* Updated: 2022/08/03 17:25:11 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,12 @@
#include "ClapTrap.hpp" #include "ClapTrap.hpp"
class ScavTrap : public ClapTrap class ScavTrap : virtual public ClapTrap
{ {
public: public:
ScavTrap(std::string name); ScavTrap(std::string name);
ScavTrap(const ScavTrap& copy); ScavTrap(const ScavTrap& copy);
~ScavTrap(); ~ScavTrap();
@ -29,8 +30,9 @@ class ScavTrap : public ClapTrap
void attack(const std::string& target); void attack(const std::string& target);
void guardGate(); void guardGate();
private: protected:
ScavTrap();
}; };
#endif #endif

125
cpp03/ex03/ClapTrap.cpp Normal file
View File

@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:49 by apommier #+# #+# */
/* Updated: 2022/08/03 18:18:50 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
//constructor destructor and assignation
ClapTrap::ClapTrap()
{
std::cout << "ClapTrap Default constructor called" << std::endl;
this->_name = "UnNamed";
this->_hitPoints = 10;
this->_energyPoints = 10;
this->_attackDamage = 0;
}
ClapTrap::ClapTrap(std::string name)
{
std::cout << "ClapTrap " << name << " Name constructor called" << std::endl;
this->_name = name;
this->_hitPoints = 10;
this->_energyPoints = 10;
this->_attackDamage = 0;
}
ClapTrap::ClapTrap(const ClapTrap& copy)
{
std::cout << "ClapTrap Copy constructor called" << std::endl;
this->_name = copy.getName();
this->_hitPoints = copy.getHitPoints();
this->_energyPoints = copy.getEnergyPoints();
this->_attackDamage = copy.getAttackDamage();
}
ClapTrap::~ClapTrap()
{
std::cout << "ClapTrap " << this->_name << " Destructor called" << std::endl;
}
ClapTrap &ClapTrap::operator=(const ClapTrap& rhs)
{
std::cout << "ClapTrap " << this->_name << " Assignation operator called" << std::endl;
this->_name = rhs.getName();
this->_hitPoints = rhs.getHitPoints();
this->_energyPoints = rhs.getEnergyPoints();
this->_attackDamage = rhs.getAttackDamage();
return *this;
}
//accessor
std::string ClapTrap::getName(void) const
{
return (this->_name);
}
int ClapTrap::getHitPoints(void) const
{
return (this->_hitPoints);
}
int ClapTrap::getEnergyPoints(void) const
{
return (this->_energyPoints);
}
int ClapTrap::getAttackDamage(void) const
{
return (this->_attackDamage);
}
//member function
void ClapTrap::attack(const std::string& target)
{
if (this->_hitPoints <= 0)
std::cout << "ClapTrap " << this->_name << " could not attack because he died" << std::endl;
else if (this->_energyPoints <= 0)
std::cout << "ClapTrap " << this->_name << " could not attack because he don't have energy" << std::endl;
else
{
std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
this->_energyPoints--;
}
}
void ClapTrap::takeDamage(unsigned int amount)
{
std::cout << "ClapTrap " << this->_name << " take " << amount << " points of damage!" << std::endl;
if (this->_hitPoints <= 0)
std::cout << "ClapTrap " << this->_name << " is dead stop hitting a dead corpse" << std::endl;
else if (this->_hitPoints - amount <= 0)
std::cout << "ClapTrap " << this->_name << " died" << std::endl;
if (this->_hitPoints > 0)
{
this->_hitPoints -= amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
}
}
void ClapTrap::beRepaired(unsigned int amount)
{
if (this->_hitPoints <= 0)
std::cout << "ClapTrap " << this->_name << " could not repair because he died" << std::endl;
else if (this->_energyPoints <= 0)
std::cout << "ClapTrap " << this->_name << " could not repair because he don't have energy" << std::endl;
else
{
std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl;
if (this->_hitPoints < 2147483647 && 2147483647 - this->_hitPoints >= (long)amount)
this->_hitPoints += amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
this->_energyPoints--;
}
}

44
cpp03/ex03/ClapTrap.hpp Normal file
View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:53 by apommier #+# #+# */
/* Updated: 2022/08/03 17:23:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CLAPTRAP_HPP
# define CLAPTRAP_HPP
#include <iostream>
class ClapTrap {
public:
ClapTrap(std::string name);
ClapTrap(const ClapTrap& copy);
~ClapTrap();
ClapTrap &operator=(const ClapTrap& rhs);
void attack(const std::string& target);
void takeDamage(unsigned int amount);
void beRepaired(unsigned int amount);
std::string getName(void) const;
int getHitPoints(void) const;
int getEnergyPoints(void) const;
int getAttackDamage(void) const;
protected:
ClapTrap();
std::string _name;
int _hitPoints;
int _energyPoints;
int _attackDamage;
};
#endif

View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DiamondTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/03 15:32:49 by apommier #+# #+# */
/* Updated: 2022/08/03 18:11:10 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "DiamondTrap.hpp"
DiamondTrap::DiamondTrap()
{
std::cout << "DiamondTrap Default constructor called" << std::endl;
}
DiamondTrap::DiamondTrap(std::string name)
{
ClapTrap::_name = name + "_clap_name";
std::cout << "DiamondTrap Name constructor called" << std::endl;
this->_name = name;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 30;
}
DiamondTrap::DiamondTrap(const DiamondTrap& copy)
{
std::cout << "DiamondTrap Copy constructor called" << std::endl;
*this = copy;
}
DiamondTrap::~DiamondTrap()
{
std::cout << "DiamondTrap " << this->_name << " Destructor called" << std::endl;
}
void DiamondTrap::attack(const std::string& target)
{
ScavTrap::attack(target);
}
void DiamondTrap::whoAmI()
{
std::cout << "My name is " << this->_name << " and my parent class name is " << ClapTrap::_name << std::endl;
}
DiamondTrap &DiamondTrap::operator=(const DiamondTrap& rhs)
{
std::cout << "DiamondTrap " << this->_name << " Assignation operator called" << std::endl;
ClapTrap::operator=(rhs);
ScavTrap::operator=(rhs);
FragTrap::operator=(rhs);
return *this;
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DiamondTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/03 15:32:51 by apommier #+# #+# */
/* Updated: 2022/08/03 18:10:08 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DIAMONDTRAP_HPP
# define DIAMONDTRAP_HPP
#include "ClapTrap.hpp"
#include "ScavTrap.hpp"
#include "FragTrap.hpp"
class DiamondTrap : public FragTrap, public ScavTrap
{
public:
DiamondTrap(std::string name);
DiamondTrap(const DiamondTrap& copy);
~DiamondTrap();
DiamondTrap &operator=(const DiamondTrap& rhs);
void attack(const std::string& target);
void whoAmI();
private:
DiamondTrap();
std::string _name;
};
#endif

74
cpp03/ex03/FragTrap.cpp Normal file
View File

@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 01:24:31 by apommier #+# #+# */
/* Updated: 2022/08/03 18:07:52 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "FragTrap.hpp"
FragTrap::FragTrap()
{
std::cout << "FragTrap Default constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 100;
this->_attackDamage = 30;
}
FragTrap::FragTrap(std::string name) : ClapTrap(name)
{
std::cout << "FragTrap " << this->_name << " Name constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 100;
this->_attackDamage = 30;
}
FragTrap::FragTrap(const FragTrap& copy) : ClapTrap(copy)
{
std::cout << "FragTrap Copy constructor called" << std::endl;
this->_name = copy.getName();
this->_hitPoints = copy.getHitPoints();
this->_energyPoints = copy.getEnergyPoints();
this->_attackDamage = copy.getAttackDamage();
}
FragTrap::~FragTrap()
{
std::cout << "FragTrap " << this->_name << " Destructor called" << std::endl;
}
void FragTrap::attack(const std::string& target)
{
if (this->_hitPoints <= 0)
std::cout << "FragTrap " << this->_name << " could not attack because he died" << std::endl;
else if (this->_energyPoints <= 0)
std::cout << "FragTrap " << this->_name << " could not attack because he don't have energy" << std::endl;
else
{
std::cout << "FragTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
this->_energyPoints--;
}
}
FragTrap &FragTrap::operator=(const FragTrap& rhs)
{
std::cout << "FragTrap " << this->_name << " Assignation operator called" << std::endl;
this->_name = rhs.getName();
this->_hitPoints = rhs.getHitPoints();
this->_energyPoints = rhs.getEnergyPoints();
this->_attackDamage = rhs.getAttackDamage();
return *this;
}
void FragTrap::highFivesGuys(void)
{
if (this->_hitPoints <= 0)
std::cout << "FragTrap " << this->_name << " could not ask you for a highfive because he died" << std::endl;
else
std::cout << "FragTrap " << this->_name << " ask you for a highfive\n";
}

42
cpp03/ex03/FragTrap.hpp Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 01:24:34 by apommier #+# #+# */
/* Updated: 2022/08/03 17:24:51 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FRAGTRAP_HPP
# define FRAGTRAP_HPP
#include "ClapTrap.hpp"
class FragTrap : virtual public ClapTrap
{
public:
FragTrap(std::string name);
FragTrap(const FragTrap& copy);
~FragTrap();
FragTrap &operator=(const FragTrap& rhs);
void attack(const std::string& target);
void highFivesGuys(void);
protected:
FragTrap();
};
#endif

41
cpp03/ex03/Makefile Normal file
View File

@ -0,0 +1,41 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/07/13 05:22:57 by apommier #+# #+# #
# Updated: 2022/08/03 15:48:16 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
ClapTrap.cpp\
ScavTrap.cpp\
FragTrap.cpp\
DiamondTrap.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
RM = rm -rf
.cpp.o:
$(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o)
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

75
cpp03/ex03/ScavTrap.cpp Normal file
View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:51 by apommier #+# #+# */
/* Updated: 2022/08/03 18:08:53 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
ScavTrap::ScavTrap()
{
std::cout << "ScavTrap Default constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}
ScavTrap::ScavTrap(std::string name)
{
std::cout << "ScavTrap Name constructor called" << std::endl;
this->_name = name;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}
ScavTrap::ScavTrap(const ScavTrap& copy)
{
std::cout << "ScavTrap Copy constructor called" << std::endl;
this->_name = copy.getName();
this->_hitPoints = copy.getHitPoints();
this->_energyPoints = copy.getEnergyPoints();
this->_attackDamage = copy.getAttackDamage();
}
ScavTrap::~ScavTrap()
{
std::cout << "ScavTrap " << this->_name << " Destructor called" << std::endl;
}
void ScavTrap::attack(const std::string& target)
{
if (this->_hitPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not attack because he died" << std::endl;
else if (this->_energyPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not attack because he don't have energy" << std::endl;
else
{
std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
this->_energyPoints--;
}
}
ScavTrap &ScavTrap::operator=(const ScavTrap& rhs)
{
std::cout << "ScavTrap " << this->_name << " Assignation operator called" << std::endl;
this->_name = rhs.getName();
this->_hitPoints = rhs.getHitPoints();
this->_energyPoints = rhs.getEnergyPoints();
this->_attackDamage = rhs.getAttackDamage();
return *this;
}
void ScavTrap::guardGate()
{
if (this->_hitPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not guard because he died" << std::endl;
else
std::cout << "ScavTrap " << this->_name << " came into Gatekeeper mode\n";
}

38
cpp03/ex03/ScavTrap.hpp Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:53 by apommier #+# #+# */
/* Updated: 2022/08/03 17:25:11 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SCAVTRAP_HPP
# define SCAVTRAP_HPP
#include "ClapTrap.hpp"
class ScavTrap : virtual public ClapTrap
{
public:
ScavTrap(std::string name);
ScavTrap(const ScavTrap& copy);
~ScavTrap();
ScavTrap &operator=(const ScavTrap& rhs);
void attack(const std::string& target);
void guardGate();
protected:
ScavTrap();
};
#endif

94
cpp03/ex03/main.cpp Normal file
View File

@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:55 by apommier #+# #+# */
/* Updated: 2022/08/03 18:14:03 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
#include "ScavTrap.hpp"
#include "FragTrap.hpp"
#include "DiamondTrap.hpp"
int main()
{
ClapTrap John("John");
John.attack("Jean");
John.takeDamage(5);
John.takeDamage(4);
John.takeDamage(1);
John.attack("Jean");
John.beRepaired(100);
std::cout << std::endl;
ClapTrap John2(John);
John2.attack("Jean");
John2.takeDamage(5);
John2.beRepaired(100);
std::cout << std::endl;
ClapTrap John3("John3");
John3.beRepaired(100);
John3.attack("Jean");
John3.beRepaired(100);
John3.attack("Jean");
John3.beRepaired(100);
John3.attack("Jean");
John3.beRepaired(100);
John3.attack("Jean");
John3.beRepaired(100);
John3.attack("Jean");
std::cout << std::endl;
John3.beRepaired(100);
John3.attack("Jean");
John3.takeDamage(100);
std::cout << std::endl << "ScavTrap turn\n";
ScavTrap ScavJohn("CL4P-TP");
std::cout << std::endl;
ScavJohn.guardGate();
ScavJohn.attack("Jean");
ScavJohn.takeDamage(5);
ScavJohn.takeDamage(4);
ScavJohn.takeDamage(1);
ScavJohn.attack("Jean");
ScavJohn.beRepaired(100);
std::cout <<"ScavTrap have " << ScavJohn.getEnergyPoints() << " energy point(s)\n\n";
std::cout << std::endl << "FragTrap turn\n";
FragTrap FragJohn("Assassin");
std::cout << std::endl;
FragJohn.highFivesGuys();
FragJohn.attack("Jean");
FragJohn.takeDamage(5);
FragJohn.takeDamage(4);
FragJohn.takeDamage(1);
FragJohn.takeDamage(300);
FragJohn.attack("Jean");
std::cout <<"ScavTrap have " << FragJohn.getEnergyPoints() << " energy point(s)\n\n";
std::cout << std::endl;
DiamondTrap diamond("Nobody");
std::cout << std::endl;
diamond.highFivesGuys();
diamond.guardGate();
diamond.whoAmI();
diamond.attack("Jean");
diamond.takeDamage(5);
diamond.takeDamage(4);
diamond.takeDamage(1);
diamond.takeDamage(300);
diamond.attack("Jean");
std::cout <<"DiamondTrap have " << diamond.getEnergyPoints() << " energy point(s)\n\n";
return (0);
}