end cpp02 and 03

This commit is contained in:
kinou-p 2022-07-14 02:00:47 +02:00
parent 1adb025a09
commit 1a86607d24
30 changed files with 1654 additions and 0 deletions

42
cpp02/ex00/Fixed.cpp Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 00:58:04 by apommier #+# #+# */
/* Updated: 2022/06/22 15:28:59 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
Fixed::Fixed()
{
std::cout << "Default constructor called\n";
this->_value = 0;
}
Fixed::~Fixed()
{
std::cout << "Destructor called\n";
}
Fixed::Fixed(const Fixed& copy)
{
std::cout << "Copy constructor called\n";
this->_value = copy.getRawBits();
}
int Fixed::getRawBits(void) const
{
std::cout << "getRawBits member function called\n";
return (this->_value);
}
void Fixed::setRawBits(int const raw)
{
std::cout << "setRawBits member function called\n";
this->_value = raw;
}

34
cpp02/ex00/Fixed.hpp Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 00:58:18 by apommier #+# #+# */
/* Updated: 2022/06/22 15:25:06 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FIXED_HPP
# define FIXED_HPP
#include <iostream>
class Fixed {
public:
Fixed();
Fixed(const Fixed&);
~Fixed();
int getRawBits(void) const;
void setRawBits(int const raw);
private:
int _value;
const static int _fraction = 8;
};
#endif

35
cpp02/ex00/Makefile Normal file
View File

@ -0,0 +1,35 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/06/22 00:57:33 by apommier #+# #+# #
# Updated: 2022/06/22 01:25:33 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
Fixed.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror
RM = rm -rf
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

26
cpp02/ex00/main.cpp Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 00:57:20 by apommier #+# #+# */
/* Updated: 2022/07/05 21:37:36 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
int main(void)
{
Fixed a;
Fixed b( a );
Fixed c;
c = b;
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
return (0);
}

76
cpp02/ex01/Fixed.cpp Normal file
View File

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 15:31:03 by apommier #+# #+# */
/* Updated: 2022/07/12 04:40:22 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
Fixed::Fixed()
{
std::cout << "Default constructor called\n";
this->_value = 0;
}
Fixed::~Fixed()
{
std::cout << "Destructor called\n";
}
Fixed::Fixed(const float nbr)
{
std::cout << "Float constructor called\n";
this->_value = (int)(roundf(nbr * (1 << this->_fraction)));
}
Fixed::Fixed(const int nbr)
{
std::cout << "Int constructor called\n";
this->_value = nbr;this->_value = nbr << this->_fraction;
}
Fixed & Fixed::operator=(const Fixed& op)
{
std::cout << "Assignation operator called" << std::endl;
if (this != &op)
this->_value = op.getRawBits();
return (*this);
}
Fixed::Fixed(const Fixed& copy)
{
std::cout << "Copy constructor called\n";
this->_value = copy.getRawBits();
}
int Fixed::getRawBits(void) const
{
return (this->_value);
}
void Fixed::setRawBits(int const raw)
{
this->_value = raw;
}
float Fixed::toFloat(void) const
{
return ((float)this->_value / (1 << this->_fraction));
}
int Fixed::toInt(void) const
{
return (((int)(this->_value >> this->_fraction)));
}
std::ostream &operator<<(std::ostream &out, const Fixed &nbr)
{
out << nbr.toFloat();
return (out);
}

45
cpp02/ex01/Fixed.hpp Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 15:31:05 by apommier #+# #+# */
/* Updated: 2022/07/11 23:52:34 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FIXED_HPP
# define FIXED_HPP
#include <iostream>
#include <tgmath.h>
class Fixed {
public:
Fixed();
Fixed(const float nbr);
Fixed(const int nbr);
Fixed(const Fixed& copy);
~Fixed();
float toFloat(void) const;
int toInt(void) const;
int getRawBits(void) const;
void setRawBits(int const raw);
Fixed &operator=(const Fixed& op);
private:
int _value;
const static int _fraction = 8;
};
std::ostream &operator<<(std::ostream &out, const Fixed &nbr);
#endif

35
cpp02/ex01/Makefile Normal file
View File

@ -0,0 +1,35 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/06/22 15:30:34 by apommier #+# #+# #
# Updated: 2022/06/22 15:30:36 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
Fixed.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror
RM = rm -rf
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

35
cpp02/ex01/main.cpp Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 15:31:09 by apommier #+# #+# */
/* Updated: 2022/07/12 04:02:55 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
int main()
{
Fixed a;
Fixed const b( 10 );
Fixed const c( 42.42f );
Fixed const d( b );
a = Fixed( 1234.4321f );
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;
std::cout << "c is " << c << std::endl;
std::cout << "d is " << d << std::endl;
std::cout << "a is " << a.toInt() << " as integer" << std::endl;
std::cout << "b is " << b.toInt() << " as integer" << std::endl;
std::cout << "c is " << c.toInt() << " as integer" << std::endl;
std::cout << "d is " << d.toInt() << " as integer" << std::endl;
return (0);
}

206
cpp02/ex02/Fixed.cpp Normal file
View File

@ -0,0 +1,206 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 15:31:03 by apommier #+# #+# */
/* Updated: 2022/07/13 03:36:07 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
//construtor and destructor
Fixed::Fixed()
{
std::cout << "Default constructor called\n";
this->_value = 0;
}
Fixed::~Fixed()
{
std::cout << "Destructor called\n";
}
Fixed::Fixed(const float nbr)
{
std::cout << "Float constructor called\n";
this->_value = (int)(roundf(nbr * (1 << this->_fraction)));
}
Fixed::Fixed(const int nbr)
{
std::cout << "Int constructor called\n";
this->_value = nbr;this->_value = nbr << this->_fraction;
}
Fixed::Fixed(const Fixed& copy)
{
std::cout << "Copy constructor called\n";
this->_value = copy.getRawBits();
}
//accesssor
int Fixed::getRawBits(void) const
{
return (this->_value);
}
void Fixed::setRawBits(int const raw)
{
this->_value = raw;
}
//convertion
float Fixed::toFloat(void) const
{
return ((float)this->_value / (float)(1 << this->_fraction));
}
int Fixed::toInt(void) const
{
return (((int)(this->_value >> this->_fraction)));
}
//operator overload----
std::ostream &operator<<(std::ostream &out, const Fixed &nbr)
{
out << nbr.toFloat();
return (out);
}
Fixed &Fixed::operator=(const Fixed& arg)
{
std::cout << "Assignation operator called" << std::endl;
if (this != &arg)
this->_value = arg.getRawBits();
return (*this);
}
//operator overload (comparison)
bool Fixed::operator>(const Fixed& arg) const
{
return (this->_value > arg.getRawBits());
}
bool Fixed::operator<(const Fixed& arg) const
{
return (this->_value < arg.getRawBits());
}
bool Fixed::operator>=(const Fixed& arg) const
{
return (this->_value >= arg.getRawBits());
}
bool Fixed::operator<=(const Fixed& arg) const
{
return (this->_value <= arg.getRawBits());
}
bool Fixed::operator==(const Fixed& arg) const
{
return (this->_value == arg.getRawBits());
}
bool Fixed::operator!=(const Fixed& arg) const
{
return (this->_value == arg.getRawBits());
}
//operator overload (soustraction addition)
Fixed Fixed::operator+(const Fixed& arg) const
{
Fixed ret(*this);
ret.setRawBits(this->_value + arg.getRawBits());
return (ret);
}
Fixed Fixed::operator-(const Fixed& arg) const
{
Fixed ret(*this);
ret.setRawBits(this->_value - arg.getRawBits());
return (ret);
}
//operator overload (division multiplication)
Fixed Fixed::operator*(const Fixed& arg) const
{
Fixed ret(*this);
ret.setRawBits((this->_value * arg.getRawBits()) / (1 << this->_fraction));
return (ret);
}
Fixed Fixed::operator/(const Fixed& arg) const
{
Fixed ret(*this);
ret.setRawBits((this->_value * (1 << this->_fraction)) / arg.getRawBits());
return (ret);
}
//operator overload (incremention and decrementation)
Fixed &Fixed::operator++(void)
{
this->_value++;
return (*this);
}
Fixed Fixed::operator++(int)
{
Fixed ret(*this);
ret.operator++();
return (ret);
}
Fixed &Fixed::operator--(void)
{
this->_value++;
return (*this);
}
Fixed Fixed::operator--(int)
{
Fixed ret(*this);
ret.operator--();
return (ret);
}
//min and max betwenn two Fixed class
Fixed &Fixed::min(Fixed& arg, Fixed& argTwo)
{
if (arg < argTwo)
return (arg);
return (argTwo);
}
const Fixed &Fixed::min(const Fixed& arg, const Fixed& argTwo)
{
if (arg< argTwo)
return (arg);
return (argTwo);
}
Fixed &Fixed::max(Fixed& arg, Fixed& argTwo)
{
if (arg > argTwo)
return (arg);
return (argTwo);
}
const Fixed &Fixed::max(const Fixed& arg, const Fixed& argTwo)
{
if (arg > argTwo)
return (arg);
return (argTwo);
}

64
cpp02/ex02/Fixed.hpp Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 15:31:05 by apommier #+# #+# */
/* Updated: 2022/07/13 05:34:25 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FIXED_HPP
# define FIXED_HPP
#include <iostream>
#include <tgmath.h>
class Fixed {
public:
Fixed();
Fixed(const float nbr);
Fixed(const int nbr);
Fixed(const Fixed& copy);
~Fixed();
float toFloat(void) const;
int toInt(void) const;
int getRawBits(void) const;
void setRawBits(int const raw);
Fixed &operator=(const Fixed& arg);
bool operator>(const Fixed& arg) const;
bool operator<(const Fixed& arg) const;
bool operator>=(const Fixed& arg) const;
bool operator<=(const Fixed& arg) const;
bool operator==(const Fixed& arg) const;
bool operator!=(const Fixed& arg) const;
Fixed operator+(const Fixed& arg) const;
Fixed operator-(const Fixed& arg) const;
Fixed operator*(const Fixed& arg) const;
Fixed operator/(const Fixed& arg) const;
Fixed operator++(int);
Fixed operator--(int);
Fixed &operator++(void);
Fixed &operator--(void);
static Fixed &min(Fixed& arg, Fixed& argTwo);
static const Fixed &min(const Fixed& arg, const Fixed& argTwo);
static Fixed &max(Fixed& arg, Fixed& argTwo);
static const Fixed &max(const Fixed& arg, const Fixed& argTwo);
private:
int _value;
const static int _fraction = 8;
};
std::ostream &operator<<(std::ostream &out, const Fixed &nbr);
#endif

35
cpp02/ex02/Makefile Normal file
View File

@ -0,0 +1,35 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/06/22 15:30:34 by apommier #+# #+# #
# Updated: 2022/06/22 15:30:36 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
Fixed.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror
RM = rm -rf
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

49
cpp02/ex02/main.cpp Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 15:31:09 by apommier #+# #+# */
/* Updated: 2022/07/13 03:54:43 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
int main( void )
{
Fixed a;
Fixed const b( Fixed( 5.05f ) * Fixed( 2 ) );
float test = a.getRawBits();
std::cout << test << std::endl;
std::cout << "1--" << std::endl;
std::cout << ++test << std::endl;
std::cout << "2--" << std::endl;
std::cout << test << std::endl;
std::cout << "3--" << std::endl;
std::cout << test++ << std::endl;
std::cout << "4--" << std::endl;
std::cout << test << std::endl;
std::cout << "5--" << std::endl;
std::cout << a << std::endl;
std::cout << "1" << std::endl;
std::cout << ++a << std::endl;
std::cout << "2" << std::endl;
std::cout << a << std::endl;
std::cout << "3" << std::endl;
std::cout << a++ << std::endl;
std::cout << "4" << std::endl;
std::cout << a << std::endl;
std::cout << "5" << std::endl;
std::cout << b << std::endl;
std::cout << Fixed::max( a, b ) << std::endl;
return (0);
}

117
cpp03/ex00/ClapTrap.cpp Normal file
View File

@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:49 by apommier #+# #+# */
/* Updated: 2022/07/14 01:16:51 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
//constructor destructor and assignation
ClapTrap::ClapTrap(std::string name)
{
std::cout << "ClapTrap " << name << " Default 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);
}
/*std::string ClapTrap::setName(std::string name)
{
this->_name = name;
}
void ClapTrap::setHitPoints(int hitPoints)
{
this->_hitPoints = hitPoints;
}
void ClapTrap::setEnergyPoints(int energyPoints)
{
this->_energyPoints = energyPoints;
}
void ClapTrap::setAttackDamage(int attackDamage)
{
this->_attackDamage = 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
std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
}
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;
this->_hitPoints -= amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
}
void ClapTrap::beRepaired(unsigned int amount)
{
std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl;
this->_hitPoints += amount;
}

48
cpp03/ex00/ClapTrap.hpp Normal file
View File

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:53 by apommier #+# #+# */
/* Updated: 2022/07/14 01:10:29 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;
/*std::string setName(std::string name);
int setHitPoints(int hitPoints);
int setEnergyPoints(int energyPoints);
int setAttackDamage(int attackDamage);*/
private:
std::string _name;
int _hitPoints;
int _energyPoints;
int _attackDamage;
};
#endif

35
cpp03/ex00/Makefile Normal file
View File

@ -0,0 +1,35 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/07/13 05:22:57 by apommier #+# #+# #
# Updated: 2022/07/13 05:23:09 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
ClapTrap.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror
RM = rm -rf
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

25
cpp03/ex00/main.cpp Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:55 by apommier #+# #+# */
/* Updated: 2022/07/13 06:53:00 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
int main()
{
ClapTrap John("John");
John.attack("Jean");
John.takeDamage(5);
John.takeDamage(4);
John.takeDamage(1);
John.takeDamage(1);
John.attack("Jean");
return (0);
}

117
cpp03/ex01/ClapTrap.cpp Normal file
View File

@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:49 by apommier #+# #+# */
/* Updated: 2022/07/14 01:16:39 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
//constructor destructor and assignation
ClapTrap::ClapTrap(std::string name)
{
std::cout << "ClapTrap " << name << " Default 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);
}
/*std::string ClapTrap::setName(std::string name)
{
this->_name = name;
}
void ClapTrap::setHitPoints(int hitPoints)
{
this->_hitPoints = hitPoints;
}
void ClapTrap::setEnergyPoints(int energyPoints)
{
this->_energyPoints = energyPoints;
}
void ClapTrap::setAttackDamage(int attackDamage)
{
this->_attackDamage = 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
std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
}
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;
this->_hitPoints -= amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
}
void ClapTrap::beRepaired(unsigned int amount)
{
std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl;
this->_hitPoints += amount;
}

48
cpp03/ex01/ClapTrap.hpp Normal file
View File

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:53 by apommier #+# #+# */
/* Updated: 2022/07/13 13:13:24 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;
/*std::string setName(std::string name);
int setHitPoints(int hitPoints);
int setEnergyPoints(int energyPoints);
int setAttackDamage(int attackDamage);*/
protected:
std::string _name;
int _hitPoints = 100;
int _energyPoints = 50;
int _attackDamage = 20;
};
#endif

36
cpp03/ex01/Makefile Normal file
View File

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

50
cpp03/ex01/ScavTrap.cpp Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:51 by apommier #+# #+# */
/* Updated: 2022/07/14 01:17:00 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
ScavTrap::ScavTrap(std::string name) : ClapTrap(name)
{
std::cout << "ScavTrap " << this->_name << " Default constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}
ScavTrap::ScavTrap(const ScavTrap& copy) : ClapTrap(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;
}
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()
{
std::cout << "ScavTrap " << this->_name << " came into Gatekeeper mode\n";
}

34
cpp03/ex01/ScavTrap.hpp Normal file
View File

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

37
cpp03/ex01/main.cpp Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:55 by apommier #+# #+# */
/* Updated: 2022/07/13 12:06:07 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
#include "ScavTrap.hpp"
int main()
{
ClapTrap John("John");
ScavTrap ScavJohn("CL4P-TP");
John.attack("Jean");
John.takeDamage(5);
John.takeDamage(4);
John.takeDamage(1);
John.takeDamage(1);
John.attack("Jean");
ScavJohn.guardGate();
ScavJohn.attack("Jean");
ScavJohn.takeDamage(5);
ScavJohn.takeDamage(4);
ScavJohn.takeDamage(1);
ScavJohn.takeDamage(1);
ScavJohn.attack("Jean");
return (0);
}

117
cpp03/ex02/ClapTrap.cpp Normal file
View File

@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:49 by apommier #+# #+# */
/* Updated: 2022/07/14 01:23:11 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
//constructor destructor and assignation
ClapTrap::ClapTrap(std::string name)
{
std::cout << "ClapTrap " << name << " Default 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);
}
/*std::string ClapTrap::setName(std::string name)
{
this->_name = name;
}
void ClapTrap::setHitPoints(int hitPoints)
{
this->_hitPoints = hitPoints;
}
void ClapTrap::setEnergyPoints(int energyPoints)
{
this->_energyPoints = energyPoints;
}
void ClapTrap::setAttackDamage(int attackDamage)
{
this->_attackDamage = 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
std::cout << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
}
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;
this->_hitPoints -= amount;
std::cout << "ClapTrap " << this->_name << " have " << this->_hitPoints << " hit points" << std::endl;
}
void ClapTrap::beRepaired(unsigned int amount)
{
std::cout << "ClapTrap " << this->_name << " regain " << amount << " hit point(s)!" << std::endl;
this->_hitPoints += amount;
}

48
cpp03/ex02/ClapTrap.hpp Normal file
View File

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:53 by apommier #+# #+# */
/* Updated: 2022/07/13 13:13:24 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;
/*std::string setName(std::string name);
int setHitPoints(int hitPoints);
int setEnergyPoints(int energyPoints);
int setAttackDamage(int attackDamage);*/
protected:
std::string _name;
int _hitPoints = 100;
int _energyPoints = 50;
int _attackDamage = 20;
};
#endif

50
cpp03/ex02/FragTrap.cpp Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 01:24:31 by apommier #+# #+# */
/* Updated: 2022/07/14 01:55:06 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "FragTrap.hpp"
FragTrap::FragTrap(std::string name) : ClapTrap(name)
{
std::cout << "FragTrap " << this->_name << " Default 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;
}
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)
{
std::cout << "FragTrap " << this->_name << " ask you for a highfive\n";
}

38
cpp03/ex02/FragTrap.hpp Normal file
View File

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

37
cpp03/ex02/Makefile Normal file
View File

@ -0,0 +1,37 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/07/13 05:22:57 by apommier #+# #+# #
# Updated: 2022/07/14 01:54:37 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
ClapTrap.cpp\
ScavTrap.cpp\
FragTrap.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror
RM = rm -rf
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

50
cpp03/ex02/ScavTrap.cpp Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:51 by apommier #+# #+# */
/* Updated: 2022/07/14 01:23:29 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
ScavTrap::ScavTrap(std::string name) : ClapTrap(name)
{
std::cout << "ScavTrap " << this->_name << " Default constructor called" << std::endl;
this->_hitPoints = 100;
this->_energyPoints = 50;
this->_attackDamage = 20;
}
ScavTrap::ScavTrap(const ScavTrap& copy) : ClapTrap(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;
}
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()
{
std::cout << "ScavTrap " << this->_name << " came into Gatekeeper mode\n";
}

34
cpp03/ex02/ScavTrap.hpp Normal file
View File

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

51
cpp03/ex02/main.cpp Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:55 by apommier #+# #+# */
/* Updated: 2022/07/14 01:58:00 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
#include "ScavTrap.hpp"
#include "FragTrap.hpp"
int main()
{
ClapTrap John("John");
John.attack("Jean");
John.takeDamage(5);
John.takeDamage(4);
John.takeDamage(1);
John.takeDamage(1);
John.attack("Jean");
std::cout << std::endl;
ScavTrap ScavJohn("CL4P-TP");
ScavJohn.guardGate();
ScavJohn.attack("Jean");
ScavJohn.takeDamage(5);
ScavJohn.takeDamage(4);
ScavJohn.takeDamage(1);
ScavJohn.takeDamage(1);
ScavJohn.attack("Jean");
std::cout << std::endl;
FragTrap FragJohn("Assassin");
FragJohn.highFivesGuys();
FragJohn.attack("Jean");
FragJohn.takeDamage(5);
FragJohn.takeDamage(4);
FragJohn.takeDamage(1);
FragJohn.takeDamage(1);
FragJohn.attack("Jean");
return (0);
}