diff --git a/cpp04/ex01/Animal.hpp b/cpp04/ex01/Animal.hpp index a2f9dbe..a2382dc 100644 --- a/cpp04/ex01/Animal.hpp +++ b/cpp04/ex01/Animal.hpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/14 02:05:20 by apommier #+# #+# */ -/* Updated: 2022/07/17 19:02:35 by apommier ### ########.fr */ +/* Updated: 2022/08/03 19:54:00 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp04/ex01/Brain.cpp b/cpp04/ex01/Brain.cpp index 8607f7f..a17511c 100644 --- a/cpp04/ex01/Brain.cpp +++ b/cpp04/ex01/Brain.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/15 04:37:19 by apommier #+# #+# */ -/* Updated: 2022/07/18 09:17:44 by apommier ### ########.fr */ +/* Updated: 2022/08/03 18:37:29 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,9 +33,8 @@ Brain &Brain::operator=(const Brain& rhs) std::cout << "Brain assignement operator called" << std::endl; if (this != &rhs) { - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 100; i++) this->_ideas[i] = rhs._ideas[i]; - } } return (*this); } diff --git a/cpp04/ex01/Cat.cpp b/cpp04/ex01/Cat.cpp index d689c10..cc2f0df 100644 --- a/cpp04/ex01/Cat.cpp +++ b/cpp04/ex01/Cat.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/14 02:11:37 by apommier #+# #+# */ -/* Updated: 2022/08/03 13:20:30 by apommier ### ########.fr */ +/* Updated: 2022/08/03 19:13:07 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,13 @@ Cat::Cat() : Animal() { std::cout << "Cat Default constructor called" << std::endl; this->type = "Cat"; - this->_Brain = new Brain; + this->_Brain = new Brain(); } Cat::Cat(const Cat& copy) : Animal(copy) { std::cout << "Cat Copy constructor called" << std::endl; + this->_Brain = new Brain(); *this = copy; } @@ -43,9 +44,9 @@ Cat &Cat::operator=(const Cat& rhs) { this->type = rhs.getType(); delete this->_Brain; - this->_Brain = new Brain(*rhs._Brain); + this->_Brain = new Brain(*rhs.getBrain()); } - return (*this); + return (*this); } Animal &Cat::operator=( const Animal &rhs) diff --git a/cpp04/ex01/Dog.cpp b/cpp04/ex01/Dog.cpp index 1ab349d..c7bbd7a 100644 --- a/cpp04/ex01/Dog.cpp +++ b/cpp04/ex01/Dog.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/14 02:11:41 by apommier #+# #+# */ -/* Updated: 2022/08/03 13:20:17 by apommier ### ########.fr */ +/* Updated: 2022/08/03 19:11:57 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,13 @@ Dog::Dog() : Animal() { std::cout << "Dog Default constructor called" << std::endl; this->type = "Dog"; - this->_Brain = new Brain; + this->_Brain = new Brain(); } Dog::Dog(const Dog& copy) : Animal(copy) { std::cout << "Dog Copy constructor called" << std::endl; + this->_Brain = new Brain(); *this = copy; } @@ -43,7 +44,7 @@ Dog &Dog::operator=(const Dog& rhs) { this->type = rhs.getType(); delete this->_Brain; - this->_Brain = new Brain(*rhs._Brain); + this->_Brain = new Brain(*rhs.getBrain()); } return (*this); } diff --git a/cpp04/ex01/Makefile b/cpp04/ex01/Makefile index 0205728..b104e7e 100644 --- a/cpp04/ex01/Makefile +++ b/cpp04/ex01/Makefile @@ -6,7 +6,7 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/07/14 02:04:13 by apommier #+# #+# # -# Updated: 2022/07/22 07:48:18 by apommier ### ########.fr # +# Updated: 2022/08/03 18:35:07 by apommier ### ########.fr # # # # **************************************************************************** # @@ -21,7 +21,7 @@ SRCS = main.cpp\ OBJS = ${SRCS:.cpp=.o} CC = c++ -CFLAGS = -Wall -Wextra -Werror -std=c++98 +CFLAGS = -Wall -Wextra -Werror -std=c++98 -g RM = rm -rf .cpp.o: diff --git a/cpp04/ex01/main.cpp b/cpp04/ex01/main.cpp index 591256a..2587424 100644 --- a/cpp04/ex01/main.cpp +++ b/cpp04/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/14 02:04:10 by apommier #+# #+# */ -/* Updated: 2022/08/03 13:44:10 by apommier ### ########.fr */ +/* Updated: 2022/08/03 19:33:10 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,18 @@ void copyTest() { std::cout << std::endl; - Dog first; - Dog cpy = first; + std::cout << "test to know sort of copy\n"; + + Dog firstDog; + std::cout << std::endl; + Dog cpyDog = firstDog; + std::cout << &firstDog << "----" << &cpyDog <getType() << std::endl; } + + std::cout << std::endl; for (int i = 0; i < 10; i++) { std::cout << i + 1 << "- "<< Animals[i]->getBrain() << std::endl; } - //copyTest(); + + copyTest(); + std::cout << "End of copy test\n\n"; + + std::cout << "Ideas of a cat" << std::endl; + std::cout << Animals[1]->getBrain()->getIdeas()[0] << std::endl; + std::cout << Animals[1]->getBrain()->getIdeas()[1] << std::endl; + std::cout << Animals[1]->getBrain()->getIdeas()[2] << std::endl; + + std::cout << "\nIdeas of a dog" << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl; + std::cout << std::endl; + + *Animals[6] = *Animals[1]; + std::cout << std::endl << "Ideas of the previous dog after copy" << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl; + + std::cout << std::endl; + for (int i = 0; i < 10; 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 Cat(); -// else -// animals[i] = new Dog(); -// 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]; -// } \ No newline at end of file +} \ No newline at end of file diff --git a/cpp04/ex02/Animal.cpp b/cpp04/ex02/Animal.cpp index fc947b4..504c02c 100644 --- a/cpp04/ex02/Animal.cpp +++ b/cpp04/ex02/Animal.cpp @@ -6,7 +6,7 @@ /* 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) { std::cout << "Animal Copy constructor called" << std::endl; - this->type = copy.getType(); + *this = copy; } Animal::~Animal() diff --git a/cpp04/ex02/Animal.hpp b/cpp04/ex02/Animal.hpp index 8010e3c..3dca9ab 100644 --- a/cpp04/ex02/Animal.hpp +++ b/cpp04/ex02/Animal.hpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/14 02:05:20 by apommier #+# #+# */ -/* Updated: 2022/07/18 18:07:34 by apommier ### ########.fr */ +/* Updated: 2022/08/03 19:52:23 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp04/ex02/Brain.cpp b/cpp04/ex02/Brain.cpp index 8607f7f..a17511c 100644 --- a/cpp04/ex02/Brain.cpp +++ b/cpp04/ex02/Brain.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/15 04:37:19 by apommier #+# #+# */ -/* Updated: 2022/07/18 09:17:44 by apommier ### ########.fr */ +/* Updated: 2022/08/03 18:37:29 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,9 +33,8 @@ Brain &Brain::operator=(const Brain& rhs) std::cout << "Brain assignement operator called" << std::endl; if (this != &rhs) { - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 100; i++) this->_ideas[i] = rhs._ideas[i]; - } } return (*this); } diff --git a/cpp04/ex02/Cat.cpp b/cpp04/ex02/Cat.cpp index 894bfb0..cc2f0df 100644 --- a/cpp04/ex02/Cat.cpp +++ b/cpp04/ex02/Cat.cpp @@ -6,7 +6,7 @@ /* 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 19:13:07 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,14 @@ Cat::Cat() : Animal() { std::cout << "Cat Default constructor called" << std::endl; this->type = "Cat"; - this->_Brain = new Brain; + this->_Brain = new Brain(); } Cat::Cat(const Cat& copy) : Animal(copy) { std::cout << "Cat Copy constructor called" << std::endl; - this->type = copy.getType(); - //this->_Brain = copy.getType(); + this->_Brain = new Brain(); + *this = copy; } Cat::~Cat() @@ -44,9 +44,9 @@ Cat &Cat::operator=(const Cat& rhs) { this->type = rhs.getType(); delete this->_Brain; - this->_Brain = new Brain(*rhs._Brain); + this->_Brain = new Brain(*rhs.getBrain()); } - return (*this); + return (*this); } Animal &Cat::operator=( const Animal &rhs) diff --git a/cpp04/ex02/Cat.hpp b/cpp04/ex02/Cat.hpp index 332984c..78e1e07 100644 --- a/cpp04/ex02/Cat.hpp +++ b/cpp04/ex02/Cat.hpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/14 02:11:39 by apommier #+# #+# */ -/* Updated: 2022/07/17 18:39:59 by apommier ### ########.fr */ +/* Updated: 2022/08/03 19:50:44 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp04/ex02/Dog.cpp b/cpp04/ex02/Dog.cpp index a0ec1d2..c7bbd7a 100644 --- a/cpp04/ex02/Dog.cpp +++ b/cpp04/ex02/Dog.cpp @@ -6,7 +6,7 @@ /* 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 19:11:57 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,14 @@ Dog::Dog() : Animal() { std::cout << "Dog Default constructor called" << std::endl; this->type = "Dog"; - this->_Brain = new Brain; + this->_Brain = new Brain(); } Dog::Dog(const Dog& copy) : Animal(copy) { std::cout << "Dog Copy constructor called" << std::endl; - this->type = copy.getType(); + this->_Brain = new Brain(); + *this = copy; } Dog::~Dog() @@ -43,7 +44,7 @@ Dog &Dog::operator=(const Dog& rhs) { this->type = rhs.getType(); delete this->_Brain; - this->_Brain = new Brain(*rhs._Brain); + this->_Brain = new Brain(*rhs.getBrain()); } return (*this); } diff --git a/cpp04/ex02/Makefile b/cpp04/ex02/Makefile index 0205728..b104e7e 100644 --- a/cpp04/ex02/Makefile +++ b/cpp04/ex02/Makefile @@ -6,7 +6,7 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/07/14 02:04:13 by apommier #+# #+# # -# Updated: 2022/07/22 07:48:18 by apommier ### ########.fr # +# Updated: 2022/08/03 18:35:07 by apommier ### ########.fr # # # # **************************************************************************** # @@ -21,7 +21,7 @@ SRCS = main.cpp\ OBJS = ${SRCS:.cpp=.o} CC = c++ -CFLAGS = -Wall -Wextra -Werror -std=c++98 +CFLAGS = -Wall -Wextra -Werror -std=c++98 -g RM = rm -rf .cpp.o: diff --git a/cpp04/ex02/main.cpp b/cpp04/ex02/main.cpp index 0a3094b..02adb76 100644 --- a/cpp04/ex02/main.cpp +++ b/cpp04/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/14 02:04:10 by apommier #+# #+# */ -/* Updated: 2022/07/18 20:29:04 by apommier ### ########.fr */ +/* Updated: 2022/08/03 19:45:13 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,64 +16,80 @@ #include "WrongAnimal.hpp" #include "WrongCat.hpp" -/*int main() +void copyTest() +{ + std::cout << std::endl; + std::cout << "test to know sort of copy\n"; + + Dog firstDog; + std::cout << std::endl; + Dog cpyDog = firstDog; + std::cout << &firstDog << "----" << &cpyDog <getBrain()->setgetIdeas(0) "Sleep"); - Animals[i]->getBrain()->setgetIdeas(1) "Hate"); - Animals[i]->getBrain()->setgetIdeas(2) "Sleep"); + oneBrain = Animals[i]->getBrain(); + oneBrain->setIdeas(0, "Sleep"); + oneBrain->setIdeas(1, "Hate"); + oneBrain->setIdeas(2, "Sleep"); } else { Animals[i] = new Dog; - Animals[i]->getBrain()->setgetIdeas(0) "Sleep"); - Animals[i]->getBrain()->setgetIdeas(1) "Play"); - Animals[i]->getBrain()->setgetIdeas(2) "Ate"); + oneBrain = Animals[i]->getBrain(); + oneBrain->setIdeas(0, "Sleep"); + oneBrain->setIdeas(1, "Play"); + oneBrain->setIdeas(2, "Ate"); } std::cout << Animals[i]->getType() << std::endl; } + + std::cout << std::endl; for (int i = 0; i < 10; i++) { - std::cout << Animals[i]->getBrain() << std::endl; + std::cout << i + 1 << "- "<< Animals[i]->getBrain() << std::endl; } + + copyTest(); + std::cout << "End of copy test\n\n"; + + std::cout << "Ideas of a cat" << std::endl; + std::cout << Animals[1]->getBrain()->getIdeas()[0] << std::endl; + std::cout << Animals[1]->getBrain()->getIdeas()[1] << std::endl; + std::cout << Animals[1]->getBrain()->getIdeas()[2] << std::endl; + + std::cout << "\nIdeas of a dog" << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl; + std::cout << std::endl; + + *Animals[6] = *Animals[1]; + std::cout << std::endl << "Ideas of the previous dog after copy" << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[0] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[1] << std::endl; + std::cout << Animals[6]->getBrain()->getIdeas()[2] << std::endl; + + std::cout << std::endl; + for (int i = 0; i < 10; 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(); - 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]; } \ No newline at end of file