d04 ex02 change pour pouvoir effectuer le test de erlazo
This commit is contained in:
@@ -17,7 +17,7 @@ ifeq "$(TYPE)" "c"
|
|||||||
CC = c
|
CC = c
|
||||||
EXT = c
|
EXT = c
|
||||||
else ifeq "$(TYPE)" "cpp"
|
else ifeq "$(TYPE)" "cpp"
|
||||||
CC = c++
|
CC = clang++
|
||||||
EXT = cpp
|
EXT = cpp
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|||||||
BIN
d04/ex02/a.out
Executable file
BIN
d04/ex02/a.out
Executable file
Binary file not shown.
@@ -2,23 +2,24 @@
|
|||||||
# define ANIMAL_HPP
|
# define ANIMAL_HPP
|
||||||
|
|
||||||
# include "color.h"
|
# include "color.h"
|
||||||
#include <iostream>
|
# include <iostream>
|
||||||
#include <string>
|
# include <string>
|
||||||
|
# include "Brain.hpp"
|
||||||
|
|
||||||
class Animal {
|
class Animal {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Animal();
|
Animal();
|
||||||
Animal( Animal const & src );
|
Animal( Animal const & src );
|
||||||
virtual ~Animal( void );
|
virtual ~Animal( void );
|
||||||
Animal & operator=( Animal const & rhs );
|
virtual Animal & operator=( Animal const & rhs );
|
||||||
|
|
||||||
virtual void makeSound() const = 0;
|
virtual void makeSound() const = 0;
|
||||||
std::string getType() const;
|
std::string getType() const;
|
||||||
|
|
||||||
protected:
|
virtual Brain * getBrain() const = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
std::string type;
|
std::string type;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -15,14 +15,17 @@ public:
|
|||||||
Cat();
|
Cat();
|
||||||
Cat( Brain *brain );
|
Cat( Brain *brain );
|
||||||
// Cat( Brain *brain = new Brain() );
|
// Cat( Brain *brain = new Brain() );
|
||||||
|
Cat( std::string ideas );
|
||||||
Cat( Cat const & src );
|
Cat( Cat const & src );
|
||||||
~Cat();
|
~Cat();
|
||||||
Cat & operator=( Cat const & rhs );
|
Cat & operator=( Cat const & rhs );
|
||||||
|
Cat & operator=( Animal const & rhs );
|
||||||
|
|
||||||
void makeSound() const;
|
void makeSound() const;
|
||||||
|
|
||||||
void printBrain() const;
|
void printBrain() const;
|
||||||
void printBrain(int pos) const;
|
void printBrain(int pos) const;
|
||||||
|
Brain * getBrain() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,10 @@ public:
|
|||||||
Dog( Dog const & src );
|
Dog( Dog const & src );
|
||||||
~Dog();
|
~Dog();
|
||||||
Dog & operator=( Dog const & rhs );
|
Dog & operator=( Dog const & rhs );
|
||||||
|
Dog & operator=( Animal const & rhs );
|
||||||
|
|
||||||
void makeSound() const;
|
void makeSound() const;
|
||||||
|
Brain * getBrain() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,45 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#define N_TEST "7"
|
#define N_TEST "9"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int i = 0;
|
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";
|
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] test subject :" RESET "\n";
|
||||||
{
|
{
|
||||||
const Animal* j = new Dog();
|
const Animal* j = new Dog();
|
||||||
@@ -66,6 +100,24 @@ int main() {
|
|||||||
delete brain2;
|
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";
|
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] array animal test :" RESET "\n";
|
||||||
{
|
{
|
||||||
Animal *animals[10];
|
Animal *animals[10];
|
||||||
|
|||||||
BIN
d04/ex02/pure
BIN
d04/ex02/pure
Binary file not shown.
@@ -46,20 +46,16 @@ void Brain::printIdea(int pos) {
|
|||||||
if (pos < SIZE_IDEAS)
|
if (pos < SIZE_IDEAS)
|
||||||
std::cout << _ideas[pos] << "\n";
|
std::cout << _ideas[pos] << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Brain::putIdea(int pos, std::string idea) {
|
void Brain::putIdea(int pos, std::string idea) {
|
||||||
if (pos < SIZE_IDEAS)
|
if (pos < SIZE_IDEAS)
|
||||||
_ideas[pos] = idea;
|
_ideas[pos] = idea;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Brain::printIdeas() {
|
void Brain::printIdeas() {
|
||||||
for (int i = 0; i < SIZE_IDEAS; i++)
|
for (int i = 0; i < SIZE_IDEAS; i++)
|
||||||
std::cout << _ideas[i] << " - ";
|
std::cout << _ideas[i] << " - ";
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Brain::putIdeas(std::string idea) {
|
void Brain::putIdeas(std::string idea) {
|
||||||
for (int i = 0; i < SIZE_IDEAS; i++)
|
for (int i = 0; i < SIZE_IDEAS; i++)
|
||||||
_ideas[i] = idea;
|
_ideas[i] = idea;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,12 +17,19 @@ Cat::Cat() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Cat::Cat( Brain * brain ) {
|
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";
|
type = "cat";
|
||||||
_brain = new Brain();
|
_brain = new Brain();
|
||||||
*_brain = *brain;
|
*_brain = *brain;
|
||||||
return;
|
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]
|
* 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 ) {
|
Cat & Cat::operator=( Cat const & rhs ) {
|
||||||
std::cout << COPLIEN_COLOR "Cat assignator" RESET "\n";
|
|
||||||
Animal::operator=(rhs);
|
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;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,3 +88,6 @@ void Cat::printBrain() const {
|
|||||||
void Cat::printBrain(int pos) const {
|
void Cat::printBrain(int pos) const {
|
||||||
_brain->printIdea(pos);
|
_brain->printIdea(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Brain * Cat::getBrain() const { return _brain; }
|
||||||
|
|
||||||
|
|||||||
@@ -49,9 +49,18 @@ Dog::~Dog() {
|
|||||||
|
|
||||||
Dog & Dog::operator=( Dog const & rhs ) {
|
Dog & Dog::operator=( Dog const & rhs ) {
|
||||||
Animal::operator=(rhs);
|
Animal::operator=(rhs);
|
||||||
_brain = new Brain();
|
std::cout << COPLIEN_COLOR "Dog assignator" RESET "\n";
|
||||||
if (this != &rhs)
|
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;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,3 +72,4 @@ void Dog::makeSound() const {
|
|||||||
std::cout << "*woof*\n";
|
std::cout << "*woof*\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Brain * Dog::getBrain() const { return _brain; }
|
||||||
|
|||||||
38
d04/ex02/test_assignement.cpp
Normal file
38
d04/ex02/test_assignement.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# include <iostream>
|
||||||
|
# include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user