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/17 21:58:36 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "Animal.hpp"
 14  #include "Cat.hpp"
 15  #include "Dog.hpp"
 16  #include "WrongAnimal.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  /*
 39   * main.cpp:40:27: error: allocating an object of abstract class type 'Animal'
 40   *       const Animal*   meta = new Animal();
 41   *                                ^
 42   * ./Animal.hpp:31:16: note: unimplemented pure virtual method 'makeSound' in 'Animal'
 43   *              virtual void    makeSound(void) const = 0;
 44   *                             ^
 45   * 1 error generated.
 46   * make: *** [main.o] Error 1
 47   */
 48  void	subjectTest(void)
 49  {
 50  //	const Animal*	meta = new Animal();
 51  	const Animal*	j = new Dog();
 52  	const Animal*	i = new Cat();
 53  
 54  	std::cout << j->getType() << " " << std::endl;
 55  	std::cout << i->getType() << " " << std::endl;
 56  	j->makeSound();
 57  	i->makeSound();
 58  //	meta->makeSound();
 59  
 60  //	delete meta;
 61  	delete j;
 62  	delete i;
 63  }
 64  
 65  void	testOne(void)
 66  {
 67  	WrongAnimal const*	cat = new WrongCat("grumpy");
 68  	WrongAnimal const*	meta = new WrongAnimal();
 69  
 70  	std::cout << cat->getType() << std::endl;
 71  	std::cout << meta->getType() << std::endl;
 72  	cat->makeSound();
 73  	meta->makeSound();
 74  
 75  	delete cat;
 76  	delete meta;
 77  }
 78  
 79  void	testTwo(void)
 80  {
 81  	Animal*	animal = new Dog("narong");
 82  	Dog*	dog = static_cast<Dog*>(animal);
 83  
 84  	std::cout << animal->getType() << std::endl;
 85  	animal->makeSound();
 86  	std::cout << dog->getType() << ", " << dog->getName() << std::endl;
 87  	dog->makeSound();
 88  
 89  //	Animal	animalTest = dynamic_cast<Animal>(dog);
 90  	delete animal;
 91  }
 92  
 93  int	main(void)
 94  {
 95  	ATEXIT_CHECK();
 96  	makeTestName("Test : Subject Test");
 97  	subjectTest();
 98  	makeTestName("Test 1 : Wrong Animals");
 99  	testOne();
100  	makeTestName("Test 2 : Casting");
101  	testTwo();
102  	return 0;
103  }