cpp03 correct

This commit is contained in:
Alexandre POMMIER 2022-08-03 15:30:46 +02:00
parent ed5770a02c
commit 66a17e5611
12 changed files with 132 additions and 99 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:51 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:51 by apommier #+# #+# */
/* Updated: 2022/07/14 01:17:00 by apommier ### ########.fr */ /* Updated: 2022/08/03 14:27:09 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,19 @@ ScavTrap::~ScavTrap()
std::cout << "ScavTrap " << this->_name << " Destructor called" << std::endl; std::cout << "ScavTrap " << this->_name << " Destructor called" << std::endl;
} }
void ScavTrap::attack(const std::string& target)
{
if (this->_hitPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not attack because he died" << std::endl;
else if (this->_energyPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not attack because he don't have energy" << std::endl;
else
{
std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
this->_energyPoints--;
}
}
ScavTrap &ScavTrap::operator=(const ScavTrap& rhs) ScavTrap &ScavTrap::operator=(const ScavTrap& rhs)
{ {
std::cout << "ScavTrap " << this->_name << " Assignation operator called" << std::endl; std::cout << "ScavTrap " << this->_name << " Assignation operator called" << std::endl;
@ -46,5 +59,8 @@ ScavTrap &ScavTrap::operator=(const ScavTrap& rhs)
void ScavTrap::guardGate() void ScavTrap::guardGate()
{ {
if (this->_hitPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not guard because he died" << std::endl;
else
std::cout << "ScavTrap " << this->_name << " came into Gatekeeper mode\n"; std::cout << "ScavTrap " << this->_name << " came into Gatekeeper mode\n";
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:53 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:53 by apommier #+# #+# */
/* Updated: 2022/07/13 10:18:35 by apommier ### ########.fr */ /* Updated: 2022/08/03 14:23:58 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,8 +23,10 @@ class ScavTrap : public ClapTrap
ScavTrap(std::string name); ScavTrap(std::string name);
ScavTrap(const ScavTrap& copy); ScavTrap(const ScavTrap& copy);
~ScavTrap(); ~ScavTrap();
ScavTrap &operator=(const ScavTrap& rhs); ScavTrap &operator=(const ScavTrap& rhs);
void attack(const std::string& target);
void guardGate(); void guardGate();
private: private:

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 01:24:31 by apommier #+# #+# */ /* Created: 2022/07/14 01:24:31 by apommier #+# #+# */
/* Updated: 2022/07/14 01:55:06 by apommier ### ########.fr */ /* Updated: 2022/08/03 15:27:21 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,19 @@ FragTrap::~FragTrap()
std::cout << "FragTrap " << this->_name << " Destructor called" << std::endl; std::cout << "FragTrap " << this->_name << " Destructor called" << std::endl;
} }
void FragTrap::attack(const std::string& target)
{
if (this->_hitPoints <= 0)
std::cout << "FragTrap " << this->_name << " could not attack because he died" << std::endl;
else if (this->_energyPoints <= 0)
std::cout << "FragTrap " << this->_name << " could not attack because he don't have energy" << std::endl;
else
{
std::cout << "FragTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
this->_energyPoints--;
}
}
FragTrap &FragTrap::operator=(const FragTrap& rhs) FragTrap &FragTrap::operator=(const FragTrap& rhs)
{ {
std::cout << "FragTrap " << this->_name << " Assignation operator called" << std::endl; std::cout << "FragTrap " << this->_name << " Assignation operator called" << std::endl;
@ -46,5 +59,8 @@ FragTrap &FragTrap::operator=(const FragTrap& rhs)
void FragTrap::highFivesGuys(void) void FragTrap::highFivesGuys(void)
{ {
if (this->_hitPoints <= 0)
std::cout << "FragTrap " << this->_name << " could not ask you for a highfive because he died" << std::endl;
else
std::cout << "FragTrap " << this->_name << " ask you for a highfive\n"; std::cout << "FragTrap " << this->_name << " ask you for a highfive\n";
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 01:24:34 by apommier #+# #+# */ /* Created: 2022/07/14 01:24:34 by apommier #+# #+# */
/* Updated: 2022/07/14 01:54:11 by apommier ### ########.fr */ /* Updated: 2022/08/03 14:23:47 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,8 +23,10 @@ class FragTrap : public ClapTrap
FragTrap(std::string name); FragTrap(std::string name);
FragTrap(const FragTrap& copy); FragTrap(const FragTrap& copy);
~FragTrap(); ~FragTrap();
FragTrap &operator=(const FragTrap& rhs); FragTrap &operator=(const FragTrap& rhs);
void attack(const std::string& target);
void highFivesGuys(void); void highFivesGuys(void);
private: private:

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:51 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:51 by apommier #+# #+# */
/* Updated: 2022/07/14 01:23:29 by apommier ### ########.fr */ /* Updated: 2022/08/03 14:27:02 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,6 +34,19 @@ ScavTrap::~ScavTrap()
std::cout << "ScavTrap " << this->_name << " Destructor called" << std::endl; std::cout << "ScavTrap " << this->_name << " Destructor called" << std::endl;
} }
void ScavTrap::attack(const std::string& target)
{
if (this->_hitPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not attack because he died" << std::endl;
else if (this->_energyPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not attack because he don't have energy" << std::endl;
else
{
std::cout << "ScavTrap " << this->_name << " attacks " << target << ", causing " << this->_attackDamage << " points of damage!" << std::endl;
this->_energyPoints--;
}
}
ScavTrap &ScavTrap::operator=(const ScavTrap& rhs) ScavTrap &ScavTrap::operator=(const ScavTrap& rhs)
{ {
std::cout << "ScavTrap " << this->_name << " Assignation operator called" << std::endl; std::cout << "ScavTrap " << this->_name << " Assignation operator called" << std::endl;
@ -46,5 +59,8 @@ ScavTrap &ScavTrap::operator=(const ScavTrap& rhs)
void ScavTrap::guardGate() void ScavTrap::guardGate()
{ {
if (this->_hitPoints <= 0)
std::cout << "ScavTrap " << this->_name << " could not guard because he died" << std::endl;
else
std::cout << "ScavTrap " << this->_name << " came into Gatekeeper mode\n"; std::cout << "ScavTrap " << this->_name << " came into Gatekeeper mode\n";
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 08:03:53 by apommier #+# #+# */ /* Created: 2022/07/13 08:03:53 by apommier #+# #+# */
/* Updated: 2022/07/13 10:18:35 by apommier ### ########.fr */ /* Updated: 2022/08/03 14:23:43 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,8 +23,10 @@ class ScavTrap : public ClapTrap
ScavTrap(std::string name); ScavTrap(std::string name);
ScavTrap(const ScavTrap& copy); ScavTrap(const ScavTrap& copy);
~ScavTrap(); ~ScavTrap();
ScavTrap &operator=(const ScavTrap& rhs); ScavTrap &operator=(const ScavTrap& rhs);
void attack(const std::string& target);
void guardGate(); void guardGate();
private: private:

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 05:22:55 by apommier #+# #+# */ /* Created: 2022/07/13 05:22:55 by apommier #+# #+# */
/* Updated: 2022/08/01 22:12:59 by apommier ### ########.fr */ /* Updated: 2022/08/03 14:21:28 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,6 +50,7 @@ int main()
std::cout << std::endl << "ScavTrap turn\n"; std::cout << std::endl << "ScavTrap turn\n";
ScavTrap ScavJohn("CL4P-TP"); ScavTrap ScavJohn("CL4P-TP");
std::cout << std::endl;
ScavJohn.guardGate(); ScavJohn.guardGate();
ScavJohn.attack("Jean"); ScavJohn.attack("Jean");
@ -62,52 +63,17 @@ int main()
std::cout << std::endl << "FragTrap turn\n"; std::cout << std::endl << "FragTrap turn\n";
FragTrap FragJohn("Assassin"); FragTrap FragJohn("Assassin");
std::cout << std::endl;
FragJohn.highFivesGuys(); FragJohn.highFivesGuys();
FragJohn.attack("Jean"); FragJohn.attack("Jean");
FragJohn.takeDamage(5); FragJohn.takeDamage(5);
FragJohn.takeDamage(4); FragJohn.takeDamage(4);
FragJohn.takeDamage(1); FragJohn.takeDamage(1);
FragJohn.takeDamage(1); FragJohn.takeDamage(300);
FragJohn.attack("Jean"); FragJohn.attack("Jean");
std::cout <<"ScavTrap have " << FragJohn.getEnergyPoints() << " energy point(s)\n\n"; std::cout <<"ScavTrap have " << FragJohn.getEnergyPoints() << " energy point(s)\n\n";
std::cout << std::endl; std::cout << std::endl;
return (0); return (0);
} }
// int main()
// {
// ClapTrap John("John");
// John.attack("Jean");
// John.takeDamage(5);
// John.takeDamage(4);
// John.takeDamage(1);
// John.takeDamage(1);
// John.attack("Jean");
// std::cout << std::endl;
// ScavTrap ScavJohn("CL4P-TP");
// ScavJohn.guardGate();
// ScavJohn.attack("Jean");
// ScavJohn.takeDamage(5);
// ScavJohn.takeDamage(4);
// ScavJohn.takeDamage(1);
// ScavJohn.takeDamage(1);
// ScavJohn.attack("Jean");
// std::cout << std::endl;
// FragTrap FragJohn("Assassin");
// FragJohn.highFivesGuys();
// FragJohn.attack("Jean");
// FragJohn.takeDamage(5);
// FragJohn.takeDamage(4);
// FragJohn.takeDamage(1);
// FragJohn.takeDamage(1);
// FragJohn.attack("Jean");
// return (0);
// }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:04:10 by apommier #+# #+# */ /* Created: 2022/07/14 02:04:10 by apommier #+# #+# */
/* Updated: 2022/07/17 11:29:39 by apommier ### ########.fr */ /* Updated: 2022/08/03 13:26:10 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,8 +24,8 @@ int main()
std::cout << j->getType() << " " << std::endl; std::cout << j->getType() << " " << std::endl;
std::cout << i->getType() << " " << std::endl; std::cout << i->getType() << " " << std::endl;
i->makeSound(); //will output the cat sound! i->makeSound(); //will output the cat sound!
j->makeSound(); j->makeSound(); //will output the dog sound!
meta->makeSound(); meta->makeSound();//will output the animal sound!
delete meta; delete meta;
delete j; delete j;
@ -34,13 +34,16 @@ int main()
std::cout << std::endl; std::cout << std::endl;
const WrongAnimal* meta2 = new WrongAnimal(); const WrongAnimal* meta2 = new WrongAnimal();
const WrongAnimal* i2 = new WrongCat(); const WrongAnimal* falseWrongCat = new WrongCat();
std::cout << i2->getType() << " " << std::endl; const WrongCat* realWrongCat = new WrongCat();
i2->makeSound(); //will output the WrongAnimal sound! std::cout << falseWrongCat->getType() << " " << std::endl;
falseWrongCat->makeSound(); //will output the WrongAnimal sound!
meta2->makeSound(); //same meta2->makeSound(); //same
realWrongCat->makeSound();//will output the WrongCat sound!
delete realWrongCat;
delete meta2; delete meta2;
delete i2; delete falseWrongCat;
return 0; return 0;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:05:22 by apommier #+# #+# */ /* Created: 2022/07/14 02:05:22 by apommier #+# #+# */
/* Updated: 2022/07/17 18:36:09 by apommier ### ########.fr */ /* Updated: 2022/08/03 13:21:28 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ Animal::Animal()
Animal::Animal(const Animal& copy) Animal::Animal(const Animal& copy)
{ {
std::cout << "Animal Copy constructor called" << std::endl; std::cout << "Animal Copy constructor called" << std::endl;
this->type = copy.getType(); *this = copy;
} }
Animal::~Animal() Animal::~Animal()

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:11:37 by apommier #+# #+# */ /* Created: 2022/07/14 02:11:37 by apommier #+# #+# */
/* Updated: 2022/07/17 19:03:24 by apommier ### ########.fr */ /* Updated: 2022/08/03 13:20:30 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,8 +22,7 @@ Cat::Cat() : Animal()
Cat::Cat(const Cat& copy) : Animal(copy) Cat::Cat(const Cat& copy) : Animal(copy)
{ {
std::cout << "Cat Copy constructor called" << std::endl; std::cout << "Cat Copy constructor called" << std::endl;
this->type = copy.getType(); *this = copy;
//this->_Brain = copy.getType();
} }
Cat::~Cat() Cat::~Cat()

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:11:41 by apommier #+# #+# */ /* Created: 2022/07/14 02:11:41 by apommier #+# #+# */
/* Updated: 2022/07/17 19:03:52 by apommier ### ########.fr */ /* Updated: 2022/08/03 13:20:17 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ Dog::Dog() : Animal()
Dog::Dog(const Dog& copy) : Animal(copy) Dog::Dog(const Dog& copy) : Animal(copy)
{ {
std::cout << "Dog Copy constructor called" << std::endl; std::cout << "Dog Copy constructor called" << std::endl;
this->type = copy.getType(); *this = copy;
} }
Dog::~Dog() Dog::~Dog()

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 02:04:10 by apommier #+# #+# */ /* Created: 2022/07/14 02:04:10 by apommier #+# #+# */
/* Updated: 2022/07/18 10:28:22 by apommier ### ########.fr */ /* Updated: 2022/08/03 13:44:10 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,64 +16,75 @@
#include "WrongAnimal.hpp" #include "WrongAnimal.hpp"
#include "WrongCat.hpp" #include "WrongCat.hpp"
/*int main() void copyTest()
{
std::cout << std::endl;
Dog first;
Dog cpy = first;
std::cout << std::endl;
}
int main()
{ {
Animal *Animals[10]; Animal *Animals[10];
Brain *OneBrain; Brain *oneBrain;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
if (i < 5) if (i < 5)
{ {
Animals[i] = new Cat; Animals[i] = new Cat;
Animals[i]->getBrain()->setgetIdeas(0) "Sleep"); oneBrain = Animals[i]->getBrain();
Animals[i]->getBrain()->setgetIdeas(1) "Hate"); oneBrain->setIdeas(0, "Sleep");
Animals[i]->getBrain()->setgetIdeas(2) "Sleep"); oneBrain->setIdeas(1, "Hate");
oneBrain->setIdeas(2, "Sleep");
} }
else else
{ {
Animals[i] = new Dog; Animals[i] = new Dog;
Animals[i]->getBrain()->setgetIdeas(0) "Sleep"); oneBrain = Animals[i]->getBrain();
Animals[i]->getBrain()->setgetIdeas(1) "Play"); oneBrain->setIdeas(0, "Sleep");
Animals[i]->getBrain()->setgetIdeas(2) "Ate"); oneBrain->setIdeas(1, "Play");
oneBrain->setIdeas(2, "Ate");
} }
std::cout << Animals[i]->getType() << std::endl; std::cout << Animals[i]->getType() << std::endl;
} }
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
std::cout << Animals[i]->getBrain() << std::endl; std::cout << i + 1 << "- "<< Animals[i]->getBrain() << std::endl;
} }
//copyTest();
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
delete Animals[i]; delete Animals[i];
}*/
int main( void )
{
Animal *animals[10];
Brain *brain;
for (int i = 0; i < 10; i++)
{
if (i < 5)
animals[i] = new Dog();
else
animals[i] = new Cat();
std::cout << animals[i]->getType() << std::endl;
} }
brain = animals[7]->getBrain(); // int main( void )
brain->setIdeas(0, "I'm hungry"); // {
brain->setIdeas(1, "That's a strange idea I'm having"); // Animal *animals[10];
brain->setIdeas(2, "Ball!!!!!"); // Brain *brain;
brain->setIdeas(3, "Squirrel!!!!!");
std::cout << std::endl << animals[7]->getBrain()->getIdeas()[0] << std::endl;
std::cout << animals[7]->getBrain()->getIdeas()[2] << std::endl;
*(animals[5]) = *(animals[7]); // for (int i = 0; i < 10; i++)
std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl; // {
std::cout << "Test of copy\n"; // if (i < 5)
animals[7]->getBrain()->setIdeas(2, "Squirrel!!!!!"); // animals[i] = new Cat();
std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl << std::endl; // else
for (int i = 0; i < 10; i++) // animals[i] = new Dog();
delete animals[i]; // std::cout << animals[i]->getType() << std::endl;
} // }
// brain = animals[7]->getBrain();
// brain->setIdeas(0, "I'm hungry");
// brain->setIdeas(1, "That's a strange idea I'm having");
// brain->setIdeas(2, "Ball!!!!!");
// brain->setIdeas(3, "Squirrel!!!!!");
// std::cout << std::endl << animals[7]->getBrain()->getIdeas()[0] << std::endl;
// std::cout << animals[7]->getBrain()->getIdeas()[2] << std::endl;
// *(animals[5]) = *(animals[7]);
// std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl;
// std::cout << "Test of copy\n";
// animals[7]->getBrain()->setIdeas(2, "Squirrel!!!!!");
// std::cout << animals[5]->getBrain()->getIdeas()[2] << std::endl << std::endl;
// for (int i = 0; i < 10; i++)
// delete animals[i];
// }