1
0

🏗️」 wip(Ex03): Now with template !

This commit is contained in:
2024-12-11 14:03:14 +01:00
parent 62e0f56f94
commit 18ae71b63f
10 changed files with 123 additions and 105 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/09 11:24:41 by adjoly #+# #+# */
/* Updated: 2024/12/10 12:28:09 by adjoly ### ########.fr */
/* Updated: 2024/12/11 11:03:42 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@ AMateria::AMateria(void) : _type("") {
}
AMateria::AMateria(std::string const &type) {
log("", "AMateria", "", "constructor called");
log("", "AMateria", "", "copy constructor called");
}
AMateria::~AMateria(void) {

View File

@ -6,17 +6,18 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 14:59:12 by adjoly #+# #+# */
/* Updated: 2024/12/10 15:15:46 by adjoly ### ########.fr */
/* Updated: 2024/12/10 15:43:54 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "AMateria.hpp"
#include "ICharacter.hpp"
#define ENV_SIZE 4
class Character {
class Character : public ICharacter{
private:
AMateria _materias[ENV_SIZE];
public:

View File

@ -1,19 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cube.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/06 16:47:20 by adjoly #+# #+# */
/* Updated: 2024/12/06 16:51:31 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "AMateria.hpp"
class Cube : public AMateria {
};

View File

@ -6,40 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 12:34:54 by adjoly #+# #+# */
/* Updated: 2024/12/10 13:47:54 by adjoly ### ########.fr */
/* Updated: 2024/12/11 12:08:03 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Cure.hpp"
#include "AMateria.hpp"
#include "ICharacter.hpp"
#include "Log.hpp"
#include <iostream>
Cure::Cure(void) : AMateria("cure") {
log("", "Cure", "", "default constructor called");
}
Cure::Cure(const Cure &cpy) {
log("", "Cure", "", "copy constructor called");
*this = cpy;
}
Cure::~Cure(void) {
log("", "Cure", "", "destructor called");
}
Cure &Cure::operator=(const Cure &cpy) {
log("", "Cure", "", "copy assignement constructor called");
if (this != &cpy) {
_type = cpy._type;
}
return (*this);
}
Cure *Cure::clone(void) const {
return (new Cure);
}
const std::string Cure::_typeName(void) { return ("cure"); }
void Cure::use(ICharacter &character) {
std::cout << "* heals " << character.getName() << "s wounds *";

View File

@ -6,22 +6,18 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 12:35:41 by adjoly #+# #+# */
/* Updated: 2024/12/10 12:36:22 by adjoly ### ########.fr */
/* Updated: 2024/12/11 12:07:54 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "AMateria.hpp"
#include "ICharacter.hpp"
#include "MateriaTemplate.hpp"
class Cure : public AMateria {
class Cure : public MateriaTemplate<Cure>{
public:
Cure(void);
~Cure(void);
Cure(const Cure &);
Cure &operator=(const Cure &);
static const std::string _typeName(void);
Cure *clone(void) const;
void use(ICharacter &);
void use(ICharacter &);
};

View File

@ -6,40 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/07 12:13:03 by adjoly #+# #+# */
/* Updated: 2024/12/10 12:25:11 by adjoly ### ########.fr */
/* Updated: 2024/12/11 12:08:18 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Ice.hpp"
#include "AMateria.hpp"
#include "ICharacter.hpp"
#include "Log.hpp"
#include <iostream>
Ice::Ice(void) : AMateria("ice") {
log("", "Ice", "", "default constructor called");
}
Ice::Ice(const Ice &cpy) {
log("", "Ice", "", "copy constructor called");
*this = cpy;
}
Ice::~Ice(void) {
log("", "Ice", "", "destructor called");
}
Ice &Ice::operator=(const Ice &cpy) {
log("", "Ice", "", "copy assignement constructor called");
if (this != &cpy) {
_type = cpy._type;
}
return (*this);
}
Ice *Ice::clone(void) const {
return (new Ice);
}
const std::string Ice::_typeName(void) { return ("ice"); }
void Ice::use(ICharacter &character) {
std::cout << "* shoots an ice bolt at " << character.getName() << " *";

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/06 16:51:34 by adjoly #+# #+# */
/* Updated: 2024/12/07 12:29:29 by adjoly ### ########.fr */
/* Updated: 2024/12/11 12:07:40 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,14 +14,11 @@
#include "AMateria.hpp"
#include "ICharacter.hpp"
#include "MateriaTemplate.hpp"
class Ice : public AMateria {
class Ice : public MateriaTemplate<Ice> {
public:
Ice(void);
~Ice(void);
Ice(const Ice &);
Ice &operator=(const Ice &);
static const std::string _typeName(void);
Ice *clone(void) const;
void use(ICharacter &);
void use(ICharacter &);
};

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MateriaSource.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/11 12:17:06 by adjoly #+# #+# */
/* Updated: 2024/12/11 12:31:04 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "MateriaSource.hpp"
#include "Log.hpp"
MateriaSource::MateriaSource(void) {
log("", "MateriaSource", "", "default constructor called");
}
MateriaSource::~MateriaSource(void) {
log("", "MateriaSource", "", "destructor called");
}
MateriaSource::MateriaSource(const MateriaSource &cpy) {
*this = cpy;
log("", "MateriaSource", "", "copy constructor called");
}
MateriaSource &MateriaSource::operator=(const MateriaSource &cpy) {
log("", "MateriaSource", "", "copy assignement constructor called");
for (int i = 0; i < 4; i++) {
this->_materias[i] = cpy._materias[i];
}
return (*this);
}

View File

@ -6,23 +6,24 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 13:51:37 by adjoly #+# #+# */
/* Updated: 2024/12/10 14:50:48 by adjoly ### ########.fr */
/* Updated: 2024/12/11 12:18:02 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "AMateria.hpp"
#include "IMateriaSource.hpp"
class MaterianSource {
class MateriaSource : public IMateriaSource {
private:
AMateria _materias[4];
AMateria *_materias[4];
public:
MaterianSource(void);
~MaterianSource(void);
MaterianSource(const MaterianSource &);
MaterianSource &operator=(const MaterianSource &);
MateriaSource(void);
~MateriaSource(void);
MateriaSource(const MateriaSource &);
MateriaSource &operator=(const MateriaSource &);
void leanrMateria(AMateria *);
AMateria createMateria(std::string const &);
void learnMateria(AMateria *);
AMateria *createMateria(std::string const &);
};

58
ex03/MateriaTemplate.hpp Normal file
View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MateriaTemplate.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/11 10:59:31 by adjoly #+# #+# */
/* Updated: 2024/12/11 12:06:51 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "AMateria.hpp"
#include "Log.hpp"
template <typename T>
class MateriaTemplate : public AMateria {
public:
MateriaTemplate(void);
~MateriaTemplate(void);
MateriaTemplate(T const &);
T &operator=(const T &);
AMateria *clone(void) const;
};
template <typename T>
MateriaTemplate<T>::MateriaTemplate(void) :
AMateria(T::_typeName()) {
log("", T::_typeName(), "", "default constructor called");
}
template <typename T>
MateriaTemplate<T>::~MateriaTemplate(void) {
log("", T::_typeName(), "", "destructor called");
}
template <typename T>
MateriaTemplate<T>::MateriaTemplate(T const &cpy) {
*this = cpy;
log("", T::_typeName(), "", "copy constructor called");
}
template <typename T>
T &MateriaTemplate<T>::operator=(const T &cpy) {
log("", T::_typeName(), "", "copy assignement constructor called");
if (this != &cpy) {
this->_type = cpy._type;
}
return (*this);
}
template <typename T>
AMateria *MateriaTemplate<T>::clone(void) const {
return (new T);
}