From b87d7eca31a91af65235d364f3562fd611924de8 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 25 Oct 2024 09:28:53 +0200 Subject: [PATCH] Extend main.cpp, make Animal destr virtual in ex00 Extend main.cpp by the Wrong- classes and add delete to call the destructors. For the delete to work properly, the Animal destructor is changed to a virtual one. --- ex00/Animal.h | 2 +- ex00/main.cpp | 35 +++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ex00/Animal.h b/ex00/Animal.h index 2115075..d5cba60 100644 --- a/ex00/Animal.h +++ b/ex00/Animal.h @@ -11,7 +11,7 @@ class Animal public: Animal(std::string type = "BASE"); Animal(const Animal &other); - ~Animal(); + virtual ~Animal(); Animal &operator=(const Animal &other); diff --git a/ex00/main.cpp b/ex00/main.cpp index 856c8a1..3a3efa2 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -7,20 +7,27 @@ int main() { - const Animal* meta = new Animal(); - const Animal* j = new Dog(); - const Animal* i = new Cat(); + 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(); - const WrongAnimal* meta2 = new WrongAnimal(); - const WrongAnimal* i2 = new WrongCat(); - - std::cout << j->getType() << " " << std::endl; - std::cout << i->getType() << " " << std::endl; - i->makeSound(); - j->makeSound(); - meta->makeSound(); - std::cout << i2->getType() << " " << std::endl; - i2->makeSound(); - meta2->makeSound(); + 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; return (0); } -- 2.30.2