main.cpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* main.cpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/08/15 17:12:29 by gychoi #+# #+# */ 9 /* Updated: 2023/08/18 18:16:11 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "Animal.hpp" 14 #include "WrongAnimal.hpp" 15 #include "Dog.hpp" 16 #include "Cat.hpp" 17 #include "WrongCat.hpp" 18 #include <iostream> 19 20 #ifdef LEAKS_CHECK 21 # include <cstdlib> 22 void check_leaks(void) 23 { 24 system("leaks a.out"); 25 } 26 # define ATEXIT_CHECK() atexit(check_leaks) 27 #else 28 # define ATEXIT_CHECK() 29 #endif 30 31 void makeTestName(std::string const& str) 32 { 33 std::cout << "\n--------------------" << std::endl; 34 std::cout << str << std::endl; 35 std::cout << "--------------------\n" << std::endl; 36 } 37 38 void subjectTest(void) 39 { 40 const Animal* meta = new Animal(); 41 const Animal* j = new Dog(); 42 const Animal* i = new Cat(); 43 44 std::cout << j->getType() << " " << std::endl; 45 std::cout << i->getType() << " " << std::endl; 46 i->makeSound(); 47 j->makeSound(); 48 meta->makeSound(); 49 50 delete meta; 51 delete j; 52 delete i; 53 } 54 55 void testOne(void) 56 { 57 WrongAnimal const* cat = new WrongCat("grumpy"); 58 WrongAnimal const* meta = new WrongAnimal(); 59 60 std::cout << cat->getType() << std::endl; 61 std::cout << meta->getType() << std::endl; 62 cat->makeSound(); 63 meta->makeSound(); 64 65 delete cat; 66 delete meta; 67 } 68 69 void testTwo(void) 70 { 71 Cat const* cat = new Cat("kitty"); 72 WrongAnimal const* wrongAnimal = dynamic_cast<WrongAnimal const*>(cat); 73 74 if (!wrongAnimal) 75 std::cout << "[INFO] : Cannot convert Cat to WrongAnimal" << std::endl; 76 else 77 std::cout << "[INFO] : [OK]" << std::endl; 78 delete cat; 79 } 80 81 void testThree(void) 82 { 83 Animal* animal = new Dog("narong"); 84 Dog* dog = static_cast<Dog*>(animal); 85 86 std::cout << animal->getType() << std::endl; 87 std::cout << dog->getType() << ", " << dog->getName() << std::endl; 88 delete dog; 89 } 90 91 void testFour(void) 92 { 93 Dog* dog = new Dog("ppoppi"); 94 WrongCat* wrongCat = reinterpret_cast<WrongCat*>(dog); 95 96 std::cout << wrongCat->getType() << ", " << wrongCat->getName() << std::endl; 97 wrongCat->makeSound(); 98 delete dog; 99 } 100 101 void testFive(void) 102 { 103 Cat catOne("toto"); 104 Cat catTwo(catOne); 105 106 std::cout << catOne.getType() << ", " << catOne.getName() << std::endl; 107 std::cout << catTwo.getType() << ", " << catTwo.getName() << std::endl; 108 109 Dog dogOne("ddochi"); 110 Dog dogTwo("rani"); 111 112 dogOne = dogTwo; 113 std::cout << dogOne.getType() << ", " << dogOne.getName() << std::endl; 114 std::cout << dogTwo.getType() << ", " << dogTwo.getName() << std::endl; 115 } 116 117 void testSix(void) 118 { 119 Animal* animalOne = new Cat("Kim Deok bae"); 120 Cat catOne(*static_cast<Cat*>(animalOne)); 121 122 std::cout << animalOne->getType() << std::endl; 123 std::cout << catOne.getType() << ", " << catOne.getName() << std::endl; 124 125 Animal* animalTwo = new Animal(); 126 Cat* catTwo = static_cast<Cat*>(animalTwo); 127 Cat catThree(*catTwo); 128 129 std::cout << animalTwo->getType() << std::endl; 130 // std::cout << catThree.getType() << ", " << catThree.getName() << std::endl; 131 std::cout << catThree.getType() << std::endl; 132 133 Animal* animalThree = new Animal(); 134 Cat catFour("nyancat"); 135 136 catFour = *static_cast<Cat*>(animalThree); 137 138 std::cout << animalThree->getType() << std::endl; 139 // std::cout << catFour.getType() << catFour.getName() << std::endl; 140 std::cout << catFour.getType() << std::endl; 141 142 delete animalOne; 143 delete animalTwo; 144 delete animalThree; 145 } 146 147 int main(void) 148 { 149 ATEXIT_CHECK(); 150 makeTestName("Test : subject Test"); 151 subjectTest(); 152 makeTestName("Test 1 : Wrong Animals"); 153 testOne(); 154 makeTestName("Test 2 : Dynamic casting with irrelevant classes"); 155 testTwo(); 156 makeTestName("Test 3 : Up-casting & Down-casting"); 157 testThree(); 158 makeTestName("Test 4 : Getting weird with reinterpret casting"); 159 testFour(); 160 makeTestName("Test 5 : Copy constructor"); 161 testFive(); 162 makeTestName("Test 6 : Getting weird with copy constructor"); 163 testSix(); 164 return 0; 165 }