From: Lukas Jiriste Date: Fri, 25 Oct 2024 08:01:50 +0000 (+0200) Subject: Add Brain class and use it in Cat and Dog X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=e2fa7dc5780ab8ff6e331a2a8aba4548b2a113bd;p=42%2FCPP_Module_04 Add Brain class and use it in Cat and Dog This is an exercise in ownership handling, so Dog and Cat contain a pointer to Brain that they have to correctly manage. --- diff --git a/ex01/Brain.cpp b/ex01/Brain.cpp new file mode 100644 index 0000000..ec96903 --- /dev/null +++ b/ex01/Brain.cpp @@ -0,0 +1,28 @@ +#include "Brain.h" +#include +#include + +Brain::Brain() +{ + std::cout << "Brain default cstr\n"; +} + +Brain::Brain(const Brain &other) +{ + std::cout << "Brain copy cstr\n"; + *this = other; +} + +Brain::~Brain() +{ + std::cout << "Brain destr\n"; +} + +Brain &Brain::operator=(const Brain &other) +{ + if (this == &other) + return (*this); + for (size_t i(0); i < CAPACITY; ++i) + ideas[i] = other.ideas[i]; + return (*this); +} diff --git a/ex01/Brain.h b/ex01/Brain.h new file mode 100644 index 0000000..94a09c3 --- /dev/null +++ b/ex01/Brain.h @@ -0,0 +1,21 @@ +#ifndef BRAIN_H +# define BRAIN_H + +# include + +class Brain +{ + private: + static const size_t CAPACITY = 100; + + std::string ideas[CAPACITY]; + + public: + Brain(); + Brain(const Brain &other); + ~Brain(); + + Brain &operator=(const Brain &other); +}; + +#endif // BRAIN_H diff --git a/ex01/Cat.cpp b/ex01/Cat.cpp index def9eb8..88e833f 100644 --- a/ex01/Cat.cpp +++ b/ex01/Cat.cpp @@ -3,12 +3,14 @@ Cat::Cat() : Animal("Cat") + , m_brain(new Brain) { std::cout << "Cat default cstr\n"; } Cat::Cat(const Cat &other) : Animal("Cat") + , m_brain(new Brain) { std::cout << "Cat copy cstr\n"; *this = other; @@ -17,12 +19,14 @@ Cat::Cat(const Cat &other) Cat::~Cat() { std::cout << "Cat destr\n"; + delete m_brain; } Cat &Cat::operator=(const Cat &other) { if (this == &other) return (*this); + *m_brain = *other.m_brain; return (*this); } diff --git a/ex01/Cat.h b/ex01/Cat.h index 77a3c09..d3a2859 100644 --- a/ex01/Cat.h +++ b/ex01/Cat.h @@ -2,10 +2,12 @@ # define CAT_H # include "Animal.h" +# include "Brain.h" class Cat : public Animal { private: + Brain *m_brain; public: Cat(); diff --git a/ex01/Dog.cpp b/ex01/Dog.cpp index c81191d..c9720fb 100644 --- a/ex01/Dog.cpp +++ b/ex01/Dog.cpp @@ -3,12 +3,14 @@ Dog::Dog() : Animal("Dog") + , m_brain(new Brain) { std::cout << "Dog default cstr\n"; } Dog::Dog(const Dog &other) : Animal("Dog") + , m_brain(new Brain) { std::cout << "Dog copy cstr\n"; *this = other; @@ -17,12 +19,14 @@ Dog::Dog(const Dog &other) Dog::~Dog() { std::cout << "Dog destr\n"; + delete m_brain; } Dog &Dog::operator=(const Dog &other) { if (this == &other) return (*this); + *m_brain = *other.m_brain; return (*this); } diff --git a/ex01/Dog.h b/ex01/Dog.h index db3631b..31d02f2 100644 --- a/ex01/Dog.h +++ b/ex01/Dog.h @@ -2,10 +2,12 @@ # define DOG_H # include "Animal.h" +# include "Brain.h" class Dog : public Animal { private: + Brain *m_brain; public: Dog(); diff --git a/ex01/Makefile b/ex01/Makefile index 8e04f72..7a50fdc 100644 --- a/ex01/Makefile +++ b/ex01/Makefile @@ -21,6 +21,7 @@ SOURCES := main.cpp \ Dog.cpp \ WrongAnimal.cpp \ WrongCat.cpp \ + Brain.cpp \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))