From 2ae3b41ffa0a7f3db49eb1af8e46307be5f72653 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 25 Oct 2024 12:31:55 +0200 Subject: [PATCH] Use Brain in Dog and Cat classes, use it in main --- ex01/Animal.h | 4 +++- ex01/Cat.cpp | 17 +++++++++++++++-- ex01/Cat.h | 4 +++- ex01/Dog.cpp | 17 +++++++++++++++-- ex01/Dog.h | 4 +++- ex01/main.cpp | 51 ++++++++++++++++++++++++++++++--------------------- 6 files changed, 69 insertions(+), 28 deletions(-) diff --git a/ex01/Animal.h b/ex01/Animal.h index d5cba60..83361a2 100644 --- a/ex01/Animal.h +++ b/ex01/Animal.h @@ -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 diff --git a/ex01/Cat.cpp b/ex01/Cat.cpp index 88e833f..a555a34 100644 --- a/ex01/Cat.cpp +++ b/ex01/Cat.cpp @@ -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(&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(); +} diff --git a/ex01/Cat.h b/ex01/Cat.h index d3a2859..a5d1eae 100644 --- a/ex01/Cat.h +++ b/ex01/Cat.h @@ -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 diff --git a/ex01/Dog.cpp b/ex01/Dog.cpp index c9720fb..ef795e1 100644 --- a/ex01/Dog.cpp +++ b/ex01/Dog.cpp @@ -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(&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(); +} diff --git a/ex01/Dog.h b/ex01/Dog.h index 31d02f2..b9854d8 100644 --- a/ex01/Dog.h +++ b/ex01/Dog.h @@ -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 diff --git a/ex01/main.cpp b/ex01/main.cpp index 3a3efa2..842f76a 100644 --- a/ex01/main.cpp +++ b/ex01/main.cpp @@ -5,29 +5,38 @@ #include "WrongCat.h" #include +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); } -- 2.30.2