diff --git a/CPP02/ex01/Fixed.cpp b/CPP02/ex01/Fixed.cpp index 6658e4c..70e1cc1 100644 --- a/CPP02/ex01/Fixed.cpp +++ b/CPP02/ex01/Fixed.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */ -/* Updated: 2022/06/20 14:15:11 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 14:00:06 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,43 +22,37 @@ Fixed::Fixed (void) { Fixed::Fixed (const int init_int) { - std::cout << "Fixed constructor from interger called " << std::endl; + std::cout << "Integer constructor 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; + std::cout << "Float constructor 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(); + std::cout << "Copy constructor called" << std::endl; + *this = src; } Fixed & Fixed::operator= (Fixed const & src) { - - std::cout << "Fixed assignment operator called" << std::endl; + std::cout << "Copy 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; +int Fixed::getRawBits(void) const { return (_raw_bits); } -void Fixed::setRawBits(int const raw) -{ - std::cout << "setRawBits member function called" << std::endl; +void Fixed::setRawBits(int const raw) { _raw_bits = raw; } diff --git a/CPP02/ex01/main.cpp b/CPP02/ex01/main.cpp index 944f370..67c29d7 100644 --- a/CPP02/ex01/main.cpp +++ b/CPP02/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/20 10:07:23 by narnaud #+# #+# */ -/* Updated: 2022/06/20 13:35:26 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 13:51:53 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,14 +19,14 @@ int main(void) Fixed const b( 10 ); Fixed const c( 42.42f ); Fixed const d( b ); - Fixed e; - e = d; + //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.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; diff --git a/CPP03/ex00/ClapTrap.cpp b/CPP03/ex00/ClapTrap.cpp index e07c0c5..34c19c2 100644 --- a/CPP03/ex00/ClapTrap.cpp +++ b/CPP03/ex00/ClapTrap.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */ -/* Updated: 2022/06/23 17:47:34 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:12:17 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,17 +16,14 @@ int ClapTrap::amount = 0; ClapTrap *ClapTrap::registery[128] = {NULL}; -ClapTrap::ClapTrap (void) { +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) { +ClapTrap::ClapTrap (std::string name) + : _name(name), _health(10), _energy(10), _attack(0) { if (ClapTrap::amount >= 128) { - delete ClapTrap::registery[amount % 128]; cout << "There are too many Traps " << ClapTrap::registery[amount % 128]->getName() << " destroyed\n"; + delete ClapTrap::registery[amount % 128]; } cout << "ClapTrap " << name << " was created" << endl; _id = ClapTrap::amount % 128; @@ -39,15 +36,11 @@ ClapTrap::ClapTrap (ClapTrap const & src) { } 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; -} +ClapTrap::~ClapTrap (void) {} void ClapTrap::attack(const std::string& target) { if (_energy == 0 || _health == 0) @@ -87,10 +80,12 @@ std::string ClapTrap::getName(void) const { } ClapTrap *ClapTrap::getClapTrap(std::string name) { - for (int i = 0; i < 128; i++) + int i = 0; + while (i < ClapTrap::amount && i < 128) { if (name == ClapTrap::registery[i]->getName()) return (ClapTrap::registery[i]); + i++; } return (NULL); } diff --git a/CPP03/ex00/ClapTrap.hpp b/CPP03/ex00/ClapTrap.hpp index 4ce8a2e..ea62e02 100644 --- a/CPP03/ex00/ClapTrap.hpp +++ b/CPP03/ex00/ClapTrap.hpp @@ -6,14 +6,11 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */ -/* Updated: 2022/06/24 07:50:53 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:09:51 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#define CT_HEALTH 10 -#define CT_ENERGY 10 -#define CT_DMG 0 #include #include @@ -26,8 +23,7 @@ class ClapTrap { 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 (std::string name); ClapTrap (ClapTrap const & src); virtual ~ClapTrap (void); ClapTrap & operator= (ClapTrap const & src); diff --git a/CPP03/ex01/ClapTrap.cpp b/CPP03/ex01/ClapTrap.cpp index e07c0c5..b808dd4 100644 --- a/CPP03/ex01/ClapTrap.cpp +++ b/CPP03/ex01/ClapTrap.cpp @@ -21,8 +21,8 @@ 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) { +ClapTrap::ClapTrap (std::string name) + : _name(name), _health(10), _energy(10), _attack(0) { if (ClapTrap::amount >= 128) { delete ClapTrap::registery[amount % 128]; diff --git a/CPP03/ex01/ClapTrap.hpp b/CPP03/ex01/ClapTrap.hpp index 384b1ab..f8d803e 100644 --- a/CPP03/ex01/ClapTrap.hpp +++ b/CPP03/ex01/ClapTrap.hpp @@ -11,9 +11,6 @@ /* ************************************************************************** */ #pragma once -#define CT_HEALTH 10 -#define CT_ENERGY 10 -#define CT_DMG 0 #include #include @@ -26,8 +23,7 @@ class ClapTrap { 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 (std::string name); ClapTrap (ClapTrap const & src); virtual ~ClapTrap (void); ClapTrap & operator= (ClapTrap const & src); diff --git a/CPP03/ex01/ScavTrap.cpp b/CPP03/ex01/ScavTrap.cpp index bb54637..14dbd82 100644 --- a/CPP03/ex01/ScavTrap.cpp +++ b/CPP03/ex01/ScavTrap.cpp @@ -6,13 +6,16 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ -/* Updated: 2022/06/24 07:49:17 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:17:44 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){ +ScavTrap::ScavTrap (std::string name):ClapTrap(name){ + _health = 100; + _energy= 50; + _attack = 20; cout << "ClapTrap " << name << " evolved to ScavTrap" << endl; } diff --git a/CPP03/ex01/ScavTrap.hpp b/CPP03/ex01/ScavTrap.hpp index 66e1e9b..de29253 100644 --- a/CPP03/ex01/ScavTrap.hpp +++ b/CPP03/ex01/ScavTrap.hpp @@ -6,22 +6,18 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ -/* Updated: 2022/06/23 17:03:22 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:16:21 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 (std::string name); ~ScavTrap (void); void attack(const std::string& target); void guardGate(void); diff --git a/CPP03/ex01/claptrap b/CPP03/ex01/claptrap deleted file mode 100755 index 4857b0e..0000000 Binary files a/CPP03/ex01/claptrap and /dev/null differ diff --git a/CPP03/ex02/ClapTrap.cpp b/CPP03/ex02/ClapTrap.cpp index e07c0c5..8056078 100644 --- a/CPP03/ex02/ClapTrap.cpp +++ b/CPP03/ex02/ClapTrap.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */ -/* Updated: 2022/06/23 17:47:34 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:20:18 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,8 @@ 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) { +ClapTrap::ClapTrap (std::string name) + : _name(name), _health(10), _energy(10), _attack(0) { if (ClapTrap::amount >= 128) { delete ClapTrap::registery[amount % 128]; diff --git a/CPP03/ex02/ClapTrap.hpp b/CPP03/ex02/ClapTrap.hpp index a4271bc..2664539 100644 --- a/CPP03/ex02/ClapTrap.hpp +++ b/CPP03/ex02/ClapTrap.hpp @@ -6,14 +6,11 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */ -/* Updated: 2022/06/24 07:51:21 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:19:51 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#define CT_HEALTH 10 -#define CT_ENERGY 10 -#define CT_DMG 0 #include #include @@ -26,8 +23,7 @@ class ClapTrap { 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 (std::string name); ClapTrap (ClapTrap const & src); virtual ~ClapTrap (void); ClapTrap & operator= (ClapTrap const & src); diff --git a/CPP03/ex02/FragTrap.cpp b/CPP03/ex02/FragTrap.cpp index ceb544e..27b0202 100644 --- a/CPP03/ex02/FragTrap.cpp +++ b/CPP03/ex02/FragTrap.cpp @@ -6,13 +6,16 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ -/* Updated: 2022/06/24 07:46:44 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:23:22 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){ +FragTrap::FragTrap (std::string name):ClapTrap(name){ + _health = 100; + _energy = 100; + _attack = 30; cout << "ClapTrap " << name << " evolved to FragTrap" << endl; } diff --git a/CPP03/ex02/FragTrap.hpp b/CPP03/ex02/FragTrap.hpp index 904cf21..8f81c1c 100644 --- a/CPP03/ex02/FragTrap.hpp +++ b/CPP03/ex02/FragTrap.hpp @@ -6,22 +6,18 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ -/* Updated: 2022/06/24 07:33:33 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:23:55 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 (std::string name); ~FragTrap (void); void attack(const std::string& target); void highFivesGuys(void); diff --git a/CPP03/ex02/ScavTrap.cpp b/CPP03/ex02/ScavTrap.cpp index 71a46c5..df0e28a 100644 --- a/CPP03/ex02/ScavTrap.cpp +++ b/CPP03/ex02/ScavTrap.cpp @@ -6,13 +6,16 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ -/* Updated: 2022/06/24 07:47:54 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:22:09 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){ +ScavTrap::ScavTrap (std::string name):ClapTrap(name){ + _health = 100; + _energy = 50; + _attack = 20; cout << "ClapTrap " << name << " evolved to ScavTrap" << endl; } diff --git a/CPP03/ex02/ScavTrap.hpp b/CPP03/ex02/ScavTrap.hpp index 66e1e9b..ec8310d 100644 --- a/CPP03/ex02/ScavTrap.hpp +++ b/CPP03/ex02/ScavTrap.hpp @@ -6,22 +6,18 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ -/* Updated: 2022/06/23 17:03:22 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 02:24:27 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 (std::string name); ~ScavTrap (void); void attack(const std::string& target); void guardGate(void); diff --git a/CPP03/ex02/claptrap b/CPP03/ex02/claptrap deleted file mode 100755 index 4857b0e..0000000 Binary files a/CPP03/ex02/claptrap and /dev/null differ diff --git a/CPP03/ex03/ClapTrap.cpp b/CPP03/ex03/ClapTrap.cpp new file mode 100644 index 0000000..71d4ee5 --- /dev/null +++ b/CPP03/ex03/ClapTrap.cpp @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 10:53:50 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:26:32 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) + : _name(name), _health(10), _energy(10), _attack(0) { + 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); +} diff --git a/CPP03/ex03/ClapTrap.hpp b/CPP03/ex03/ClapTrap.hpp new file mode 100644 index 0000000..7f1c8af --- /dev/null +++ b/CPP03/ex03/ClapTrap.hpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:49:28 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +using std::cout; +using std::endl; + +class ClapTrap { + + public: + static ClapTrap *registery[128]; + static int amount; + + ClapTrap (void); + ClapTrap (std::string name); + 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; +}; diff --git a/CPP03/ex03/DiamondTrap.cpp b/CPP03/ex03/DiamondTrap.cpp new file mode 100644 index 0000000..d120025 --- /dev/null +++ b/CPP03/ex03/DiamondTrap.cpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* DiamondTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ +/* Updated: 2022/08/03 10:30:43 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "DiamondTrap.hpp" + +DiamondTrap::DiamondTrap (std::string name): ScavTrap(name), FragTrap(name){ + cout << ScavTrap::_energy << endl; + ClapTrap::_name = name + "_clap_name"; + _name = name; + _health = FragTrap::_health; + _energy = ScavTrap::_energy; + _attack = FragTrap::_attack; + cout << name << "is now a DiamondTrap" << endl; +} + +DiamondTrap::~DiamondTrap (void) { + cout << "DiamondTrap " << _name << " was destroyed" << endl; +} + +void DiamondTrap::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 DiamondTrap::whoAmI(void) { + cout << "Name: " << _name << " - Clap name: " << ClapTrap::_name << endl; +} diff --git a/CPP03/ex03/DiamondTrap.hpp b/CPP03/ex03/DiamondTrap.hpp new file mode 100644 index 0000000..b3886ac --- /dev/null +++ b/CPP03/ex03/DiamondTrap.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* DiamondTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:52:31 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include "ScavTrap.hpp" +#include "FragTrap.hpp" + +class DiamondTrap : public ScavTrap, public FragTrap { + std::string _name; + public: + DiamondTrap (std::string name); + ~DiamondTrap (void); + void attack(const std::string& target); + void whoAmI(void); +}; diff --git a/CPP03/ex03/FragTrap.cpp b/CPP03/ex03/FragTrap.cpp new file mode 100644 index 0000000..1bfa0b1 --- /dev/null +++ b/CPP03/ex03/FragTrap.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FragTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:29:38 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "FragTrap.hpp" + +FragTrap::FragTrap (std::string name):ClapTrap(name){ + _health = 100; + _energy = 100; + _attack = 30; + cout << "ClapTrap " << name << " evolved to FragTrap" << endl; +} + +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--; + } +} + +void FragTrap::highFivesGuys(void) { + for (int i = 0; i < 128; i++) + { + if (ClapTrap::registery[i]) + cout << _name << " high five " << ClapTrap::registery[i]->getName() << endl; + } +} diff --git a/CPP03/ex03/FragTrap.hpp b/CPP03/ex03/FragTrap.hpp new file mode 100644 index 0000000..a163147 --- /dev/null +++ b/CPP03/ex03/FragTrap.hpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FragTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:49:59 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include "ClapTrap.hpp" + +class FragTrap : virtual public ClapTrap { + + public: + FragTrap (std::string name); + ~FragTrap (void); + void attack(const std::string& target); + void highFivesGuys(void); +}; diff --git a/CPP03/ex03/Makefile b/CPP03/ex03/Makefile new file mode 100644 index 0000000..f4ff839 --- /dev/null +++ b/CPP03/ex03/Makefile @@ -0,0 +1,20 @@ +NAME = diamondtrap +SRCS = main.cpp ClapTrap.cpp ScavTrap.cpp FragTrap.cpp DiamondTrap.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 diff --git a/CPP03/ex03/ScavTrap.cpp b/CPP03/ex03/ScavTrap.cpp new file mode 100644 index 0000000..44fb426 --- /dev/null +++ b/CPP03/ex03/ScavTrap.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:27:49 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScavTrap.hpp" + +ScavTrap::ScavTrap (std::string name) +:ClapTrap(name){ + _health = 100; + _energy = 50; + _attack = 20; + cout << "ClapTrap " << name << " evolved to ScavTrap" << endl; +} + +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"; +} diff --git a/CPP03/ex03/ScavTrap.hpp b/CPP03/ex03/ScavTrap.hpp new file mode 100644 index 0000000..a4758d4 --- /dev/null +++ b/CPP03/ex03/ScavTrap.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:27:07 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include "ClapTrap.hpp" + +class ScavTrap : virtual public ClapTrap { + + public: + + ScavTrap (std::string name); + ~ScavTrap (void); + void attack(const std::string& target); + void guardGate(void); +}; diff --git a/CPP03/ex03/main.cpp b/CPP03/ex03/main.cpp new file mode 100644 index 0000000..7c827eb --- /dev/null +++ b/CPP03/ex03/main.cpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/23 10:53:16 by narnaud #+# #+# */ +/* Updated: 2022/07/19 02:47:30 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScavTrap.hpp" +#include "FragTrap.hpp" +#include "DiamondTrap.hpp" + +int main(void) +{ + ClapTrap a("A"); + ScavTrap b("B"); + FragTrap c("C"); + DiamondTrap d("D"); + + a.attack("B"); + b.attack("C"); + c.attack("B"); + c.highFivesGuys(); + d.attack("C"); + d.whoAmI(); +} diff --git a/CPP04/ex00/Animal.cpp b/CPP04/ex00/Animal.cpp index 71a3e72..d17f56c 100644 --- a/CPP04/ex00/Animal.cpp +++ b/CPP04/ex00/Animal.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */ -/* Updated: 2022/06/24 11:22:10 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 12:42:41 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,5 +39,5 @@ string Animal::getType(void) const { return (_type); } void Animal::makeSound(void) const { - cout << "..." << endl; + cout << "Strange noise" << endl; } diff --git a/CPP04/ex00/WrongAnimal.cpp b/CPP04/ex00/WrongAnimal.cpp index 2c07637..b5ae921 100644 --- a/CPP04/ex00/WrongAnimal.cpp +++ b/CPP04/ex00/WrongAnimal.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */ -/* Updated: 2022/06/24 11:22:03 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 12:43:12 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,5 +38,5 @@ string WrongAnimal::getType(void) const { return (_type); } void WrongAnimal::makeSound(void) const { - cout << "..." << endl; + cout << "Strange noise" << endl; } diff --git a/CPP04/ex00/main.cpp b/CPP04/ex00/main.cpp index 9882379..48ba373 100644 --- a/CPP04/ex00/main.cpp +++ b/CPP04/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 08:28:40 by narnaud #+# #+# */ -/* Updated: 2022/06/24 10:16:31 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 12:40:35 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,5 +28,9 @@ int main(void) meta->makeSound(); k->makeSound(); + delete meta; + delete i; + delete j; + delete k; return (0); } diff --git a/CPP04/ex01/Brain.cpp b/CPP04/ex01/Brain.cpp index 2d69c0b..5c84ea5 100644 --- a/CPP04/ex01/Brain.cpp +++ b/CPP04/ex01/Brain.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */ -/* Updated: 2022/06/24 11:57:44 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 14:16:32 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,15 @@ Brain::Brain (void) { } Brain::Brain (Brain const & src) { - (void)src; + for (int i = 0; i < 100; i++) + _idea[i] = src._idea[i]; cout << "Brain copy constructor called" << endl; } Brain & Brain::operator= (Brain const & src) { - (void)src; cout << "Brain assignment operator called" << endl; + for (int i = 0; i < 100; i++) + _idea[i] = src._idea[i]; return (*this); } diff --git a/CPP04/ex02/Brain.cpp b/CPP04/ex02/Brain.cpp index 2d69c0b..60e66b6 100644 --- a/CPP04/ex02/Brain.cpp +++ b/CPP04/ex02/Brain.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */ -/* Updated: 2022/06/24 11:57:44 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 14:22:38 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,14 @@ Brain::Brain (void) { } Brain::Brain (Brain const & src) { - (void)src; + for (int i = 0; i < 100; i++) + _ideas[i] = src._ideas[i]; cout << "Brain copy constructor called" << endl; } Brain & Brain::operator= (Brain const & src) { - (void)src; + for (int i = 0; i < 100; i++) + _ideas[i] = src._ideas[i]; cout << "Brain assignment operator called" << endl; return (*this); } diff --git a/CPP04/ex02/Brain.hpp b/CPP04/ex02/Brain.hpp index 4598a81..9108054 100644 --- a/CPP04/ex02/Brain.hpp +++ b/CPP04/ex02/Brain.hpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */ -/* Updated: 2022/06/24 11:56:58 by narnaud ### ########.fr */ +/* Updated: 2022/07/19 14:23:14 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ using std::endl; using std::string; class Brain{ - string ideas[100]; + string _ideas[100]; public: Brain(void); Brain(Brain const & src); diff --git a/CPP05/ex03/Bureaucrat.o b/CPP05/ex03/Bureaucrat.o deleted file mode 100644 index 7af8727..0000000 Binary files a/CPP05/ex03/Bureaucrat.o and /dev/null differ diff --git a/CPP05/ex03/Form.o b/CPP05/ex03/Form.o deleted file mode 100644 index dafe23b..0000000 Binary files a/CPP05/ex03/Form.o and /dev/null differ diff --git a/CPP05/ex03/Intern.o b/CPP05/ex03/Intern.o deleted file mode 100644 index 2322f5f..0000000 Binary files a/CPP05/ex03/Intern.o and /dev/null differ diff --git a/CPP05/ex03/PresidentialPardonForm.o b/CPP05/ex03/PresidentialPardonForm.o deleted file mode 100644 index c5bdb92..0000000 Binary files a/CPP05/ex03/PresidentialPardonForm.o and /dev/null differ diff --git a/CPP05/ex03/RobotomyRequestForm.o b/CPP05/ex03/RobotomyRequestForm.o deleted file mode 100644 index 810f3fc..0000000 Binary files a/CPP05/ex03/RobotomyRequestForm.o and /dev/null differ diff --git a/CPP05/ex03/ShrubberyCreationForm.o b/CPP05/ex03/ShrubberyCreationForm.o deleted file mode 100644 index eb0e4b7..0000000 Binary files a/CPP05/ex03/ShrubberyCreationForm.o and /dev/null differ diff --git a/CPP05/ex03/bureau b/CPP05/ex03/bureau deleted file mode 100755 index d91abf4..0000000 Binary files a/CPP05/ex03/bureau and /dev/null differ diff --git a/CPP05/ex03/main.o b/CPP05/ex03/main.o deleted file mode 100644 index a287cb2..0000000 Binary files a/CPP05/ex03/main.o and /dev/null differ diff --git a/CPP06/ex00/Converter.cpp b/CPP06/ex00/Converter.cpp index 3ce027d..7656edf 100644 --- a/CPP06/ex00/Converter.cpp +++ b/CPP06/ex00/Converter.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/30 08:45:12 by narnaud #+# #+# */ -/* Updated: 2022/07/12 08:42:27 by narnaud ### ########.fr */ +/* Updated: 2022/08/18 08:54:04 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,17 +30,22 @@ Converter::Converter (string str){ _c = static_cast(_d); if (_d < 0 || _d > 255) cout << "Invalid" << endl; - else if (std::isprint(_c)) + else if (isnan(_d)) + cout << "Impossible" << endl; + else if (!std::isprint(_c)) cout << "Not Printable" << endl; else - cout << _c << endl; + cout << "'" << _c << "'" << endl; } - cout << "Double: " << _d << endl; cout << "Integer: "; - _i = static_cast(_d); + _i = static_cast(_d); if (_d < std::numeric_limits::min() || _d > std::numeric_limits::max()) cout << "Off limits -> "; - cout << _i << endl; + if (isnan(_d)) + cout << "Impossible" << endl; + else + cout << _i << endl; + cout << "Double: " << _d << endl; _f = static_cast(_d); cout << "Float: " << _f << "f"<< endl; } diff --git a/CPP06/ex00/Converter.hpp b/CPP06/ex00/Converter.hpp index b13ad66..98829ff 100644 --- a/CPP06/ex00/Converter.hpp +++ b/CPP06/ex00/Converter.hpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/30 08:45:12 by narnaud #+# #+# */ -/* Updated: 2022/07/12 08:46:08 by narnaud ### ########.fr */ +/* Updated: 2022/08/18 08:45:46 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,8 @@ #include #include #include +#include + using std::cout; using std::endl; using std::string; @@ -29,7 +31,7 @@ class Converter{ int _i; float _f; double _d; - int _types[4]; + //int _types[4]; public: Converter(void); Converter(string str); diff --git a/CPP06/ex00/main.cpp b/CPP06/ex00/main.cpp index 91b5a6f..5a5296d 100644 --- a/CPP06/ex00/main.cpp +++ b/CPP06/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/28 08:44:43 by narnaud #+# #+# */ -/* Updated: 2022/07/12 08:09:02 by narnaud ### ########.fr */ +/* Updated: 2022/07/21 12:16:42 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/CPP07/ex00/Makefile b/CPP07/ex00/Makefile new file mode 100644 index 0000000..be24ac4 --- /dev/null +++ b/CPP07/ex00/Makefile @@ -0,0 +1,20 @@ +NAME = templates +SRCS = main.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 diff --git a/CPP07/ex00/main.cpp b/CPP07/ex00/main.cpp new file mode 100644 index 0000000..31790cb --- /dev/null +++ b/CPP07/ex00/main.cpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:05:29 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:13:45 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "main.hpp" +#include + +int main( void ) { + int a = 2; + int b = 3; + std::cout << "a = " << a << ", b = " << b << std::endl; + ::swap( a, b ); + std::cout << "a = " << a << ", b = " << b << std::endl; + std::cout << "min( a, b ) = " << ::min( a, b ) << std::endl; + std::cout << "max( a, b ) = " << ::max( a, b ) << std::endl; + std::string c = "chaine1"; + std::string d = "chaine2"; + ::swap(c, d); + std::cout << "c = " << c << ", d = " << d << std::endl; + std::cout << "min( c, d ) = " << ::min( c, d ) << std::endl; + std::cout << "max( c, d ) = " << ::max( c, d ) << std::endl; + return 0; +} + diff --git a/CPP07/ex00/main.hpp b/CPP07/ex00/main.hpp new file mode 100644 index 0000000..659bb67 --- /dev/null +++ b/CPP07/ex00/main.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:05:52 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:13:08 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +template +void swap (T &t1, T &t2) { + T tmp; + tmp = t1; + t1 = t2; + t2 = tmp; +} + +template +T min (T &t1, T &t2) { + return (t1 < t2 ? t1 : t2); +} + +template +T max (T &t1, T &t2) { + return (t1 > t2 ? t1 : t2); +} diff --git a/CPP07/ex01/Makefile b/CPP07/ex01/Makefile new file mode 100644 index 0000000..cec2b40 --- /dev/null +++ b/CPP07/ex01/Makefile @@ -0,0 +1,21 @@ +NAME = iter +SRCS = main.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 + diff --git a/CPP07/ex01/iter.hpp b/CPP07/ex01/iter.hpp new file mode 100644 index 0000000..43d1d4f --- /dev/null +++ b/CPP07/ex01/iter.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:14:42 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:33:06 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +template +void iter(T *arr, int length, void (*fct)(T)) { + for (int i = 0; i < length; i++) { + fct(arr[i]); + } +} diff --git a/CPP07/ex01/main.cpp b/CPP07/ex01/main.cpp new file mode 100644 index 0000000..e4e1561 --- /dev/null +++ b/CPP07/ex01/main.cpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:14:45 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:43:03 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "iter.hpp" + +template +void put_t(T t) { + std::cout << t << std::endl; +} + +template +void put_square(T t) { + put_t(t * t); +} + +int main(void) { + int nbrs[5] = {1, 2, 3 , 4, 5}; + char words[5][4] = {"the", "one", "two", "get", "any"}; + + iter(nbrs, 5, put_t); + iter(nbrs, 3, put_square); + iter(words, 5, put_t); +} diff --git a/CPP07/ex02/Array.hpp b/CPP07/ex02/Array.hpp new file mode 100644 index 0000000..188d4c6 --- /dev/null +++ b/CPP07/ex02/Array.hpp @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Array.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:50:55 by narnaud #+# #+# */ +/* Updated: 2022/09/05 11:23:19 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +template class Array { + T *tab; + int nbr; + public: + Array() { + tab = new T[0]; + }; + + Array(int n) { + tab = new T[nbr = n]; + }; + + Array(const Array &a) { + tab = new T[nbr = a.size()]; + for (int i = 0; i < nbr; i++) { + tab[i] = a[i]; + } + }; + + ~Array() { + delete [] tab; + }; + + Array &operator= (const Array &a) { + delete [] tab; + nbr = a.size(); + tab = new T[nbr]; + for (int i = 0; i < nbr; i++) { + tab[i] = a[i]; + } + return (*this); + }; + + T &operator[] (int i) { + if (i >= nbr) throw (std::runtime_error("error: Array index out of bound.")); + return (tab[i]); + }; + + T operator[] (int i) const { + if (i >= nbr) throw (std::runtime_error("error: Array index out of bound.")); + return (tab[i]); + }; + + int size() const{ + return (nbr); + }; +}; diff --git a/CPP07/ex02/Makefile b/CPP07/ex02/Makefile new file mode 100644 index 0000000..6c35345 --- /dev/null +++ b/CPP07/ex02/Makefile @@ -0,0 +1,20 @@ +NAME = array +SRCS = main.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 diff --git a/CPP07/ex02/main.cpp b/CPP07/ex02/main.cpp new file mode 100644 index 0000000..1dbb026 --- /dev/null +++ b/CPP07/ex02/main.cpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:49:45 by narnaud #+# #+# */ +/* Updated: 2022/09/05 11:00:32 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Array.hpp" + +int main(void) { + Array a; + Array b(10); + Array c(b); + + b[3] = 'b'; + std::cout << b[3] << std::endl; + std::cout << b.size() << std::endl; + std::cout << c[3] << std::endl; + c = b; + std::cout << c[3] << std::endl; + + try { + std::cout << b[12] << std::endl; + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } +} diff --git a/CPP08/ex00/Makefile b/CPP08/ex00/Makefile new file mode 100644 index 0000000..30d1ec0 --- /dev/null +++ b/CPP08/ex00/Makefile @@ -0,0 +1,21 @@ +NAME = easy_find +SRCS = +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 +