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