ex01 finished
This commit is contained in:
@@ -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();
|
||||
const Animal* j = new Dog();
|
||||
const Animal* i = new Cat();
|
||||
const WrongAnimal* z = new WrongCat();
|
||||
std::cout << "=== Basic Test ===" << std::endl;
|
||||
const Animal* j = new Dog();
|
||||
const Animal* i = new Cat();
|
||||
|
||||
delete j;
|
||||
delete i;
|
||||
|
||||
std::cout << std::endl << "----- Animal sounds ------" << std::endl;
|
||||
|
||||
std::cout << j->getType() << " says: ";
|
||||
j->makeSound();
|
||||
std::cout << "\n=== Array Test ===" << std::endl;
|
||||
const int arraySize = 6;
|
||||
Animal* animals[arraySize];
|
||||
|
||||
// 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 << i->getType() << " says ";
|
||||
i->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 << z->getType() << " says ";
|
||||
z->makeSound();
|
||||
std::cout << "\n=== Deleting Array ===" << std::endl;
|
||||
for (int i = 0; i < arraySize; i++)
|
||||
delete animals[i];
|
||||
|
||||
std::cout << std::endl << "----- Free all! ------" << std::endl;
|
||||
delete (meta);
|
||||
delete (j);
|
||||
delete (i);
|
||||
delete (z);
|
||||
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!");
|
||||
|
||||
return (0);
|
||||
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