diff --git a/d04/ex02/Makefile b/d04/ex02/Makefile index fc4b7c2..fd3eed7 100644 --- a/d04/ex02/Makefile +++ b/d04/ex02/Makefile @@ -17,7 +17,7 @@ ifeq "$(TYPE)" "c" CC = c EXT = c else ifeq "$(TYPE)" "cpp" - CC = c++ + CC = clang++ EXT = cpp endif diff --git a/d04/ex02/a.out b/d04/ex02/a.out new file mode 100755 index 0000000..9701802 Binary files /dev/null and b/d04/ex02/a.out differ diff --git a/d04/ex02/headers/Animal.hpp b/d04/ex02/headers/Animal.hpp index d055205..8378a82 100644 --- a/d04/ex02/headers/Animal.hpp +++ b/d04/ex02/headers/Animal.hpp @@ -2,23 +2,24 @@ # define ANIMAL_HPP # include "color.h" -#include -#include +# include +# include +# include "Brain.hpp" class Animal { public: - Animal(); Animal( Animal const & src ); virtual ~Animal( void ); - Animal & operator=( Animal const & rhs ); + virtual Animal & operator=( Animal const & rhs ); virtual void makeSound() const = 0; std::string getType() const; -protected: + virtual Brain * getBrain() const = 0; +protected: std::string type; }; diff --git a/d04/ex02/headers/Cat.hpp b/d04/ex02/headers/Cat.hpp index ba516e2..1c69329 100644 --- a/d04/ex02/headers/Cat.hpp +++ b/d04/ex02/headers/Cat.hpp @@ -15,14 +15,17 @@ public: Cat(); Cat( Brain *brain ); // Cat( Brain *brain = new Brain() ); + Cat( std::string ideas ); Cat( Cat const & src ); ~Cat(); Cat & operator=( Cat const & rhs ); + Cat & operator=( Animal const & rhs ); void makeSound() const; void printBrain() const; void printBrain(int pos) const; + Brain * getBrain() const; private: diff --git a/d04/ex02/headers/Dog.hpp b/d04/ex02/headers/Dog.hpp index 17617cd..2fdc58d 100644 --- a/d04/ex02/headers/Dog.hpp +++ b/d04/ex02/headers/Dog.hpp @@ -17,8 +17,10 @@ public: Dog( Dog const & src ); ~Dog(); Dog & operator=( Dog const & rhs ); + Dog & operator=( Animal const & rhs ); void makeSound() const; + Brain * getBrain() const; private: diff --git a/d04/ex02/main.cpp b/d04/ex02/main.cpp index b443872..8292f33 100644 --- a/d04/ex02/main.cpp +++ b/d04/ex02/main.cpp @@ -6,11 +6,45 @@ #include #include "color.h" -#define N_TEST "7" +#define N_TEST "9" int main() { int i = 0; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] test erlazo :" RESET "\n"; + { + Animal* i = new Cat("I am catwoman"); + Animal* j = new Cat("I am just a cat"); + + std::cout << std::endl; + std::cout << "cat i : "; + i->getBrain()->printIdea(0); + std::cout << "cat j : "; + j->getBrain()->printIdea(0); + + std::cout << "\n*i = *j\n"; + *i = *j; + + std::cout << "cat i : "; + i->getBrain()->printIdea(0); + std::cout << "cat j : "; + j->getBrain()->printIdea(0); + + std::cout << "\nj->getBrain->putIdea(\"I am not a cat\")\n"; + j->getBrain()->putIdea(0, "I am not a cat");; + + std::cout << "cat i : "; + i->getBrain()->printIdea(0); + std::cout << "cat j : "; + j->getBrain()->printIdea(0); + + std::cout << std::endl; + std::cout << "delete i\n"; + delete i; + std::cout << "delete j\n"; + delete j; + } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] test subject :" RESET "\n"; { const Animal* j = new Dog(); @@ -66,6 +100,24 @@ int main() { delete brain2; } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] test with brain :" RESET "\n"; + { + Cat * cat1; + Cat cat2; + Brain * brain1 = new Brain(); + + + std::cout << B_BLUE "create new cat with brain1 :" RESET "\n"; + cat1 = new Cat(brain1); + std::cout << B_BLUE "cat2 copy cat1 :" RESET "\n"; + cat2 = *cat1; + + std::cout << B_BLUE "delete cat1 :" RESET "\n"; + delete cat1; + std::cout << B_BLUE "delete brain1 :" RESET "\n"; + delete brain1; + } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] array animal test :" RESET "\n"; { Animal *animals[10]; diff --git a/d04/ex02/pure b/d04/ex02/pure index 52f6480..956ca8d 100755 Binary files a/d04/ex02/pure and b/d04/ex02/pure differ diff --git a/d04/ex02/srcs/Brain.cpp b/d04/ex02/srcs/Brain.cpp index bd3543e..3c552f8 100644 --- a/d04/ex02/srcs/Brain.cpp +++ b/d04/ex02/srcs/Brain.cpp @@ -46,20 +46,16 @@ void Brain::printIdea(int pos) { if (pos < SIZE_IDEAS) std::cout << _ideas[pos] << "\n"; } - void Brain::putIdea(int pos, std::string idea) { if (pos < SIZE_IDEAS) _ideas[pos] = idea; } - void Brain::printIdeas() { for (int i = 0; i < SIZE_IDEAS; i++) std::cout << _ideas[i] << " - "; std::cout << "\n"; } - void Brain::putIdeas(std::string idea) { for (int i = 0; i < SIZE_IDEAS; i++) _ideas[i] = idea; } - diff --git a/d04/ex02/srcs/Cat.cpp b/d04/ex02/srcs/Cat.cpp index b9a3ace..621d138 100644 --- a/d04/ex02/srcs/Cat.cpp +++ b/d04/ex02/srcs/Cat.cpp @@ -17,12 +17,19 @@ Cat::Cat() { return; } Cat::Cat( Brain * brain ) { - std::cout << COPLIEN_COLOR "Cat parameters constructor" RESET "\n"; + std::cout << COPLIEN_COLOR "Cat parameters brain constructor" RESET "\n"; type = "cat"; _brain = new Brain(); *_brain = *brain; return; } +Cat::Cat( std::string ideas ) { + std::cout << COPLIEN_COLOR "Cat parameters ideas constructor" RESET "\n"; + type = "cat"; + _brain = new Brain(); + _brain->putIdeas(ideas); + return; +} /* * error: base class ‘class Animal’ should be explicitly initialized in the copy constructor [-Werror=extra] @@ -52,9 +59,19 @@ Cat::~Cat() { *********************************************/ Cat & Cat::operator=( Cat const & rhs ) { - std::cout << COPLIEN_COLOR "Cat assignator" RESET "\n"; Animal::operator=(rhs); - *_brain = *rhs._brain; + std::cout << COPLIEN_COLOR "Cat assignator" RESET "\n"; + if (this != &rhs) + *_brain = *(rhs.getBrain()); + return *this; +} +// need of a second overload in case "Animal cat" = "Animal cat"; +// https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended +Cat & Cat::operator=( Animal const & rhs ) { + Animal::operator=(rhs); + std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n"; + if (this != &rhs) + *_brain = *(rhs.getBrain()); return *this; } @@ -71,3 +88,6 @@ void Cat::printBrain() const { void Cat::printBrain(int pos) const { _brain->printIdea(pos); } + +Brain * Cat::getBrain() const { return _brain; } + diff --git a/d04/ex02/srcs/Dog.cpp b/d04/ex02/srcs/Dog.cpp index f6f1cdb..cacfa69 100644 --- a/d04/ex02/srcs/Dog.cpp +++ b/d04/ex02/srcs/Dog.cpp @@ -49,9 +49,18 @@ Dog::~Dog() { Dog & Dog::operator=( Dog const & rhs ) { Animal::operator=(rhs); - _brain = new Brain(); + std::cout << COPLIEN_COLOR "Dog assignator" RESET "\n"; if (this != &rhs) - _brain = rhs._brain; + *_brain = *(rhs.getBrain()); + return *this; +} +// need of a second overload in case "Animal cat" = "Animal cat"; +// https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended +Dog & Dog::operator=( Animal const & rhs ) { + Animal::operator=(rhs); + std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n"; + if (this != &rhs) + *_brain = *(rhs.getBrain()); return *this; } @@ -63,3 +72,4 @@ void Dog::makeSound() const { std::cout << "*woof*\n"; } +Brain * Dog::getBrain() const { return _brain; } diff --git a/d04/ex02/test_assignement.cpp b/d04/ex02/test_assignement.cpp new file mode 100644 index 0000000..f01af24 --- /dev/null +++ b/d04/ex02/test_assignement.cpp @@ -0,0 +1,38 @@ +# include +# include + +class Animal { +public: + virtual Animal & operator=( Animal const & rhs ) { + std::cout << "Animal operator=\n"; + return *this; + } +}; + +class Cat : public Animal { +public: + Cat & operator=( Cat const & rhs ) { + Animal::operator=(rhs); + std::cout << "Cat operator=\n"; + return *this; + } + Cat & operator=( Animal const & rhs ) { + Animal::operator=(rhs); + std::cout << "Cat operator=\n"; + return *this; + } + void catSpeak() {std::cout << "I am a cat\n";} +}; + +int main() { + Animal* i = new Cat(); + Animal* j = new Cat(); + + i->catSpeak(); + j->catSpeak(); + *i = *j; + + delete i; + delete j; +} +