ex01 finished
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/10 11:22:21 by aortigos #+# #+# */
|
||||
/* Updated: 2025/11/10 12:45:21 by aortigos ### ########.fr */
|
||||
/* Updated: 2025/11/10 20:43:06 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
Brain::Brain()
|
||||
{
|
||||
std::cout << "Brain has been created" << std::endl;
|
||||
int i = 0;
|
||||
while (i < 100)
|
||||
{
|
||||
ideas[i++] = "Empty idea";
|
||||
}
|
||||
}
|
||||
|
||||
Brain::~Brain()
|
||||
@@ -25,6 +30,7 @@ Brain::~Brain()
|
||||
Brain::Brain(const Brain &other)
|
||||
{
|
||||
std::cout << "Brain copied" << std::endl;
|
||||
*this = other;
|
||||
}
|
||||
|
||||
Brain& Brain::operator=(const Brain &other)
|
||||
@@ -32,7 +38,25 @@ Brain& Brain::operator=(const Brain &other)
|
||||
std::cout << "Brain copy assignment called" << std::endl;
|
||||
if (this != &other)
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
while (i < 100)
|
||||
{
|
||||
this->ideas[i] = other.ideas[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
void Brain::setIdea(int i, const std::string &idea)
|
||||
{
|
||||
if (i >= 0 && i < 100)
|
||||
ideas[i] = idea;
|
||||
}
|
||||
|
||||
std::string Brain::getIdea(int i) const
|
||||
{
|
||||
if (i >= 0 && i < 100)
|
||||
return (this->ideas[i]);
|
||||
return ("Invalid idea");
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/10 11:22:17 by aortigos #+# #+# */
|
||||
/* Updated: 2025/11/10 12:43:08 by aortigos ### ########.fr */
|
||||
/* Updated: 2025/11/10 20:42:43 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -26,6 +26,9 @@ class Brain
|
||||
~Brain();
|
||||
Brain(const Brain &other);
|
||||
Brain& operator=(const Brain &other);
|
||||
|
||||
void setIdea(int i, const std::string &idea);
|
||||
std::string getIdea(int i) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,9 +25,10 @@ Cat::~Cat()
|
||||
delete (this->brain);
|
||||
}
|
||||
|
||||
Cat::Cat(const Cat &other)
|
||||
Cat::Cat(const Cat &other) : Animal(other)
|
||||
{
|
||||
std::cout << "Cat copied" << std::endl;
|
||||
this->brain = NULL;
|
||||
*this = other;
|
||||
}
|
||||
|
||||
@@ -35,7 +36,12 @@ Cat& Cat::operator=(const Cat &other)
|
||||
{
|
||||
std::cout << "Cat copy assignment called" << std::endl;
|
||||
if (this != &other)
|
||||
this->type = other.type;
|
||||
{
|
||||
Animal::operator=(other);
|
||||
if (this->brain)
|
||||
delete (this->brain);
|
||||
this->brain = new Brain(*other.brain);
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,8 @@ class Cat : public Animal
|
||||
Cat& operator=(const Cat &other);
|
||||
|
||||
void makeSound() const;
|
||||
|
||||
Brain* getBrain() const { return brain; }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,9 +25,10 @@ Dog::~Dog()
|
||||
delete (this->brain);
|
||||
}
|
||||
|
||||
Dog::Dog(const Dog &other)
|
||||
Dog::Dog(const Dog &other) : Animal(other)
|
||||
{
|
||||
std::cout << "Dog copied" << std::endl;
|
||||
this->brain = NULL;
|
||||
*this = other;
|
||||
}
|
||||
|
||||
@@ -35,7 +36,12 @@ Dog& Dog::operator=(const Dog &other)
|
||||
{
|
||||
std::cout << "Dog copy assignment called" << std::endl;
|
||||
if (this != &other)
|
||||
this->type = other.type;
|
||||
{
|
||||
Animal::operator=(other);
|
||||
if (this->brain)
|
||||
delete (this->brain);
|
||||
this->brain = new Brain(*other.brain);
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,8 @@ class Dog : public Animal
|
||||
|
||||
void makeSound() const;
|
||||
|
||||
Brain* getBrain() const { return brain; }
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,40 +6,66 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/08 17:43:10 by hadi #+# #+# */
|
||||
/* Updated: 2025/11/10 10:33:20 by aortigos ### ########.fr */
|
||||
/* Updated: 2025/11/10 20:40:42 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal/Animal.hpp"
|
||||
#include "Dog/Dog.hpp"
|
||||
#include "Cat/Cat.hpp"
|
||||
#include "WrongAnimal/WrongAnimal.hpp"
|
||||
#include "WrongCat/WrongCat.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::endl << "----- Creating animals ------" << std::endl;
|
||||
const Animal* meta = new Animal();
|
||||
std::cout << "=== Basic Test ===" << std::endl;
|
||||
const Animal* j = new Dog();
|
||||
const Animal* i = new Cat();
|
||||
const WrongAnimal* z = new WrongCat();
|
||||
|
||||
std::cout << std::endl << "----- Animal sounds ------" << std::endl;
|
||||
delete j;
|
||||
delete i;
|
||||
|
||||
std::cout << j->getType() << " says: ";
|
||||
j->makeSound();
|
||||
std::cout << "\n=== Array Test ===" << std::endl;
|
||||
const int arraySize = 6;
|
||||
Animal* animals[arraySize];
|
||||
|
||||
std::cout << std::endl << i->getType() << " says ";
|
||||
i->makeSound();
|
||||
// Fill half with Dogs, half with Cats
|
||||
for (int i = 0; i < arraySize / 2; i++)
|
||||
animals[i] = new Dog();
|
||||
for (int i = arraySize / 2; i < arraySize; i++)
|
||||
animals[i] = new Cat();
|
||||
|
||||
std::cout << std::endl << z->getType() << " says ";
|
||||
z->makeSound();
|
||||
std::cout << "\n=== Making Sounds ===" << std::endl;
|
||||
for (int i = 0; i < arraySize; i++)
|
||||
{
|
||||
std::cout << "Animal " << i << " (" << animals[i]->getType() << "): ";
|
||||
animals[i]->makeSound();
|
||||
}
|
||||
|
||||
std::cout << std::endl << "----- Free all! ------" << std::endl;
|
||||
delete (meta);
|
||||
delete (j);
|
||||
delete (i);
|
||||
delete (z);
|
||||
std::cout << "\n=== Deleting Array ===" << std::endl;
|
||||
for (int i = 0; i < arraySize; i++)
|
||||
delete animals[i];
|
||||
|
||||
return (0);
|
||||
std::cout << "\n=== Deep Copy Test ===" << std::endl;
|
||||
Dog originalDog;
|
||||
originalDog.getBrain()->setIdea(0, "I love bones!");
|
||||
|
||||
Dog copiedDog = originalDog;
|
||||
copiedDog.getBrain()->setIdea(0, "I love balls!");
|
||||
|
||||
std::cout << "Original dog idea: " << originalDog.getBrain()->getIdea(0) << std::endl;
|
||||
std::cout << "Copied dog idea: " << copiedDog.getBrain()->getIdea(0) << std::endl;
|
||||
|
||||
std::cout << "\n=== Testing Brain Independence ===" << std::endl;
|
||||
Cat* cat1 = new Cat();
|
||||
cat1->getBrain()->setIdea(0, "Chase mice");
|
||||
|
||||
Cat* cat2 = new Cat(*cat1); // Copy constructor
|
||||
cat2->getBrain()->setIdea(0, "Sleep all day");
|
||||
|
||||
std::cout << "Cat1 idea: " << cat1->getBrain()->getIdea(0) << std::endl;
|
||||
std::cout << "Cat2 idea: " << cat2->getBrain()->getIdea(0) << std::endl;
|
||||
|
||||
delete cat1;
|
||||
delete cat2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user