From 783d649289ad94e033c74d49459186af33aaa661 Mon Sep 17 00:00:00 2001 From: aortigos Date: Mon, 10 Nov 2025 20:46:53 +0100 Subject: [PATCH] ex01 finished --- ex01/Brain/Brain.cpp | 28 ++++++++++++++++-- ex01/Brain/Brain.hpp | 5 +++- ex01/Cat/Cat.cpp | 10 +++++-- ex01/Cat/Cat.hpp | 2 ++ ex01/Dog/Dog.cpp | 10 +++++-- ex01/Dog/Dog.hpp | 2 ++ ex01/main.cpp | 70 ++++++++++++++++++++++++++++++-------------- 7 files changed, 98 insertions(+), 29 deletions(-) diff --git a/ex01/Brain/Brain.cpp b/ex01/Brain/Brain.cpp index ad076b5..e18ed94 100644 --- a/ex01/Brain/Brain.cpp +++ b/ex01/Brain/Brain.cpp @@ -6,7 +6,7 @@ /* By: aortigos 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"); +} \ No newline at end of file diff --git a/ex01/Brain/Brain.hpp b/ex01/Brain/Brain.hpp index 57711d6..c95fe84 100644 --- a/ex01/Brain/Brain.hpp +++ b/ex01/Brain/Brain.hpp @@ -6,7 +6,7 @@ /* By: aortigos 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); } diff --git a/ex01/Cat/Cat.hpp b/ex01/Cat/Cat.hpp index da72feb..fe4460f 100644 --- a/ex01/Cat/Cat.hpp +++ b/ex01/Cat/Cat.hpp @@ -30,6 +30,8 @@ class Cat : public Animal Cat& operator=(const Cat &other); void makeSound() const; + + Brain* getBrain() const { return brain; } }; #endif diff --git a/ex01/Dog/Dog.cpp b/ex01/Dog/Dog.cpp index edcd4cf..abf6aad 100644 --- a/ex01/Dog/Dog.cpp +++ b/ex01/Dog/Dog.cpp @@ -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); } diff --git a/ex01/Dog/Dog.hpp b/ex01/Dog/Dog.hpp index d5448d7..dd62dce 100644 --- a/ex01/Dog/Dog.hpp +++ b/ex01/Dog/Dog.hpp @@ -31,6 +31,8 @@ class Dog : public Animal void makeSound() const; + Brain* getBrain() const { return brain; } + }; #endif diff --git a/ex01/main.cpp b/ex01/main.cpp index b895cb7..a72c283 100644 --- a/ex01/main.cpp +++ b/ex01/main.cpp @@ -6,40 +6,66 @@ /* By: aortigos 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; }