Dog.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   Dog.cpp                                            :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/08/15 16:04:57 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/08/17 21:22:04 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "Dog.hpp"
 14  #include <iostream>
 15  
 16  /******************************
 17   * Constructor and Destructor *
 18   ******************************/
 19  
 20  Dog::Dog(void)
 21  {
 22  	std::cout << "[Dog] : Default constructor called" << std::endl;
 23  	Animal::type = "Dog";
 24  	this->brain = new Brain();
 25  }
 26  
 27  Dog::~Dog(void)
 28  {
 29  	std::cout << "[Dog] : Destructor called" << std::endl;
 30  	delete this->brain;
 31  }
 32  
 33  Dog::Dog(std::string name) : name(name)
 34  {
 35  	std::cout << "[Dog] : Parameterize constructor called" << std::endl;
 36  	Animal::type = "Dog";
 37  	this->brain = new Brain();
 38  }
 39  
 40  Dog::Dog(Dog const& target) : Animal(target)
 41  {
 42  	std::cout << "[Dog] : Copy constructor called" << std::endl;
 43  	if (this != &target) {
 44  		*this = target;
 45  		Animal::type = "Dog";
 46  	}
 47  }
 48  
 49  Dog&	Dog::operator=(Dog const& target)
 50  {
 51  	std::cout << "[Dog] : Copy assignment operator called" << std::endl;
 52  	if (this != &target) {
 53  		Animal::operator=(target);
 54  		if (target.type == "Dog") {
 55  			this->name = target.name;
 56  			this->memory = target.memory;
 57  			delete this->brain;
 58  			this->brain = new Brain(*target.brain);
 59  		}
 60  	}
 61  	return *this;
 62  }
 63  
 64  /******************************
 65   *      Getter  function      *
 66   ******************************/
 67  
 68  std::string	Dog::getType(void) const
 69  {
 70  	return this->type;
 71  }
 72  
 73  std::string	Dog::getName(void) const
 74  {
 75  	return this->name;
 76  }
 77  
 78  std::size_t	Dog::getMemory(void) const
 79  {
 80  	return this->memory;
 81  }
 82  
 83  /******************************
 84   *   Public Member function   *
 85   ******************************/
 86  
 87  void	Dog::makeSound(void) const
 88  {
 89  	std::cout << "[" << getType() << "] : Bark!" << std::endl;
 90  }
 91  
 92  void	Dog::thinking(std::size_t idx) const
 93  {
 94  	std::cout << "[Dog] : Thinking about '" << this->brain->getIdea(idx) << "'" << std::endl;
 95  }
 96  
 97  void	Dog::learning(std::string const& idea)
 98  {
 99  	std::cout << "[Dog] : Learning idea..." << std::endl;
100  	this->brain->setIdea(this->memory, idea);
101  	this->memory++;
102  	if (this->memory >= 100)
103  		this->memory = this->memory % 100;
104  }