Add Brain class and use it in Cat and Dog
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 08:01:50 +0000 (10:01 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 08:01:50 +0000 (10:01 +0200)
This is an exercise in ownership handling, so Dog and Cat contain a
pointer to Brain that they have to correctly manage.

ex01/Brain.cpp [new file with mode: 0644]
ex01/Brain.h [new file with mode: 0644]
ex01/Cat.cpp
ex01/Cat.h
ex01/Dog.cpp
ex01/Dog.h
ex01/Makefile

diff --git a/ex01/Brain.cpp b/ex01/Brain.cpp
new file mode 100644 (file)
index 0000000..ec96903
--- /dev/null
@@ -0,0 +1,28 @@
+#include "Brain.h"
+#include <string>
+#include <iostream>
+
+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 (file)
index 0000000..94a09c3
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef BRAIN_H
+# define BRAIN_H
+
+# include <string>
+
+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
index def9eb80591f9f75148d72ff808b03053849a81b..88e833fd9f50958376ea290ac33e652e66e311f8 100644 (file)
@@ -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);
 }
 
index 77a3c09906a86d8a2cdd19c45e02a783df3141de..d3a285938132a690ff5262df758ee0b09b2834dd 100644 (file)
@@ -2,10 +2,12 @@
 # define CAT_H
 
 # include "Animal.h"
+# include "Brain.h"
 
 class Cat : public Animal
 {
        private:
+               Brain   *m_brain;
 
        public:
                Cat();
index c81191d8c2ed2f7b50a5ff9c6a9e74d3a6ed75f8..c9720fb46ce5e3c0d5dc23cb1fee07e6adb07122 100644 (file)
@@ -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);
 }
 
index db3631baceefb3225c6d47def2d849ba24a4c151..31d02f20ca347f4567ac2c7a1013ed728e84e1cc 100644 (file)
@@ -2,10 +2,12 @@
 # define DOG_H
 
 # include "Animal.h"
+# include "Brain.h"
 
 class Dog : public Animal
 {
        private:
+               Brain   *m_brain;
 
        public:
                Dog();
index 8e04f7247a6e23c8fdac9cd4a5ae787f6651cd09..7a50fdcbc870ae9bf40a35e0046eaeaf6ac4ec9b 100644 (file)
@@ -21,6 +21,7 @@ SOURCES :=    main.cpp                \
                        Dog.cpp                 \
                        WrongAnimal.cpp \
                        WrongCat.cpp    \
+                       Brain.cpp               \
 
 SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))