Use Brain in Dog and Cat classes, use it in main
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 10:31:55 +0000 (12:31 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 10:31:55 +0000 (12:31 +0200)
ex01/Animal.h
ex01/Cat.cpp
ex01/Cat.h
ex01/Dog.cpp
ex01/Dog.h
ex01/main.cpp

index d5cba60bcafd7da3a8a96ff950075eafc41c6718..83361a2ebd39cdd34ec2711bd212599a9aab8765 100644 (file)
@@ -13,10 +13,12 @@ class Animal
                Animal(const Animal &other);
                virtual ~Animal();
 
-               Animal  &operator=(const Animal &other);
+               virtual Animal  &operator=(const Animal &other);
 
                virtual void    makeSound() const;
                std::string             getType() const;
+               virtual void    listIdeas() const = 0;
+               virtual void    addIdea(std::string idea) = 0;
 };
 
 #endif // ANIMAL_H
index 88e833fd9f50958376ea290ac33e652e66e311f8..a555a34b1b425b387b8ee46cb54b134444b5d627 100644 (file)
@@ -22,11 +22,14 @@ Cat::~Cat()
        delete m_brain;
 }
 
-Cat    &Cat::operator=(const Cat &other)
+Animal &Cat::operator=(const Animal &other)
 {
+       const Cat       *ptr(dynamic_cast<const Cat *>(&other));
+
        if (this == &other)
                return (*this);
-       *m_brain = *other.m_brain;
+       if (ptr)
+               *m_brain = *ptr->m_brain;
        return (*this);
 }
 
@@ -34,3 +37,13 @@ void Cat::makeSound() const
 {
        std::cout << "Meow :3\n";
 }
+
+void   Cat::addIdea(std::string idea)
+{
+       m_brain->addIdea(idea);
+}
+
+void   Cat::listIdeas() const
+{
+       m_brain->listIdeas();
+}
index d3a285938132a690ff5262df758ee0b09b2834dd..a5d1eaef1b22557687aed6aa41a1570f1a97eb1f 100644 (file)
@@ -14,9 +14,11 @@ class Cat : public Animal
                Cat(const Cat &other);
                ~Cat();
 
-               Cat     &operator=(const Cat &other);
+               Animal  &operator=(const Animal &other);
 
                void    makeSound() const;
+               void    listIdeas() const;
+               void    addIdea(std::string idea);
 };
 
 #endif // CAT_H
index c9720fb46ce5e3c0d5dc23cb1fee07e6adb07122..ef795e17a1359b4cdf0f87e3eb885a82a5c763d8 100644 (file)
@@ -22,11 +22,14 @@ Dog::~Dog()
        delete m_brain;
 }
 
-Dog    &Dog::operator=(const Dog &other)
+Animal &Dog::operator=(const Animal &other)
 {
+       const Dog       *ptr(dynamic_cast<const Dog *>(&other));
+
        if (this == &other)
                return (*this);
-       *m_brain = *other.m_brain;
+       if (ptr)
+               *m_brain = *ptr->m_brain;
        return (*this);
 }
 
@@ -34,3 +37,13 @@ void Dog::makeSound() const
 {
        std::cout << "Bark!\n";
 }
+
+void   Dog::addIdea(std::string idea)
+{
+       m_brain->addIdea(idea);
+}
+
+void   Dog::listIdeas() const
+{
+       m_brain->listIdeas();
+}
index 31d02f20ca347f4567ac2c7a1013ed728e84e1cc..b9854d88f038452a6512329ccb4dd6f9f909a3d8 100644 (file)
@@ -14,9 +14,11 @@ class Dog : public Animal
                Dog(const Dog &other);
                ~Dog();
 
-               Dog     &operator=(const Dog &other);
+               Animal  &operator=(const Animal &other);
 
                void    makeSound() const;
+               void    listIdeas() const;
+               void    addIdea(std::string idea);
 };
 
 #endif // DOG_H
index 3a3efa25598aaa2d824c192034aa38c65ee4c337..842f76acc865c501f98b337817f0f279ee4e5457 100644 (file)
@@ -5,29 +5,38 @@
 #include "WrongCat.h"
 #include <iostream>
 
+static const size_t    SIZE = 4;
+
 int main()
 {
-       const Animal* base = new Animal();
-       const Animal* cat = new Cat();
-       const Animal* dog = new Dog();
-       const WrongAnimal* wrong_base = new WrongAnimal();
-       const WrongAnimal* wrong_cat = new WrongCat();
+       Animal  *arr[SIZE];
+       Dog             dog;
+       Cat             cat;
 
-       std::cout << '\n' << base->getType() << ": ";
-       base->makeSound();
-       std::cout << cat->getType() << ": ";
-       cat->makeSound();
-       std::cout << dog->getType() << ": ";
-       dog->makeSound();
-       std::cout << '\n' << wrong_base->getType() << ": ";
-       wrong_base->makeSound();
-       std::cout << wrong_cat->getType() << ": ";
-       wrong_cat->makeSound();
-       std::cout << '\n';
-       delete base;
-       delete cat;
-       delete dog;
-       delete wrong_base;
-       delete wrong_cat;
+       dog.addIdea("Being constructed is fun.");
+       cat.addIdea("Being constructed is tiring.");
+       for (size_t i(0); i < SIZE / 2; ++i)
+       {
+               arr[i] = new Dog();
+               *arr[i] = dog;
+       }
+       for (size_t i(SIZE / 2); i < SIZE; ++i)
+       {
+               arr[i] = new Cat();
+               *arr[i] = cat;
+       }
+       arr[0]->addIdea("I'm the second dog alive. What a thrill.");
+       dog.addIdea("I'm not gonna be alone.");
+       arr[1]->addIdea("Wow, there are 2 other dogs. Hello.");
+       dog.addIdea("Being the prototype of so many is a great responsibility.");
+       arr[SIZE/2]->addIdea("Where is a high spot? I need to oversee the situation.");
+       arr[SIZE / 2 + 1]->addIdea("*YAWN* Why am I heWHAT IS THIS SHINY LITTLE THING?");
+       cat.addIdea("Clone me more, I am the ultimate being after all.");
+       dog.listIdeas();
+       cat.listIdeas();
+       for (size_t i(0); i < SIZE; ++i)
+               arr[i]->listIdeas();
+       for (size_t i(0); i < SIZE; ++i)
+               delete arr[i];
        return (0);
 }