diff --git a/d04/WrongCat.hpp b/d04/WrongCat.hpp deleted file mode 100644 index 158267d..0000000 --- a/d04/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/ex00/poly b/d04/ex00/poly deleted file mode 100755 index 6b37710..0000000 Binary files a/d04/ex00/poly and /dev/null differ diff --git a/d04/ex01/Makefile b/d04/ex01/Makefile index 0fb712c..5de1a7f 100644 --- a/d04/ex01/Makefile +++ b/d04/ex01/Makefile @@ -28,14 +28,14 @@ LIBS = INCLUDES = -I$(D_HEADERS) -D_SRCS = . +D_SRCS = srcs SRCS = main.cpp \ Animal.cpp \ Dog.cpp \ Cat.cpp \ Brain.cpp -D_HEADERS = . +D_HEADERS = headers HEADERS = Animal.hpp \ Dog.hpp \ Cat.hpp \ diff --git a/d04/ex01/a.out b/d04/ex01/a.out deleted file mode 100755 index 459a130..0000000 Binary files a/d04/ex01/a.out and /dev/null differ diff --git a/d04/ex01/fr.subject.pdf b/d04/ex01/fr.subject.pdf deleted file mode 100644 index d3212de..0000000 Binary files a/d04/ex01/fr.subject.pdf and /dev/null differ diff --git a/d04/ex01/Animal.hpp b/d04/ex01/headers/Animal.hpp similarity index 100% rename from d04/ex01/Animal.hpp rename to d04/ex01/headers/Animal.hpp diff --git a/d04/ex01/Brain.hpp b/d04/ex01/headers/Brain.hpp similarity index 100% rename from d04/ex01/Brain.hpp rename to d04/ex01/headers/Brain.hpp diff --git a/d04/ex01/Cat.hpp b/d04/ex01/headers/Cat.hpp similarity index 100% rename from d04/ex01/Cat.hpp rename to d04/ex01/headers/Cat.hpp diff --git a/d04/ex01/Dog.hpp b/d04/ex01/headers/Dog.hpp similarity index 100% rename from d04/ex01/Dog.hpp rename to d04/ex01/headers/Dog.hpp diff --git a/d04/ex01/color.h b/d04/ex01/headers/color.h similarity index 100% rename from d04/ex01/color.h rename to d04/ex01/headers/color.h diff --git a/d04/ex01/Animal.cpp b/d04/ex01/srcs/Animal.cpp similarity index 100% rename from d04/ex01/Animal.cpp rename to d04/ex01/srcs/Animal.cpp diff --git a/d04/ex01/Brain.cpp b/d04/ex01/srcs/Brain.cpp similarity index 100% rename from d04/ex01/Brain.cpp rename to d04/ex01/srcs/Brain.cpp diff --git a/d04/ex01/Cat.cpp b/d04/ex01/srcs/Cat.cpp similarity index 100% rename from d04/ex01/Cat.cpp rename to d04/ex01/srcs/Cat.cpp diff --git a/d04/ex01/Dog.cpp b/d04/ex01/srcs/Dog.cpp similarity index 100% rename from d04/ex01/Dog.cpp rename to d04/ex01/srcs/Dog.cpp diff --git a/d04/ex01/test.cpp b/d04/ex01/test.cpp deleted file mode 100644 index c7f9492..0000000 --- a/d04/ex01/test.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -class A { -public: - A & operator=( A const & rhs ) { - std::cout << "A assignation operator\n"; - _i = rhs._i; - return *this;} -private: - int _i; -}; - -class B { -public: - B() { - std::cout << "B default constructor\n"; - _a = new A();} - B( A *a ) { - std::cout << "B parameterized constructor\n"; - _a = new A(); - *_a = *a;} - - ~B() { - std::cout << "B destructor\n"; - delete _a;} -private: - A *_a; -}; - -int main() { - { - const B * b = new B(); - delete b; - } - std::cout << "\n"; - { - B * b; - A * a = new A(); - b = new B(a); - delete a; - delete b; - } - return 0; -} diff --git a/d04/ex02/Makefile b/d04/ex02/Makefile new file mode 100644 index 0000000..2265727 --- /dev/null +++ b/d04/ex02/Makefile @@ -0,0 +1,82 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = pure + +#TYPE = c +TYPE = cpp + +ifeq "$(TYPE)" "c" + CC = c + EXT = c +else ifeq "$(TYPE)" "cpp" + CC = c++ + EXT = cpp +endif + +CFLAGS = -Wall -Wextra -Werror $(INCLUDES) +ifeq "$(TYPE)" "cpp" + CFLAGS += -std=c++98 +endif + +VPATH = $(D_SRCS) + +LIBS = + +INCLUDES = -I$(D_HEADERS) + +D_SRCS = srcs +SRCS = main.cpp \ + Animal.cpp \ + Dog.cpp \ + Cat.cpp \ + Brain.cpp + +D_HEADERS = headers +HEADERS = Animal.hpp \ + Dog.hpp \ + Cat.hpp \ + Brain.hpp + +D_OBJS = builds +OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) + +ifeq "$(D_OBJS)" "." + RM_OBJS = rm -f $(OBJS) +else + RM_OBJS = rm -rf $(D_OBJS) +endif + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . target: prerequisites . $@ : target # +# RULES . recipe . $< : 1st prerequisite # +# . recipe . $^ : all prerequisites # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +all: $(NAME) + +$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) + $(CC) $(CFLAGS) -c $< -o $@ + +$(D_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(D_HEADERS)/%) + +$(NAME): $(OBJS) + $(CC) $(OBJS) -o $@ $(LIBS) + +clean: + $(RM_OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d04/ex02/headers/Animal.hpp b/d04/ex02/headers/Animal.hpp new file mode 100644 index 0000000..d055205 --- /dev/null +++ b/d04/ex02/headers/Animal.hpp @@ -0,0 +1,27 @@ +#ifndef ANIMAL_HPP +# define ANIMAL_HPP + +# include "color.h" +#include +#include + +class Animal { + +public: + + Animal(); + Animal( Animal const & src ); + virtual ~Animal( void ); + Animal & operator=( Animal const & rhs ); + + virtual void makeSound() const = 0; + std::string getType() const; + +protected: + + std::string type; + +}; + +#endif + diff --git a/d04/ex02/headers/Brain.hpp b/d04/ex02/headers/Brain.hpp new file mode 100644 index 0000000..e7b699d --- /dev/null +++ b/d04/ex02/headers/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/ex02/headers/Cat.hpp b/d04/ex02/headers/Cat.hpp new file mode 100644 index 0000000..ba516e2 --- /dev/null +++ b/d04/ex02/headers/Cat.hpp @@ -0,0 +1,34 @@ +#ifndef CAT_HPP +# define CAT_HPP + +# include "color.h" +# include +# include + +# include "Animal.hpp" +# include "Brain.hpp" + +class Cat : public Animal { + +public: + + Cat(); + Cat( Brain *brain ); +// Cat( Brain *brain = new Brain() ); + Cat( Cat const & src ); + ~Cat(); + Cat & operator=( Cat const & rhs ); + + void makeSound() const; + + void printBrain() const; + void printBrain(int pos) const; + +private: + + Brain *_brain; + +}; + +#endif + diff --git a/d04/ex02/headers/Dog.hpp b/d04/ex02/headers/Dog.hpp new file mode 100644 index 0000000..17617cd --- /dev/null +++ b/d04/ex02/headers/Dog.hpp @@ -0,0 +1,30 @@ +#ifndef DOG_HPP +# define DOG_HPP + +# include "color.h" +# include +# include + +# include "Animal.hpp" +# include "Brain.hpp" + +class Dog : public Animal { + +public: + + Dog(); + Dog( Brain *brain ); + Dog( Dog const & src ); + ~Dog(); + Dog & operator=( Dog const & rhs ); + + void makeSound() const; + +private: + + Brain *_brain; + +}; + +#endif + diff --git a/d04/ex02/headers/color.h b/d04/ex02/headers/color.h new file mode 100644 index 0000000..e313f5f --- /dev/null +++ b/d04/ex02/headers/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/ex02/main.cpp b/d04/ex02/main.cpp new file mode 100644 index 0000000..5166e80 --- /dev/null +++ b/d04/ex02/main.cpp @@ -0,0 +1,123 @@ +#include "Animal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" + +#include +#include + +#include "color.h" +#define N_TEST "7" + +int main() { + std::cout << B_YELLOW "\n[1/" N_TEST "] test subject :" RESET "\n"; + { + const Animal* j = new Dog(); + const Animal* i = new Cat(); + delete j;//should not create a leak + delete i; + } + + std::cout << B_YELLOW "\n[2/" N_TEST "] test with brain :" RESET "\n"; + { + 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(); + + std::cout << B_BLUE "delete dog :" RESET "\n"; + delete dog; + std::cout << B_BLUE "delete cat1 :" RESET "\n"; + delete cat1; + std::cout << B_BLUE "delete brain1 :" RESET "\n"; + delete brain1; + std::cout << B_BLUE "delete brain2 :" RESET "\n"; + delete brain2; + } + + std::cout << B_YELLOW "\n[3/" N_TEST "] array animal test :" RESET "\n"; + { + 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 "\n[4/" N_TEST "] copy constructor test1/2 :" 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 "\n[5/" N_TEST "] copy constructor test2/2 :" 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 "\n[6/" N_TEST "] assignation 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 "\n[7/" N_TEST "] assignation 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; +} + diff --git a/d04/ex02/srcs/Animal.cpp b/d04/ex02/srcs/Animal.cpp new file mode 100644 index 0000000..e773c87 --- /dev/null +++ b/d04/ex02/srcs/Animal.cpp @@ -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;} + diff --git a/d04/ex02/srcs/Brain.cpp b/d04/ex02/srcs/Brain.cpp new file mode 100644 index 0000000..bd3543e --- /dev/null +++ b/d04/ex02/srcs/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/ex02/srcs/Cat.cpp b/d04/ex02/srcs/Cat.cpp new file mode 100644 index 0000000..b9a3ace --- /dev/null +++ b/d04/ex02/srcs/Cat.cpp @@ -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); +} diff --git a/d04/ex02/srcs/Dog.cpp b/d04/ex02/srcs/Dog.cpp new file mode 100644 index 0000000..f6f1cdb --- /dev/null +++ b/d04/ex02/srcs/Dog.cpp @@ -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"; +} + diff --git a/d04/ex00/fr.subject.pdf b/d04/fr.subject.pdf similarity index 100% rename from d04/ex00/fr.subject.pdf rename to d04/fr.subject.pdf