CPP04 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Brain.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Brain.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/05/20 23:17:10 by kamitsui #+# #+# */
9/* Updated: 2025/05/28 23:56:43 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
22#include "Brain.hpp"
23
30 std::cout << "Brain default constructor called" << std::endl;
31 for (int i = 0; i < 100; ++i) {
32 this->ideas[i] = "Thinking...";
33 }
34}
35
42Brain::Brain(const Brain &other) {
43 std::cout << "Brain copy constructor called" << std::endl;
44 for (int i = 0; i < 100; ++i) {
45 this->ideas[i] = other.ideas[i];
46 }
47}
48
57Brain &Brain::operator=(const Brain &other) {
58 std::cout << "Brain copy assignment operator called" << std::endl;
59 if (this != &other) {
60 for (int i = 0; i < 100; ++i) {
61 this->ideas[i] = other.ideas[i];
62 }
63 }
64 return *this;
65}
66
72Brain::~Brain() { std::cout << "Brain destructor called" << std::endl; }
73
80void Brain::setIdea(int index, const std::string &idea) {
81 if (index >= 0 && index < 100) {
82 this->ideas[index] = idea;
83 }
84}
85
92const std::string &Brain::getIdea(int index) const {
93 if (index >= 0 && index < 100) {
94 return this->ideas[index];
95 }
96 static const std::string emptyString = "";
97 return emptyString;
98}
Represents the brain of an animal, holding a collection of ideas.
Definition Brain.hpp:37
const std::string & getIdea(int index) const
Gets an idea from a specific index in the brain. Performs bounds checking. If the index is out of bou...
Definition Brain.cpp:92
Brain & operator=(const Brain &other)
Copy assignment operator for Brain. Performs a deep copy of all ideas from another Brain object....
Definition Brain.cpp:57
Brain()
Default constructor for Brain. Initializes all 100 ideas to "Thinking...". Displays a construction me...
Definition Brain.cpp:29
void setIdea(int index, const std::string &idea)
Sets an idea at a specific index in the brain. Performs bounds checking to ensure the index is valid ...
Definition Brain.cpp:80
~Brain()
Destructor for Brain. Displays a destruction message. No dynamic memory to free directly within Brain...
Definition Brain.cpp:72
T endl(T... args)
Declares the Brain class.