diff --git a/d04/ex01/Animal.cpp b/d04/ex01/Animal.cpp index 3f9502f..f6fe5b2 100644 --- a/d04/ex01/Animal.cpp +++ b/d04/ex01/Animal.cpp @@ -1,15 +1,19 @@ #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; } @@ -19,6 +23,7 @@ Animal::Animal( Animal const & src ) { *********************************************/ Animal::~Animal() { + std::cout << COPLIEN_COLOR "Animal destructor" RESET "\n"; return; } @@ -27,6 +32,7 @@ Animal::~Animal() { *********************************************/ Animal & Animal::operator=( Animal const & rhs ) { + std::cout << COPLIEN_COLOR "Animal assignator" RESET "\n"; if ( this != &rhs ) { type = rhs.getType(); diff --git a/d04/ex01/Animal.hpp b/d04/ex01/Animal.hpp index 6d72ba1..c804d6f 100644 --- a/d04/ex01/Animal.hpp +++ b/d04/ex01/Animal.hpp @@ -1,6 +1,7 @@ #ifndef ANIMAL_HPP # define ANIMAL_HPP +# include "color.h" #include #include @@ -8,9 +9,9 @@ class Animal { public: - Animal( void ); + Animal(); Animal( Animal const & src ); - ~Animal( void ); + virtual ~Animal( void ); Animal & operator=( Animal const & rhs ); virtual void makeSound() const; @@ -20,8 +21,6 @@ protected: std::string type; -private: - }; #endif diff --git a/d04/ex01/Brain.cpp b/d04/ex01/Brain.cpp new file mode 100644 index 0000000..bd3543e --- /dev/null +++ b/d04/ex01/Brain.cpp @@ -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; +} + diff --git a/d04/ex01/Brain.hpp b/d04/ex01/Brain.hpp new file mode 100644 index 0000000..e7b699d --- /dev/null +++ b/d04/ex01/Brain.hpp @@ -0,0 +1,30 @@ +#ifndef BRAIN_HPP +# define BRAIN_HPP + +#include "color.h" +#include +#include + +#define SIZE_IDEAS 100 + +class Brain { + +public: + + Brain( void ); + Brain( Brain const & src ); + ~Brain( void ); + Brain & operator=( Brain const & rhs ); + + void printIdea(int pos); + void putIdea(int pos, std::string idea); + void printIdeas(); + void putIdeas(std::string idea); + +private: + + std::string _ideas[SIZE_IDEAS]; +}; + +#endif + diff --git a/d04/ex01/Cat.cpp b/d04/ex01/Cat.cpp index 71212f1..734f11a 100644 --- a/d04/ex01/Cat.cpp +++ b/d04/ex01/Cat.cpp @@ -1,15 +1,20 @@ #include "Cat.hpp" +#define COPLIEN_COLOR B_CYAN + /********************************************* * CONSTRUCTORS *********************************************/ -Cat::Cat() { +Cat::Cat( Brain * brain ) { + std::cout << COPLIEN_COLOR "Cat constructor" RESET "\n"; type = "cat"; + _brain = brain; return; } Cat::Cat( Cat const & src ) { + std::cout << COPLIEN_COLOR "Cat copy constructor" RESET "\n"; *this = src; return; } @@ -19,6 +24,7 @@ Cat::Cat( Cat const & src ) { *********************************************/ Cat::~Cat() { + std::cout << COPLIEN_COLOR "Cat destructor" RESET "\n"; return; } @@ -27,8 +33,9 @@ Cat::~Cat() { *********************************************/ Cat & Cat::operator=( Cat const & rhs ) { + std::cout << COPLIEN_COLOR "Cat assignator" RESET "\n"; Animal::operator=(rhs); - + *_brain = *rhs._brain; return *this; } @@ -39,4 +46,9 @@ Cat & Cat::operator=( Cat const & rhs ) { void Cat::makeSound() const { std::cout << "*miaow*\n"; } - +void Cat::printBrain() const { + _brain->printIdeas(); +} +void Cat::printBrain(int pos) const { + _brain->printIdea(pos); +} diff --git a/d04/ex01/Cat.hpp b/d04/ex01/Cat.hpp index 0c7012e..2ff1055 100644 --- a/d04/ex01/Cat.hpp +++ b/d04/ex01/Cat.hpp @@ -1,26 +1,31 @@ #ifndef CAT_HPP # define CAT_HPP -# include "Animal.hpp" +# include "color.h" +# include +# include -#include -#include +# include "Animal.hpp" +# include "Brain.hpp" class Cat : public Animal { public: - Cat( void ); + Cat( Brain *brain = new Brain() ); Cat( Cat const & src ); - ~Cat( void ); + ~Cat(); Cat & operator=( Cat const & rhs ); void makeSound() const; -protected: + void printBrain() const; + void printBrain(int pos) const; private: + Brain *_brain; + }; #endif diff --git a/d04/ex01/Dog.cpp b/d04/ex01/Dog.cpp index dc4f62a..0b9f3ba 100644 --- a/d04/ex01/Dog.cpp +++ b/d04/ex01/Dog.cpp @@ -1,15 +1,20 @@ #include "Dog.hpp" +#define COPLIEN_COLOR B_CYAN + /********************************************* * CONSTRUCTORS *********************************************/ -Dog::Dog() { +Dog::Dog( Brain *brain ) { + std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n"; type = "dog"; + _brain = brain; return; } Dog::Dog( Dog const & src ) { + std::cout << COPLIEN_COLOR "Dog copy constructor" RESET "\n"; *this = src; return; } @@ -19,6 +24,7 @@ Dog::Dog( Dog const & src ) { *********************************************/ Dog::~Dog() { + std::cout << COPLIEN_COLOR "Dog destructor" RESET "\n"; return; } @@ -28,6 +34,7 @@ Dog::~Dog() { Dog & Dog::operator=( Dog const & rhs ) { Animal::operator=(rhs); + _brain = rhs._brain; return *this; } diff --git a/d04/ex01/Dog.hpp b/d04/ex01/Dog.hpp index 6a977df..5a75ddc 100644 --- a/d04/ex01/Dog.hpp +++ b/d04/ex01/Dog.hpp @@ -1,26 +1,28 @@ #ifndef DOG_HPP # define DOG_HPP -# include "Animal.hpp" - +# include "color.h" # include # include +# include "Animal.hpp" +# include "Brain.hpp" + class Dog : public Animal { public: - Dog( void ); + Dog( Brain *brain = new Brain() ); Dog( Dog const & src ); - ~Dog( void ); + ~Dog(); Dog & operator=( Dog const & rhs ); void makeSound() const; -protected: - private: + Brain *_brain; + }; #endif diff --git a/d04/ex01/Makefile b/d04/ex01/Makefile index 50eca1c..0fb712c 100644 --- a/d04/ex01/Makefile +++ b/d04/ex01/Makefile @@ -4,7 +4,7 @@ # . name is case sensitive . ?= set if not already set # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # -NAME = poly +NAME = burn #TYPE = c TYPE = cpp @@ -33,15 +33,13 @@ SRCS = main.cpp \ Animal.cpp \ Dog.cpp \ Cat.cpp \ - WrongAnimal.cpp \ - WrongCat.cpp + Brain.cpp D_HEADERS = . HEADERS = Animal.hpp \ Dog.hpp \ Cat.hpp \ - WrongAnimal.hpp \ - WrongCat.hpp + Brain.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d04/ex01/WrongAnimal.cpp b/d04/ex01/WrongAnimal.cpp deleted file mode 100644 index 61928ff..0000000 --- a/d04/ex01/WrongAnimal.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "WrongAnimal.hpp" - -/********************************************* - * CONSTRUCTORS - *********************************************/ - -WrongAnimal::WrongAnimal() { - type = "wrong_animal"; - return; -} - -WrongAnimal::WrongAnimal( WrongAnimal const & src ) { - *this = src; - return; -} - -/********************************************* - * DESTRUCTORS - *********************************************/ - -WrongAnimal::~WrongAnimal() { - return; -} - -/********************************************* - * OPERATORS - *********************************************/ - -WrongAnimal & WrongAnimal::operator=( WrongAnimal const & rhs ) { - if ( this != &rhs ) - { - type = rhs.getType(); - } - return *this; -} - -/********************************************* - * ACCESSORS - *********************************************/ - -std::string WrongAnimal::getType() const {return type;} - -/********************************************* - * PUBLIC MEMBER FUNCTIONS - *********************************************/ - -void WrongAnimal::makeSound() const { - std::cout << "*sound*\n"; -} - diff --git a/d04/ex01/WrongAnimal.hpp b/d04/ex01/WrongAnimal.hpp deleted file mode 100644 index 8dd40db..0000000 --- a/d04/ex01/WrongAnimal.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef WRONG_ANIMAL_HPP -# define WRONG_ANIMAL_HPP - -#include -#include - -class WrongAnimal { - -public: - - WrongAnimal( void ); - WrongAnimal( WrongAnimal const & src ); - ~WrongAnimal( void ); - WrongAnimal & operator=( WrongAnimal const & rhs ); - - void makeSound() const; - std::string getType() const; - -protected: - - std::string type; - -}; - -#endif - diff --git a/d04/ex01/WrongCat.cpp b/d04/ex01/WrongCat.cpp deleted file mode 100644 index 8d13eef..0000000 --- a/d04/ex01/WrongCat.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "WrongCat.hpp" - -/********************************************* - * CONSTRUCTORS - *********************************************/ - -WrongCat::WrongCat() { - type = "wrong_cat"; - return; -} - -WrongCat::WrongCat( WrongCat const & src ) { - *this = src; - return; -} - -/********************************************* - * DESTRUCTORS - *********************************************/ - -WrongCat::~WrongCat() { - return; -} - -/********************************************* - * OPERATORS - *********************************************/ - -WrongCat & WrongCat::operator=( WrongCat const & rhs ) { - WrongAnimal::operator=(rhs); - return *this; -} - -/********************************************* - * PUBLIC MEMBER FUNCTIONS - *********************************************/ - -void WrongCat::makeSound() const { - std::cout << "*miaow*\n"; -} diff --git a/d04/ex01/WrongCat.hpp b/d04/ex01/WrongCat.hpp deleted file mode 100644 index 158267d..0000000 --- a/d04/ex01/WrongCat.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef WRONG_CAT_HPP -# define WRONG_CAT_HPP - -# include "WrongAnimal.hpp" - -#include -#include - -class WrongCat : public WrongAnimal { - -public: - - WrongCat( void ); - WrongCat( WrongCat const & src ); - ~WrongCat( void ); - WrongCat & operator=( WrongCat const & rhs ); - - void makeSound() const; - -}; - -#endif - diff --git a/d04/ex01/burn b/d04/ex01/burn new file mode 100755 index 0000000..6199fb4 Binary files /dev/null and b/d04/ex01/burn differ diff --git a/d04/ex01/color.h b/d04/ex01/color.h new file mode 100644 index 0000000..e313f5f --- /dev/null +++ b/d04/ex01/color.h @@ -0,0 +1,24 @@ +#ifndef COLOR_H +# define COLOR_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif diff --git a/d04/ex01/main.cpp b/d04/ex01/main.cpp index 672ad4a..da15404 100644 --- a/d04/ex01/main.cpp +++ b/d04/ex01/main.cpp @@ -1,52 +1,118 @@ #include "Animal.hpp" #include "Dog.hpp" #include "Cat.hpp" -#include "WrongAnimal.hpp" -#include "WrongCat.hpp" #include #include +#include "color.h" + int main() { - const Animal* meta = new Animal(); - const Animal* j = new Dog(); - const Animal* i = new Cat(); - std::cout << j->getType() << " " << std::endl; - std::cout << i->getType() << " " << std::endl; - i->makeSound(); //will output the cat sound! - j->makeSound(); - meta->makeSound(); - + std::cout << B_YELLOW "\n1rst test :" RESET "\n"; { - std::cout << std::endl; - const Cat* i = new Cat(); - const Animal* j = new Cat(); - std::cout << j->getType() << " " << std::endl; - std::cout << i->getType() << " " << std::endl; - i->makeSound(); //will output the cat sound! - j->makeSound(); + const Animal* j = new Dog(); + const Animal* i = new Cat(); + delete j;//should not create a leak + delete i; } + std::cout << B_YELLOW "\n2nd test :" RESET "\n"; { - std::cout << std::endl; - const WrongAnimal* i = new WrongAnimal(); - const WrongAnimal* j = new WrongCat(); - std::cout << i->getType() << " " << std::endl; - std::cout << j->getType() << " " << std::endl; - i->makeSound(); //will output the cat sound! - j->makeSound(); + Dog * dog; + Cat * cat1; + Cat cat2; + Brain * brain1 = new Brain(); + Brain * brain2 = new Brain(); + + std::cout << B_BLUE "fill brain1 with \"giraffe\" :" RESET "\n"; + brain1->putIdeas("giraffe"); + std::cout << B_BLUE "print brain1 :" RESET "\n"; + brain1->printIdeas(); + std::cout << B_BLUE "print brain2 :" RESET "\n"; + brain2->printIdeas(); + std::cout << B_BLUE "brain2 copy brain1 :" RESET "\n"; + *brain2 = *brain1; + std::cout << B_BLUE "fill brain1 with \"hippopotamus\" :" RESET "\n"; + brain1->putIdeas("hippopotamus"); + std::cout << B_BLUE "print brain1 :" RESET "\n"; + brain1->printIdeas(); + std::cout << B_BLUE "print brain2 :" RESET "\n"; + brain2->printIdeas(); + + std::cout << B_BLUE "create new dog with brain1 :" RESET "\n"; + dog = new Dog(brain1); + 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 "fill brain1 with \"zebra\" :" RESET "\n"; + brain1->putIdeas("zebra"); + std::cout << B_BLUE "print cat1 :" RESET "\n"; + cat1->printBrain(); + std::cout << B_BLUE "print cat2 :" RESET "\n"; + cat2.printBrain(); + + delete dog; + delete cat1; + delete brain1; + delete brain2; } + std::cout << B_YELLOW "\narray animal test :" RESET "\n"; { - std::cout << std::endl; - const WrongCat* i = new WrongCat(); - const WrongAnimal* j = new WrongCat(); - std::cout << j->getType() << " " << std::endl; - std::cout << i->getType() << " " << std::endl; - i->makeSound(); //will output the cat sound! - j->makeSound(); + Animal *animals[10]; + int i; + + for (i = 0 ; i < 5 ; ++i) + animals[i] = new Cat(); + for ( ; i < 10 ; ++i) + animals[i] = new Dog(); + for (i = 0 ; i < 10 ; ++i) + animals[i]->makeSound(); + for (i = 0 ; i < 10 ; ++i) + delete animals[i]; } + std::cout << B_YELLOW "\ncopy constructor test1 :" RESET "\n"; + { + std::cout << B_BLUE "Cat a_cat :" RESET "\n"; + Cat a_cat; + std::cout << B_BLUE "Cat a_cpy_cat(a_cat) :" RESET "\n"; + Cat a_cpy_cat(a_cat); + } + + std::cout << B_YELLOW "\ncopy constructor test2 :" RESET "\n"; + { + std::cout << B_BLUE "Cat a_cat :" RESET "\n"; + Cat a_cat; + std::cout << B_BLUE "Cat a_cpy_cat = a_cat :" RESET "\n"; + Cat a_cpy_cat = a_cat; + } + + std::cout << B_YELLOW "\nassignation operator test1 :" RESET "\n"; + { + std::cout << B_BLUE "Cat a_cat :" RESET "\n"; + Cat a_cat; + std::cout << B_BLUE "Cat a_cpy_cat :" RESET "\n"; + Cat a_cpy_cat; + std::cout << B_BLUE "a_cpy_cat = a_cat :" RESET "\n"; + a_cpy_cat = a_cat; + } + + std::cout << B_YELLOW "\nassignation operator test2 :" RESET "\n"; + { + std::cout << B_BLUE "const Cat *a_cat :" RESET "\n"; + const Cat *a_cat = new Cat(); + std::cout << B_BLUE "Cat a_cpy_cat :" RESET "\n"; + Cat a_cpy_cat; + std::cout << B_BLUE "a_cpy_cat = *a_cat :" RESET "\n"; + a_cpy_cat = *a_cat; + + delete a_cat; + } + + return 0; }