ex01 finished

This commit is contained in:
2025-11-10 20:46:53 +01:00
parent b4b5a9b592
commit 783d649289
7 changed files with 98 additions and 29 deletions

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 11:22:21 by aortigos #+# #+# */ /* 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() Brain::Brain()
{ {
std::cout << "Brain has been created" << std::endl; std::cout << "Brain has been created" << std::endl;
int i = 0;
while (i < 100)
{
ideas[i++] = "Empty idea";
}
} }
Brain::~Brain() Brain::~Brain()
@@ -25,6 +30,7 @@ Brain::~Brain()
Brain::Brain(const Brain &other) Brain::Brain(const Brain &other)
{ {
std::cout << "Brain copied" << std::endl; std::cout << "Brain copied" << std::endl;
*this = other;
} }
Brain& Brain::operator=(const Brain &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; std::cout << "Brain copy assignment called" << std::endl;
if (this != &other) if (this != &other)
{ {
int i = 0;
while (i < 100)
{
this->ideas[i] = other.ideas[i];
i++;
}
} }
return (*this); 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");
}

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 11:22:17 by aortigos #+# #+# */ /* 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();
Brain(const Brain &other); Brain(const Brain &other);
Brain& operator=(const Brain &other); Brain& operator=(const Brain &other);
void setIdea(int i, const std::string &idea);
std::string getIdea(int i) const;
}; };
#endif #endif

View File

@@ -25,9 +25,10 @@ Cat::~Cat()
delete (this->brain); delete (this->brain);
} }
Cat::Cat(const Cat &other) Cat::Cat(const Cat &other) : Animal(other)
{ {
std::cout << "Cat copied" << std::endl; std::cout << "Cat copied" << std::endl;
this->brain = NULL;
*this = other; *this = other;
} }
@@ -35,7 +36,12 @@ Cat& Cat::operator=(const Cat &other)
{ {
std::cout << "Cat copy assignment called" << std::endl; std::cout << "Cat copy assignment called" << std::endl;
if (this != &other) if (this != &other)
this->type = other.type; {
Animal::operator=(other);
if (this->brain)
delete (this->brain);
this->brain = new Brain(*other.brain);
}
return (*this); return (*this);
} }

View File

@@ -30,6 +30,8 @@ class Cat : public Animal
Cat& operator=(const Cat &other); Cat& operator=(const Cat &other);
void makeSound() const; void makeSound() const;
Brain* getBrain() const { return brain; }
}; };
#endif #endif

View File

@@ -25,9 +25,10 @@ Dog::~Dog()
delete (this->brain); delete (this->brain);
} }
Dog::Dog(const Dog &other) Dog::Dog(const Dog &other) : Animal(other)
{ {
std::cout << "Dog copied" << std::endl; std::cout << "Dog copied" << std::endl;
this->brain = NULL;
*this = other; *this = other;
} }
@@ -35,7 +36,12 @@ Dog& Dog::operator=(const Dog &other)
{ {
std::cout << "Dog copy assignment called" << std::endl; std::cout << "Dog copy assignment called" << std::endl;
if (this != &other) if (this != &other)
this->type = other.type; {
Animal::operator=(other);
if (this->brain)
delete (this->brain);
this->brain = new Brain(*other.brain);
}
return (*this); return (*this);
} }

View File

@@ -31,6 +31,8 @@ class Dog : public Animal
void makeSound() const; void makeSound() const;
Brain* getBrain() const { return brain; }
}; };
#endif #endif

View File

@@ -6,40 +6,66 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:43:10 by hadi #+# #+# */ /* 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 "Animal/Animal.hpp"
#include "Dog/Dog.hpp" #include "Dog/Dog.hpp"
#include "Cat/Cat.hpp" #include "Cat/Cat.hpp"
#include "WrongAnimal/WrongAnimal.hpp"
#include "WrongCat/WrongCat.hpp"
int main() int main()
{ {
std::cout << std::endl << "----- Creating animals ------" << std::endl; std::cout << "=== Basic Test ===" << std::endl;
const Animal* meta = new Animal();
const Animal* j = new Dog(); const Animal* j = new Dog();
const Animal* i = new Cat(); 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: "; std::cout << "\n=== Array Test ===" << std::endl;
j->makeSound(); const int arraySize = 6;
Animal* animals[arraySize];
std::cout << std::endl << i->getType() << " says "; // Fill half with Dogs, half with Cats
i->makeSound(); 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 "; std::cout << "\n=== Making Sounds ===" << std::endl;
z->makeSound(); 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; std::cout << "\n=== Deleting Array ===" << std::endl;
delete (meta); for (int i = 0; i < arraySize; i++)
delete (j); delete animals[i];
delete (i);
delete (z);
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;
} }