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
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);
}
{
std::cout << "Meow :3\n";
}
+
+void Cat::addIdea(std::string idea)
+{
+ m_brain->addIdea(idea);
+}
+
+void Cat::listIdeas() const
+{
+ m_brain->listIdeas();
+}
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
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);
}
{
std::cout << "Bark!\n";
}
+
+void Dog::addIdea(std::string idea)
+{
+ m_brain->addIdea(idea);
+}
+
+void Dog::listIdeas() const
+{
+ m_brain->listIdeas();
+}
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
#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);
}