narnaud
3 years ago
35 changed files with 1361 additions and 17 deletions
@ -0,0 +1,20 @@ |
|||||
|
NAME = harl |
||||
|
SRCS = main.cpp Harl.cpp |
||||
|
OBJS = ${SRCS:.cpp=.o} |
||||
|
|
||||
|
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
c++ $(OBJS) -o $(NAME) |
||||
|
|
||||
|
all: $(NAME) |
||||
|
|
||||
|
clean: |
||||
|
rm -rf $(OBJS) |
||||
|
|
||||
|
fclean: clean |
||||
|
rm -rf $(NAME) |
||||
|
|
||||
|
re: fclean all |
||||
|
|
||||
|
.PHONY: all clean fclean re |
@ -0,0 +1,56 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* Fixed.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/20 10:42:53 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "Fixed.hpp" |
||||
|
|
||||
|
const int Fixed::_fract_bits = 8; |
||||
|
|
||||
|
Fixed::Fixed (void) { |
||||
|
|
||||
|
std::cout << "Fixed default constructor called " << std::endl; |
||||
|
_raw_bits = 0; |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (const int init_val):_raw_bits(init_val) { |
||||
|
|
||||
|
std::cout << "Fixed parameter constructor called" << std::endl; |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (Fixed const & src) { |
||||
|
|
||||
|
std::cout << "Fixed copy constructor called" << std::endl; |
||||
|
_raw_bits = src.getRawBits(); |
||||
|
} |
||||
|
|
||||
|
Fixed & Fixed::operator= (Fixed const & src) { |
||||
|
|
||||
|
std::cout << "Fixed assignment operator called" << std::endl; |
||||
|
_raw_bits = src.getRawBits(); |
||||
|
return (*this); |
||||
|
} |
||||
|
|
||||
|
Fixed::~Fixed (void) { |
||||
|
|
||||
|
std::cout << "Fixed default destructor called" << std::endl; |
||||
|
} |
||||
|
|
||||
|
int Fixed::getRawBits(void) const |
||||
|
{ |
||||
|
std::cout << "getRawBits member function called" << std::endl; |
||||
|
return (_raw_bits); |
||||
|
} |
||||
|
|
||||
|
void Fixed::setRawBits(int const raw) |
||||
|
{ |
||||
|
std::cout << "setRawBits member function called" << std::endl; |
||||
|
_raw_bits = raw; |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* Fixed.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:06:37 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/20 10:38:33 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <iostream> |
||||
|
|
||||
|
class Fixed { |
||||
|
|
||||
|
public: |
||||
|
|
||||
|
Fixed (void); |
||||
|
Fixed (const int init_val); |
||||
|
Fixed (Fixed const & src); |
||||
|
~Fixed (void); |
||||
|
Fixed & operator= (Fixed const & src); |
||||
|
int getRawBits(void) const; |
||||
|
void setRawBits(int const raw); |
||||
|
private: |
||||
|
int _raw_bits; |
||||
|
static const int _fract_bits; |
||||
|
|
||||
|
}; |
@ -0,0 +1,20 @@ |
|||||
|
NAME = fixed |
||||
|
SRCS = main.cpp Fixed.cpp |
||||
|
OBJS = $(SRCS:.cpp=.o) |
||||
|
|
||||
|
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
c++ $(OBJS) -o $(NAME) |
||||
|
|
||||
|
all: $(NAME) |
||||
|
|
||||
|
clean: |
||||
|
rm -rf $(OBJS) |
||||
|
|
||||
|
fclean: clean |
||||
|
rm -rf $(NAME) |
||||
|
|
||||
|
re: fclean all |
||||
|
|
||||
|
.PHONY: all clean fclean re |
@ -0,0 +1,26 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* main.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:07:23 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/20 10:37:27 by narnaud ### ########.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; |
||||
|
} |
@ -0,0 +1,80 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* Fixed.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/20 14:15:11 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "Fixed.hpp" |
||||
|
|
||||
|
const int Fixed::_fract_bits = 8; |
||||
|
|
||||
|
Fixed::Fixed (void) { |
||||
|
|
||||
|
std::cout << "Fixed default constructor called " << std::endl; |
||||
|
_raw_bits = 0; |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (const int init_int) { |
||||
|
|
||||
|
std::cout << "Fixed constructor from interger called " << std::endl; |
||||
|
_raw_bits = init_int * (1 << _fract_bits); |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (const float init_float) { |
||||
|
|
||||
|
std::cout << "Fixed constructor from float called " << std::endl; |
||||
|
_raw_bits = std::roundf(init_float * (1 << _fract_bits)); |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (Fixed const & src) { |
||||
|
|
||||
|
std::cout << "Fixed copy constructor called" << std::endl; |
||||
|
_raw_bits = src.getRawBits(); |
||||
|
} |
||||
|
|
||||
|
Fixed & Fixed::operator= (Fixed const & src) { |
||||
|
|
||||
|
std::cout << "Fixed assignment operator called" << std::endl; |
||||
|
_raw_bits = src.getRawBits(); |
||||
|
return (*this); |
||||
|
} |
||||
|
|
||||
|
Fixed::~Fixed (void) { |
||||
|
|
||||
|
std::cout << "Fixed default destructor called" << std::endl; |
||||
|
} |
||||
|
|
||||
|
int Fixed::getRawBits(void) const |
||||
|
{ |
||||
|
std::cout << "getRawBits member function called" << std::endl; |
||||
|
return (_raw_bits); |
||||
|
} |
||||
|
|
||||
|
void Fixed::setRawBits(int const raw) |
||||
|
{ |
||||
|
std::cout << "setRawBits member function called" << std::endl; |
||||
|
_raw_bits = raw; |
||||
|
} |
||||
|
|
||||
|
float Fixed::toFloat(void) const { |
||||
|
|
||||
|
//std::cout << "toFloat member function called" << std::endl;
|
||||
|
return ((float)((float)_raw_bits / (float)(1 << _fract_bits))); |
||||
|
} |
||||
|
|
||||
|
int Fixed::toInt(void) const { |
||||
|
|
||||
|
//std::cout << "toInt member function called" << std::endl;
|
||||
|
return ((int)((float)_raw_bits / (float)(1 << _fract_bits))); |
||||
|
} |
||||
|
|
||||
|
std::ostream & operator << (std::ostream &out, const Fixed &f){ |
||||
|
out << f.toFloat(); |
||||
|
return (out); |
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* Fixed.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:06:37 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/20 14:49:38 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <iostream> |
||||
|
#include <cmath> |
||||
|
|
||||
|
class Fixed { |
||||
|
|
||||
|
public: |
||||
|
|
||||
|
Fixed (void); |
||||
|
Fixed (const int init_int); |
||||
|
Fixed (const float init_float); |
||||
|
Fixed (Fixed const & src); |
||||
|
~Fixed (void); |
||||
|
Fixed & operator= (Fixed const & src); |
||||
|
int getRawBits(void) const; |
||||
|
void setRawBits(int const raw); |
||||
|
float toFloat(void) const; |
||||
|
int toInt(void) const; |
||||
|
private: |
||||
|
int _raw_bits; |
||||
|
static const int _fract_bits; |
||||
|
|
||||
|
}; |
||||
|
std::ostream & operator << (std::ostream &out, const Fixed &f); |
@ -0,0 +1,20 @@ |
|||||
|
NAME = fixed |
||||
|
SRCS = main.cpp Fixed.cpp |
||||
|
OBJS = $(SRCS:.cpp=.o) |
||||
|
|
||||
|
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
c++ $(OBJS) -o $(NAME) |
||||
|
|
||||
|
all: $(NAME) |
||||
|
|
||||
|
clean: |
||||
|
rm -rf $(OBJS) |
||||
|
|
||||
|
fclean: clean |
||||
|
rm -rf $(NAME) |
||||
|
|
||||
|
re: fclean all |
||||
|
|
||||
|
.PHONY: all clean fclean re |
@ -0,0 +1,40 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* main.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:07:23 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/20 13:35:26 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "Fixed.hpp" |
||||
|
|
||||
|
int main(void) |
||||
|
{ |
||||
|
|
||||
|
Fixed a; |
||||
|
Fixed const b( 10 ); |
||||
|
Fixed const c( 42.42f ); |
||||
|
Fixed const d( b ); |
||||
|
Fixed e; |
||||
|
e = d; |
||||
|
|
||||
|
a = Fixed( 1234.4321f ); |
||||
|
|
||||
|
std::cout << a.getRawBits() << std::endl; |
||||
|
std::cout << b.getRawBits() << std::endl; |
||||
|
std::cout << c.getRawBits() << std::endl; |
||||
|
|
||||
|
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; |
||||
|
} |
@ -0,0 +1,149 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* Fixed.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 10:41:09 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "Fixed.hpp" |
||||
|
|
||||
|
const int Fixed::_fract_bits = 8; |
||||
|
|
||||
|
Fixed::Fixed (void) { |
||||
|
_raw_bits = 0; |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (const int init_int) { |
||||
|
_raw_bits = init_int * (1 << _fract_bits); |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (const float init_float) { |
||||
|
_raw_bits = std::roundf(init_float * (1 << _fract_bits)); |
||||
|
} |
||||
|
|
||||
|
Fixed::Fixed (Fixed const & src) { |
||||
|
_raw_bits = src.getRawBits(); |
||||
|
} |
||||
|
|
||||
|
Fixed::~Fixed (void) { |
||||
|
} |
||||
|
|
||||
|
int Fixed::getRawBits(void) const { |
||||
|
return (_raw_bits); |
||||
|
} |
||||
|
|
||||
|
void Fixed::setRawBits(int const raw) { |
||||
|
_raw_bits = raw; |
||||
|
} |
||||
|
|
||||
|
float Fixed::toFloat(void) const { |
||||
|
return ((float)((float)_raw_bits / (float)(1 << _fract_bits))); |
||||
|
} |
||||
|
|
||||
|
int Fixed::toInt(void) const { |
||||
|
return ((int)((float)_raw_bits / (float)(1 << _fract_bits))); |
||||
|
} |
||||
|
|
||||
|
Fixed & Fixed::operator= (Fixed const & src) { |
||||
|
_raw_bits = src.getRawBits(); |
||||
|
return (*this); |
||||
|
} |
||||
|
|
||||
|
Fixed Fixed::operator+ (Fixed const & src) { |
||||
|
return (Fixed(_raw_bits + src.getRawBits())); |
||||
|
} |
||||
|
|
||||
|
Fixed Fixed::operator- (Fixed const & src) { |
||||
|
return (Fixed(_raw_bits - src.getRawBits())); |
||||
|
} |
||||
|
|
||||
|
Fixed Fixed::operator * (Fixed const & src) { |
||||
|
return (Fixed(this->toFloat() * src.toFloat())); |
||||
|
} |
||||
|
|
||||
|
Fixed Fixed::operator / (Fixed const & src) { |
||||
|
return (Fixed(this->toFloat() / src.toFloat())); |
||||
|
} |
||||
|
|
||||
|
Fixed & Fixed::operator++ (void) { |
||||
|
_raw_bits++; |
||||
|
return (*this); |
||||
|
} |
||||
|
|
||||
|
Fixed & Fixed::operator-- (void) { |
||||
|
_raw_bits--; |
||||
|
return (*this); |
||||
|
} |
||||
|
Fixed Fixed::operator++ (int) { |
||||
|
Fixed ret; |
||||
|
ret.setRawBits(_raw_bits++); |
||||
|
return (ret); |
||||
|
} |
||||
|
|
||||
|
Fixed Fixed::operator-- (int) { |
||||
|
Fixed ret; |
||||
|
ret.setRawBits(_raw_bits++); |
||||
|
return (ret); |
||||
|
} |
||||
|
|
||||
|
bool Fixed::operator< (Fixed const & src) { |
||||
|
return (_raw_bits < src.getRawBits()); |
||||
|
} |
||||
|
|
||||
|
bool Fixed::operator> (Fixed const & src) { |
||||
|
return (_raw_bits > src.getRawBits()); |
||||
|
} |
||||
|
|
||||
|
bool Fixed::operator<= (Fixed const & src) { |
||||
|
return (_raw_bits <= src.getRawBits()); |
||||
|
} |
||||
|
|
||||
|
bool Fixed::operator>= (Fixed const & src) { |
||||
|
return (_raw_bits >= src.getRawBits()); |
||||
|
} |
||||
|
|
||||
|
bool Fixed::operator== (Fixed const & src) { |
||||
|
return (_raw_bits == src.getRawBits()); |
||||
|
} |
||||
|
|
||||
|
bool Fixed::operator!= (Fixed const & src) { |
||||
|
return (_raw_bits != src.getRawBits()); |
||||
|
} |
||||
|
|
||||
|
std::ostream & operator << (std::ostream &out, const Fixed &f){ |
||||
|
out << f.toFloat(); |
||||
|
return (out); |
||||
|
} |
||||
|
|
||||
|
Fixed & Fixed::min(Fixed & a, Fixed & b) { |
||||
|
if (a < b) |
||||
|
return (a); |
||||
|
else |
||||
|
return (b); |
||||
|
} |
||||
|
|
||||
|
Fixed & Fixed::max(Fixed & a, Fixed & b) { |
||||
|
if (a > b) |
||||
|
return (a); |
||||
|
else |
||||
|
return (b); |
||||
|
} |
||||
|
|
||||
|
const Fixed & Fixed::min(Fixed const & a, Fixed const & b){ |
||||
|
if (a.getRawBits() < b.getRawBits()) |
||||
|
return (a); |
||||
|
else |
||||
|
return (b); |
||||
|
} |
||||
|
|
||||
|
const Fixed & Fixed::max(Fixed const & a, Fixed const & b){ |
||||
|
if (a.getRawBits() > b.getRawBits()) |
||||
|
return (a); |
||||
|
else |
||||
|
return (b); |
||||
|
} |
@ -0,0 +1,53 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* Fixed.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:06:37 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 10:33:51 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <iostream> |
||||
|
#include <cmath> |
||||
|
|
||||
|
class Fixed { |
||||
|
|
||||
|
public: |
||||
|
Fixed (void); |
||||
|
Fixed (const int init_int); |
||||
|
Fixed (const float init_float); |
||||
|
Fixed (Fixed const & src); |
||||
|
~Fixed (void); |
||||
|
Fixed & operator= (Fixed const & src); |
||||
|
Fixed operator+ (Fixed const & src); |
||||
|
Fixed operator- (Fixed const & src); |
||||
|
Fixed operator* (Fixed const & src); |
||||
|
Fixed operator/ (Fixed const & src); |
||||
|
Fixed & operator++ (); |
||||
|
Fixed & operator-- (); |
||||
|
Fixed operator++ (int); |
||||
|
Fixed operator-- (int); |
||||
|
bool operator< (Fixed const & src); |
||||
|
bool operator> (Fixed const & src); |
||||
|
bool operator<= (Fixed const & src); |
||||
|
bool operator>= (Fixed const & src); |
||||
|
bool operator== (Fixed const & src); |
||||
|
bool operator!= (Fixed const & src); |
||||
|
int getRawBits(void) const; |
||||
|
void setRawBits(int const raw); |
||||
|
float toFloat(void) const; |
||||
|
int toInt(void) const; |
||||
|
static Fixed & min(Fixed & a, Fixed & b); |
||||
|
static Fixed & max(Fixed & a, Fixed & b); |
||||
|
static const Fixed & min(Fixed const & a, Fixed const & b); |
||||
|
static const Fixed & max(Fixed const & a, Fixed const & b); |
||||
|
private: |
||||
|
int _raw_bits; |
||||
|
static const int _fract_bits; |
||||
|
}; |
||||
|
std::ostream & operator << (std::ostream &out, const Fixed &f); |
@ -0,0 +1,20 @@ |
|||||
|
NAME = fixed |
||||
|
SRCS = main.cpp Fixed.cpp |
||||
|
OBJS = $(SRCS:.cpp=.o) |
||||
|
|
||||
|
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
c++ $(OBJS) -o $(NAME) |
||||
|
|
||||
|
all: $(NAME) |
||||
|
|
||||
|
clean: |
||||
|
rm -rf $(OBJS) |
||||
|
|
||||
|
fclean: clean |
||||
|
rm -rf $(NAME) |
||||
|
|
||||
|
re: fclean all |
||||
|
|
||||
|
.PHONY: all clean fclean re |
@ -0,0 +1,28 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* main.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/20 10:07:23 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 09:47:47 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "Fixed.hpp" |
||||
|
|
||||
|
int main(void) |
||||
|
{ |
||||
|
Fixed a; |
||||
|
Fixed const b( Fixed( 5.05f ) * Fixed( 2 ) ); |
||||
|
|
||||
|
std::cout << a << std::endl; |
||||
|
std::cout << ++a << std::endl; |
||||
|
std::cout << a << std::endl; |
||||
|
std::cout << a++ << std::endl; |
||||
|
std::cout << a << std::endl; |
||||
|
std::cout << b << std::endl; |
||||
|
std::cout << Fixed::max( a, b ) << std::endl; |
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,96 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ClapTrap.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:47:34 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ClapTrap.hpp" |
||||
|
|
||||
|
int ClapTrap::amount = 0; |
||||
|
ClapTrap *ClapTrap::registery[128] = {NULL}; |
||||
|
|
||||
|
|
||||
|
ClapTrap::ClapTrap (void) { |
||||
|
|
||||
|
std::cout << "ClapTrap default constructor called " << endl; |
||||
|
} |
||||
|
|
||||
|
ClapTrap::ClapTrap (std::string name, size_t hp, size_t ep, size_t atk) |
||||
|
: _name(name), _health(hp), _energy(ep), _attack(atk) { |
||||
|
if (ClapTrap::amount >= 128) |
||||
|
{ |
||||
|
delete ClapTrap::registery[amount % 128]; |
||||
|
cout << "There are too many Traps " << ClapTrap::registery[amount % 128]->getName() << " destroyed\n"; |
||||
|
} |
||||
|
cout << "ClapTrap " << name << " was created" << endl; |
||||
|
_id = ClapTrap::amount % 128; |
||||
|
ClapTrap::registery[ClapTrap::amount % 128] = this; |
||||
|
ClapTrap::amount++; |
||||
|
} |
||||
|
|
||||
|
ClapTrap::ClapTrap (ClapTrap const & src) { |
||||
|
_name = src.getName(); |
||||
|
} |
||||
|
|
||||
|
ClapTrap & ClapTrap::operator= (ClapTrap const & src) { |
||||
|
std::cout << "ClapTrap assignment operator called" << endl; |
||||
|
_name = src.getName(); |
||||
|
return (*this); |
||||
|
} |
||||
|
|
||||
|
ClapTrap::~ClapTrap (void) { |
||||
|
|
||||
|
std::cout << "ClapTrap " << _name << " was destroyed" << endl; |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::attack(const std::string& target) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to attack anyone" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " attacked " << target << endl; |
||||
|
getClapTrap(target)->takeDamage(_attack); |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::takeDamage(unsigned int amount) { |
||||
|
if (_health == 0) |
||||
|
cout << _name << " is already death" << endl; |
||||
|
else |
||||
|
cout << _name << " lose " << amount << " hp" << endl; |
||||
|
if (amount >= _health) |
||||
|
_health = 0; |
||||
|
else |
||||
|
_health -= amount; |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::beRepaired(unsigned int amount) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to repair anyone" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " was repaired for " << amount << " hp" << endl; |
||||
|
_health += amount; |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
std::string ClapTrap::getName(void) const { |
||||
|
return (_name); |
||||
|
} |
||||
|
|
||||
|
ClapTrap *ClapTrap::getClapTrap(std::string name) { |
||||
|
for (int i = 0; i < 128; i++) |
||||
|
{ |
||||
|
if (name == ClapTrap::registery[i]->getName()) |
||||
|
return (ClapTrap::registery[i]); |
||||
|
} |
||||
|
return (NULL); |
||||
|
} |
@ -0,0 +1,46 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ClapTrap.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:21:48 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
#define CT_HEALTH 10 |
||||
|
#define CT_ENERGY 10 |
||||
|
#define CT_DMG 1 |
||||
|
|
||||
|
#include <string> |
||||
|
#include <iostream> |
||||
|
using std::cout; |
||||
|
using std::endl; |
||||
|
|
||||
|
class ClapTrap { |
||||
|
|
||||
|
public: |
||||
|
static ClapTrap *registery[128]; |
||||
|
static int amount; |
||||
|
ClapTrap (void); |
||||
|
ClapTrap (std::string name, size_t hp = CT_HEALTH,\ |
||||
|
size_t ep = CT_ENERGY, size_t atk = CT_DMG); |
||||
|
ClapTrap (ClapTrap const & src); |
||||
|
virtual ~ClapTrap (void); |
||||
|
ClapTrap & operator= (ClapTrap const & src); |
||||
|
virtual void attack(const std::string& target); |
||||
|
void takeDamage(unsigned int amount); |
||||
|
void beRepaired(unsigned int amount); |
||||
|
std::string getName(void) const; |
||||
|
static ClapTrap *getClapTrap(std::string name); |
||||
|
|
||||
|
protected: |
||||
|
std::string _name; |
||||
|
unsigned int _health; |
||||
|
unsigned int _energy; |
||||
|
unsigned int _attack; |
||||
|
unsigned int _id; |
||||
|
}; |
@ -0,0 +1,20 @@ |
|||||
|
NAME = claptrap |
||||
|
SRCS = main.cpp ClapTrap.cpp |
||||
|
OBJS = $(SRCS:.cpp=.o) |
||||
|
|
||||
|
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
c++ $(OBJS) -o $(NAME) |
||||
|
|
||||
|
all : $(NAME) |
||||
|
|
||||
|
clean : |
||||
|
rm -rf $(OBJS) |
||||
|
|
||||
|
fclean : clean |
||||
|
rm -rf $(NAME) |
||||
|
|
||||
|
re : fclean all |
||||
|
|
||||
|
.PHONY: all clean fclean re |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* main.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:53:16 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 12:46:19 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ClapTrap.hpp" |
||||
|
|
||||
|
int main(void) |
||||
|
{ |
||||
|
ClapTrap a("A"); |
||||
|
ClapTrap b("B"); |
||||
|
|
||||
|
a.attack("B"); |
||||
|
} |
@ -0,0 +1,96 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ClapTrap.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:47:34 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ClapTrap.hpp" |
||||
|
|
||||
|
int ClapTrap::amount = 0; |
||||
|
ClapTrap *ClapTrap::registery[128] = {NULL}; |
||||
|
|
||||
|
|
||||
|
ClapTrap::ClapTrap (void) { |
||||
|
|
||||
|
std::cout << "ClapTrap default constructor called " << endl; |
||||
|
} |
||||
|
|
||||
|
ClapTrap::ClapTrap (std::string name, size_t hp, size_t ep, size_t atk) |
||||
|
: _name(name), _health(hp), _energy(ep), _attack(atk) { |
||||
|
if (ClapTrap::amount >= 128) |
||||
|
{ |
||||
|
delete ClapTrap::registery[amount % 128]; |
||||
|
cout << "There are too many Traps " << ClapTrap::registery[amount % 128]->getName() << " destroyed\n"; |
||||
|
} |
||||
|
cout << "ClapTrap " << name << " was created" << endl; |
||||
|
_id = ClapTrap::amount % 128; |
||||
|
ClapTrap::registery[ClapTrap::amount % 128] = this; |
||||
|
ClapTrap::amount++; |
||||
|
} |
||||
|
|
||||
|
ClapTrap::ClapTrap (ClapTrap const & src) { |
||||
|
_name = src.getName(); |
||||
|
} |
||||
|
|
||||
|
ClapTrap & ClapTrap::operator= (ClapTrap const & src) { |
||||
|
std::cout << "ClapTrap assignment operator called" << endl; |
||||
|
_name = src.getName(); |
||||
|
return (*this); |
||||
|
} |
||||
|
|
||||
|
ClapTrap::~ClapTrap (void) { |
||||
|
|
||||
|
std::cout << "ClapTrap " << _name << " was destroyed" << endl; |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::attack(const std::string& target) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to attack anyone" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " attacked " << target << endl; |
||||
|
getClapTrap(target)->takeDamage(_attack); |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::takeDamage(unsigned int amount) { |
||||
|
if (_health == 0) |
||||
|
cout << _name << " is already death" << endl; |
||||
|
else |
||||
|
cout << _name << " lose " << amount << " hp" << endl; |
||||
|
if (amount >= _health) |
||||
|
_health = 0; |
||||
|
else |
||||
|
_health -= amount; |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::beRepaired(unsigned int amount) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to repair anyone" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " was repaired for " << amount << " hp" << endl; |
||||
|
_health += amount; |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
std::string ClapTrap::getName(void) const { |
||||
|
return (_name); |
||||
|
} |
||||
|
|
||||
|
ClapTrap *ClapTrap::getClapTrap(std::string name) { |
||||
|
for (int i = 0; i < 128; i++) |
||||
|
{ |
||||
|
if (name == ClapTrap::registery[i]->getName()) |
||||
|
return (ClapTrap::registery[i]); |
||||
|
} |
||||
|
return (NULL); |
||||
|
} |
@ -0,0 +1,46 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ClapTrap.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:21:48 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
#define CT_HEALTH 10 |
||||
|
#define CT_ENERGY 10 |
||||
|
#define CT_DMG 1 |
||||
|
|
||||
|
#include <string> |
||||
|
#include <iostream> |
||||
|
using std::cout; |
||||
|
using std::endl; |
||||
|
|
||||
|
class ClapTrap { |
||||
|
|
||||
|
public: |
||||
|
static ClapTrap *registery[128]; |
||||
|
static int amount; |
||||
|
ClapTrap (void); |
||||
|
ClapTrap (std::string name, size_t hp = CT_HEALTH,\ |
||||
|
size_t ep = CT_ENERGY, size_t atk = CT_DMG); |
||||
|
ClapTrap (ClapTrap const & src); |
||||
|
virtual ~ClapTrap (void); |
||||
|
ClapTrap & operator= (ClapTrap const & src); |
||||
|
virtual void attack(const std::string& target); |
||||
|
void takeDamage(unsigned int amount); |
||||
|
void beRepaired(unsigned int amount); |
||||
|
std::string getName(void) const; |
||||
|
static ClapTrap *getClapTrap(std::string name); |
||||
|
|
||||
|
protected: |
||||
|
std::string _name; |
||||
|
unsigned int _health; |
||||
|
unsigned int _energy; |
||||
|
unsigned int _attack; |
||||
|
unsigned int _id; |
||||
|
}; |
@ -0,0 +1,20 @@ |
|||||
|
NAME = scavtrap |
||||
|
SRCS = main.cpp ClapTrap.cpp ScavTrap.cpp |
||||
|
OBJS = $(SRCS:.cpp=.o) |
||||
|
|
||||
|
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
c++ $(OBJS) -o $(NAME) |
||||
|
|
||||
|
all : $(NAME) |
||||
|
|
||||
|
clean : |
||||
|
rm -rf $(OBJS) |
||||
|
|
||||
|
fclean : clean |
||||
|
rm -rf $(NAME) |
||||
|
|
||||
|
re : fclean all |
||||
|
|
||||
|
.PHONY: all clean fclean re |
@ -0,0 +1,46 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ScavTrap.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:55:34 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ScavTrap.hpp" |
||||
|
|
||||
|
ScavTrap::ScavTrap (std::string name, size_t hp, size_t ep, size_t atk):ClapTrap(name, hp, ep, atk){ |
||||
|
if (ScavTrap::amount >= 128) |
||||
|
{ |
||||
|
delete ScavTrap::registery[amount % 128]; |
||||
|
cout << "There are already too many Traps" << endl; |
||||
|
} |
||||
|
cout << "ScavTrap " << name << " was created" << endl; |
||||
|
ScavTrap::registery[ScavTrap::amount % 128] = this; |
||||
|
ScavTrap::amount++; |
||||
|
} |
||||
|
|
||||
|
ScavTrap::~ScavTrap (void) { |
||||
|
cout << "ScavTrap " << _name << " was destroyed" << endl; |
||||
|
} |
||||
|
|
||||
|
void ScavTrap::attack(const std::string& target) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to attack" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " heavily attacked " << target << endl; |
||||
|
getClapTrap(target)->takeDamage(_attack); |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ScavTrap::guardGate(void) { |
||||
|
if (_health == 0) |
||||
|
cout << _name << " is no more able to guard" << endl; |
||||
|
else |
||||
|
cout << _name << " is now in guard gate mode.\n"; |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ScavTrap.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:03:22 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
#include "ClapTrap.hpp" |
||||
|
#define ST_HEALTH 100 |
||||
|
#define ST_ENERGY 50 |
||||
|
#define ST_DMG 20 |
||||
|
|
||||
|
class ScavTrap : public ClapTrap { |
||||
|
|
||||
|
public: |
||||
|
|
||||
|
ScavTrap (std::string name, size_t hp = ST_HEALTH,\ |
||||
|
size_t ep = ST_ENERGY, size_t atk = ST_DMG); |
||||
|
~ScavTrap (void); |
||||
|
void attack(const std::string& target); |
||||
|
void guardGate(void); |
||||
|
}; |
Binary file not shown.
@ -0,0 +1,30 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* main.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:53:16 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:34:04 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ScavTrap.hpp" |
||||
|
|
||||
|
int main(void) |
||||
|
{ |
||||
|
ClapTrap a("A"); |
||||
|
ClapTrap b("B"); |
||||
|
ScavTrap c("C"); |
||||
|
|
||||
|
a.attack("B"); |
||||
|
b.attack("C"); |
||||
|
c.attack("A"); |
||||
|
a.attack("C"); |
||||
|
b.attack("A"); |
||||
|
c.beRepaired(10); |
||||
|
a.beRepaired(5); |
||||
|
b.attack("C"); |
||||
|
c.guardGate(); |
||||
|
} |
@ -0,0 +1,96 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ClapTrap.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:47:34 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ClapTrap.hpp" |
||||
|
|
||||
|
int ClapTrap::amount = 0; |
||||
|
ClapTrap *ClapTrap::registery[128] = {NULL}; |
||||
|
|
||||
|
|
||||
|
ClapTrap::ClapTrap (void) { |
||||
|
|
||||
|
std::cout << "ClapTrap default constructor called " << endl; |
||||
|
} |
||||
|
|
||||
|
ClapTrap::ClapTrap (std::string name, size_t hp, size_t ep, size_t atk) |
||||
|
: _name(name), _health(hp), _energy(ep), _attack(atk) { |
||||
|
if (ClapTrap::amount >= 128) |
||||
|
{ |
||||
|
delete ClapTrap::registery[amount % 128]; |
||||
|
cout << "There are too many Traps " << ClapTrap::registery[amount % 128]->getName() << " destroyed\n"; |
||||
|
} |
||||
|
cout << "ClapTrap " << name << " was created" << endl; |
||||
|
_id = ClapTrap::amount % 128; |
||||
|
ClapTrap::registery[ClapTrap::amount % 128] = this; |
||||
|
ClapTrap::amount++; |
||||
|
} |
||||
|
|
||||
|
ClapTrap::ClapTrap (ClapTrap const & src) { |
||||
|
_name = src.getName(); |
||||
|
} |
||||
|
|
||||
|
ClapTrap & ClapTrap::operator= (ClapTrap const & src) { |
||||
|
std::cout << "ClapTrap assignment operator called" << endl; |
||||
|
_name = src.getName(); |
||||
|
return (*this); |
||||
|
} |
||||
|
|
||||
|
ClapTrap::~ClapTrap (void) { |
||||
|
|
||||
|
std::cout << "ClapTrap " << _name << " was destroyed" << endl; |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::attack(const std::string& target) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to attack anyone" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " attacked " << target << endl; |
||||
|
getClapTrap(target)->takeDamage(_attack); |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::takeDamage(unsigned int amount) { |
||||
|
if (_health == 0) |
||||
|
cout << _name << " is already death" << endl; |
||||
|
else |
||||
|
cout << _name << " lose " << amount << " hp" << endl; |
||||
|
if (amount >= _health) |
||||
|
_health = 0; |
||||
|
else |
||||
|
_health -= amount; |
||||
|
} |
||||
|
|
||||
|
void ClapTrap::beRepaired(unsigned int amount) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to repair anyone" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " was repaired for " << amount << " hp" << endl; |
||||
|
_health += amount; |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
std::string ClapTrap::getName(void) const { |
||||
|
return (_name); |
||||
|
} |
||||
|
|
||||
|
ClapTrap *ClapTrap::getClapTrap(std::string name) { |
||||
|
for (int i = 0; i < 128; i++) |
||||
|
{ |
||||
|
if (name == ClapTrap::registery[i]->getName()) |
||||
|
return (ClapTrap::registery[i]); |
||||
|
} |
||||
|
return (NULL); |
||||
|
} |
@ -0,0 +1,46 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ClapTrap.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:21:48 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
#define CT_HEALTH 10 |
||||
|
#define CT_ENERGY 10 |
||||
|
#define CT_DMG 1 |
||||
|
|
||||
|
#include <string> |
||||
|
#include <iostream> |
||||
|
using std::cout; |
||||
|
using std::endl; |
||||
|
|
||||
|
class ClapTrap { |
||||
|
|
||||
|
public: |
||||
|
static ClapTrap *registery[128]; |
||||
|
static int amount; |
||||
|
ClapTrap (void); |
||||
|
ClapTrap (std::string name, size_t hp = CT_HEALTH,\ |
||||
|
size_t ep = CT_ENERGY, size_t atk = CT_DMG); |
||||
|
ClapTrap (ClapTrap const & src); |
||||
|
virtual ~ClapTrap (void); |
||||
|
ClapTrap & operator= (ClapTrap const & src); |
||||
|
virtual void attack(const std::string& target); |
||||
|
void takeDamage(unsigned int amount); |
||||
|
void beRepaired(unsigned int amount); |
||||
|
std::string getName(void) const; |
||||
|
static ClapTrap *getClapTrap(std::string name); |
||||
|
|
||||
|
protected: |
||||
|
std::string _name; |
||||
|
unsigned int _health; |
||||
|
unsigned int _energy; |
||||
|
unsigned int _attack; |
||||
|
unsigned int _id; |
||||
|
}; |
@ -0,0 +1,39 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* FragTrap.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 18:04:05 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "FragTrap.hpp" |
||||
|
|
||||
|
FragTrap::FragTrap (std::string name, size_t hp, size_t ep, size_t atk):ClapTrap(name, hp, ep, atk){ |
||||
|
if (FragTrap::amount >= 128) |
||||
|
{ |
||||
|
delete FragTrap::registery[amount % 128]; |
||||
|
cout << "There are already too many Traps" << endl; |
||||
|
} |
||||
|
cout << "FragTrap " << name << " was created" << endl; |
||||
|
FragTrap::registery[FragTrap::amount % 128] = this; |
||||
|
FragTrap::amount++; |
||||
|
} |
||||
|
|
||||
|
FragTrap::~FragTrap (void) { |
||||
|
cout << "FragTrap " << _name << " was destroyed" << endl; |
||||
|
} |
||||
|
|
||||
|
void FragTrap::attack(const std::string& target) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to attack anyone"; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " hardly attacked " << target << endl; |
||||
|
getClapTrap(target)->takeDamage(_attack); |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* FragTrap.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:59:52 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
#include "ClapTrap.hpp" |
||||
|
#define FT_HEALTH 100 |
||||
|
#define FT_ENERGY 100 |
||||
|
#define FT_DMG 30 |
||||
|
|
||||
|
class FragTrap : public ClapTrap { |
||||
|
|
||||
|
public: |
||||
|
|
||||
|
FragTrap (std::string name, size_t hp = FT_HEALTH,\ |
||||
|
size_t ep = FT_ENERGY, size_t atk = FT_DMG); |
||||
|
~FragTrap (void); |
||||
|
void attack(const std::string& target); |
||||
|
}; |
@ -0,0 +1,20 @@ |
|||||
|
NAME = fragtrap |
||||
|
SRCS = main.cpp ClapTrap.cpp ScavTrap.cpp FragTrap.cpp |
||||
|
OBJS = $(SRCS:.cpp=.o) |
||||
|
|
||||
|
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
c++ $(OBJS) -o $(NAME) |
||||
|
|
||||
|
all : $(NAME) |
||||
|
|
||||
|
clean : |
||||
|
rm -rf $(OBJS) |
||||
|
|
||||
|
fclean : clean |
||||
|
rm -rf $(NAME) |
||||
|
|
||||
|
re : fclean all |
||||
|
|
||||
|
.PHONY: all clean fclean re |
@ -0,0 +1,46 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ScavTrap.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:55:34 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ScavTrap.hpp" |
||||
|
|
||||
|
ScavTrap::ScavTrap (std::string name, size_t hp, size_t ep, size_t atk):ClapTrap(name, hp, ep, atk){ |
||||
|
if (ScavTrap::amount >= 128) |
||||
|
{ |
||||
|
delete ScavTrap::registery[amount % 128]; |
||||
|
cout << "There are already too many Traps" << endl; |
||||
|
} |
||||
|
cout << "ScavTrap " << name << " was created" << endl; |
||||
|
ScavTrap::registery[ScavTrap::amount % 128] = this; |
||||
|
ScavTrap::amount++; |
||||
|
} |
||||
|
|
||||
|
ScavTrap::~ScavTrap (void) { |
||||
|
cout << "ScavTrap " << _name << " was destroyed" << endl; |
||||
|
} |
||||
|
|
||||
|
void ScavTrap::attack(const std::string& target) { |
||||
|
if (_energy == 0 || _health == 0) |
||||
|
cout << _name << " is no more able to attack" << endl; |
||||
|
else |
||||
|
{ |
||||
|
cout << _name << " heavily attacked " << target << endl; |
||||
|
getClapTrap(target)->takeDamage(_attack); |
||||
|
_energy--; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ScavTrap::guardGate(void) { |
||||
|
if (_health == 0) |
||||
|
cout << _name << " is no more able to guard" << endl; |
||||
|
else |
||||
|
cout << _name << " is now in guard gate mode.\n"; |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ScavTrap.hpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:03:22 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#pragma once |
||||
|
#include "ClapTrap.hpp" |
||||
|
#define ST_HEALTH 100 |
||||
|
#define ST_ENERGY 50 |
||||
|
#define ST_DMG 20 |
||||
|
|
||||
|
class ScavTrap : public ClapTrap { |
||||
|
|
||||
|
public: |
||||
|
|
||||
|
ScavTrap (std::string name, size_t hp = ST_HEALTH,\ |
||||
|
size_t ep = ST_ENERGY, size_t atk = ST_DMG); |
||||
|
~ScavTrap (void); |
||||
|
void attack(const std::string& target); |
||||
|
void guardGate(void); |
||||
|
}; |
Binary file not shown.
@ -0,0 +1,25 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* main.cpp :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/06/23 10:53:16 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/06/23 17:59:14 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "ScavTrap.hpp" |
||||
|
#include "FragTrap.hpp" |
||||
|
|
||||
|
int main(void) |
||||
|
{ |
||||
|
ClapTrap a("A"); |
||||
|
ScavTrap b("B"); |
||||
|
FragTrap c("C"); |
||||
|
|
||||
|
a.attack("B"); |
||||
|
b.attack("C"); |
||||
|
c.attack("B"); |
||||
|
} |
Loading…
Reference in new issue