d04 ex02 change pour pouvoir effectuer le test de erlazo

This commit is contained in:
hugogogo
2022-03-12 16:21:56 +01:00
parent 329e38b84b
commit 96e78e34a1
11 changed files with 138 additions and 16 deletions

View File

@@ -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;
}

View File

@@ -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; }

View File

@@ -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; }