d04 ex02 ok, presque rien a changer ou je me trompe ?

This commit is contained in:
hugogogo
2022-02-26 22:12:55 +01:00
parent ed2bbc7fd2
commit c056db80b7
27 changed files with 597 additions and 68 deletions

42
d04/ex02/srcs/Animal.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include "Animal.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Animal::Animal() {
std::cout << COPLIEN_COLOR "Animal constructor" RESET "\n";
type = "animal";
return;
}
Animal::Animal( Animal const & src ) {
std::cout << COPLIEN_COLOR "Animal copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Animal::~Animal() {
std::cout << COPLIEN_COLOR "Animal destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Animal & Animal::operator=( Animal const & rhs ) {
std::cout << COPLIEN_COLOR "Animal assignator" RESET "\n";
if ( this != &rhs )
type = rhs.getType();
return *this;
}
std::string Animal::getType() const {return type;}

65
d04/ex02/srcs/Brain.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "Brain.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Brain::Brain() {
std::cout << COPLIEN_COLOR "Brain constructor" RESET "\n";
return;
}
Brain::Brain( Brain const & src ) {
std::cout << COPLIEN_COLOR "Brain copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Brain::~Brain() {
std::cout << COPLIEN_COLOR "Brain destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Brain & Brain::operator=( Brain const & rhs ) {
std::cout << COPLIEN_COLOR "Brain assignator" RESET "\n";
if ( this != &rhs )
for (int i = 0; i < SIZE_IDEAS; i++)
_ideas[i] = rhs._ideas[i];
return *this;
}
/*********************************************
* ACCESSORS
*********************************************/
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;
}

73
d04/ex02/srcs/Cat.cpp Normal file
View File

@@ -0,0 +1,73 @@
#include "Cat.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
/*
* default arguments in default constructor : https://stackoverflow.com/questions/187640/default-parameters-with-c-constructors
* in this cas it doesn't work i think, since both constructors don't act exactly the same
*/
Cat::Cat() {
std::cout << COPLIEN_COLOR "Cat default constructor" RESET "\n";
type = "cat";
_brain = new Brain();
return;
}
Cat::Cat( Brain * brain ) {
std::cout << COPLIEN_COLOR "Cat parameters constructor" RESET "\n";
type = "cat";
_brain = new Brain();
*_brain = *brain;
return;
}
/*
* error: base class class Animal should be explicitly initialized in the copy constructor [-Werror=extra]
* Cat::Cat( Cat const & src ) {
* ^~~
* answer : https://stackoverflow.com/questions/43612772/base-class-class-a-should-be-explicitly-initialized-in-the-copy-constructor
*/
Cat::Cat( Cat const & src ) : Animal(src) {
std::cout << COPLIEN_COLOR "Cat copy constructor" RESET "\n";
_brain = new Brain();
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Cat::~Cat() {
std::cout << COPLIEN_COLOR "Cat destructor" RESET "\n";
delete _brain;
return;
}
/*********************************************
* OPERATORS
*********************************************/
Cat & Cat::operator=( Cat const & rhs ) {
std::cout << COPLIEN_COLOR "Cat assignator" RESET "\n";
Animal::operator=(rhs);
*_brain = *rhs._brain;
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void Cat::makeSound() const {
std::cout << "*miaow*\n";
}
void Cat::printBrain() const {
_brain->printIdeas();
}
void Cat::printBrain(int pos) const {
_brain->printIdea(pos);
}

65
d04/ex02/srcs/Dog.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "Dog.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Dog::Dog() {
std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n";
type = "dog";
_brain = new Brain();
return;
}
Dog::Dog( Brain *brain ) {
std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n";
type = "dog";
_brain = new Brain();
*_brain = *brain;
return;
}
/*
error: base class class Animal should be explicitly initialized in the copy constructor [-Werror=extra]
Dog::Dog( Dog const & src ) {
^~~
answer : https://stackoverflow.com/questions/43612772/base-class-class-a-should-be-explicitly-initialized-in-the-copy-constructor
*/
Dog::Dog( Dog const & src ) : Animal() {
std::cout << COPLIEN_COLOR "Dog copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Dog::~Dog() {
std::cout << COPLIEN_COLOR "Dog destructor" RESET "\n";
delete _brain;
return;
}
/*********************************************
* OPERATORS
*********************************************/
Dog & Dog::operator=( Dog const & rhs ) {
Animal::operator=(rhs);
_brain = new Brain();
if (this != &rhs)
_brain = rhs._brain;
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void Dog::makeSound() const {
std::cout << "*woof*\n";
}