Extend main.cpp, make Animal destr virtual in ex00
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 07:28:53 +0000 (09:28 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 07:33:13 +0000 (09:33 +0200)
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
ex00/main.cpp

index 21150751fb69833e8c0275da50d6bff29856e90f..d5cba60bcafd7da3a8a96ff950075eafc41c6718 100644 (file)
@@ -11,7 +11,7 @@ class Animal
        public:
                Animal(std::string type = "BASE");
                Animal(const Animal &other);
-               ~Animal();
+               virtual ~Animal();
 
                Animal  &operator=(const Animal &other);
 
index 856c8a1fc9b98d147af90f7d054217d58ab58e3a..3a3efa25598aaa2d824c192034aa38c65ee4c337 100644 (file)
@@ -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);
 }