반응형
들어가기 전 깊은 복사를 공부하고 가자
https://wo-dbs.tistory.com/153
ex00에서 파일 그대로 복사하고 추가해야될 파일은 Brain.cpp Brain.hpp, 수정해야될 파일은 Dog.cpp, Cat.cpp
- Brain.hpp Brain.cpp 만들기
#ifndef BRAIN_HPP
# define BRAIN_HPP
#include "iostream"
class Brain {
private:
std::string ideas[100];
public:
Brain();
Brain(std::string idea);
Brain(Brain const &brain);
~Brain();
Brain &operator=(Brain const &brain);
std::string getIdea(int index) const;
void setIdea(int index, std::string str);
};
#endif
#include "Brain.hpp"
Brain::Brain(void) {
std::cout << "Brain Constructor called\\n";
for (int i = 0; i < 100; i++)
{
this->ideas[i] = "nothing";
}
}
Brain::Brain(std::string idea)
{
std::cout << "Brain Constructor called\\n";
for (int i = 0; i < 100; i++)
{
this->ideas[i] = idea;
}
}
Brain::Brain(Brain const &brain) {
*this = brain;
}
Brain::~Brain(void) {
std::cout << "Brain Destructor called\\n";
}
Brain& Brain::operator=(Brain const &brain) {
if (this == &brain)
return *this;
for (int i = 0; i < 100; i++) {
this->ideas[i] = brain.getIdea(i);
}
return (*this);
}
std::string Brain::getIdea(int index) const
{
return this->ideas[index];
}
void Brain::setIdea(int index, std::string str)
{
ideas[index] = str;
}
- Dog.cpp, Cat.cpp 수정
Cat
#include "Cat.hpp"
Cat::Cat(void)
{
this->type = "Cat";
this->brain = new Brain();
std::cout << "Cat Constructor called\\n";
}
Cat::Cat(Cat const &cat)
{
this->brain = new Brain();
*this = cat;
std::cout << "Cat Constructor copy called\\n";
}
Cat::~Cat()
{
delete brain;
std::cout << "Cat Destructor called\\n";
}
Cat& Cat::operator=(Cat const &cat)
{
if (this == &cat)
return *this;
this->type = cat.getType();
if (this->brain)
{
delete this->brain;
}
this->brain = new Brain(*(cat.brain));
std::cout << "Cat operator = " << type << " called\\n";
return (*this);
}
std::string Cat::getCatIdea(int index)
{
if (index >= 100)
{
std::cout << "99 out of range\\n";
return "";
}
return brain->getIdea(index);
}
void Cat::setBrain(int index, std::string str)
{
if (index >= 100)
{
std::cout << "99 out of range\\n";
return ;
}
brain->setIdea(index, str);
}
void Cat::makeSound(void) const
{
std::cout << "nyang\\n";
}
Dog
#include "Dog.hpp"
#include "Brain.hpp"
Dog::Dog(void)
{
this->type = "Dog";
this->brain = new Brain();
std::cout << "Dog Constructor called\\n";
}
Dog::Dog(Dog const &dog)
{
this->brain = new Brain();
*this = dog;
std::cout << "Dog Constructor copy called\\n";
}
Dog::~Dog()
{
delete brain;
std::cout << "Dog Destructor called\\n";
}
Dog& Dog::operator=(Dog const &dog)
{
if (this == &dog)
return *this;
this->type = dog.getType();
if (this->brain)
{
delete this->brain;
}
this->brain = new Brain(*(dog.brain));
std::cout << "Dog operator = " << type << " called\\n";
return (*this);
}
std::string Dog::getDogIdea(int index)
{
if (index >= 100)
{
std::cout << "99 out of range\\n";
return "";
}
return brain->getIdea(index);
}
void Dog::setBrain(int index, std::string str)
{
if (index >= 100)
{
std::cout << "99 out of range\\n";
return ;
}
brain->setIdea(index, str);
}
void Dog::makeSound(void) const
{
std::cout << "meong\\n";
}
반응형
'42Seoul > CPP Module 04' 카테고리의 다른 글
ex02 (0) | 2024.01.25 |
---|---|
ex00(가상 함수) (0) | 2024.01.24 |