반응형
들어가기 전 밑 블로그에서 가상 함수를 공부하고 가자 ~~
https://wo-dbs.tistory.com/152
- Animal을 먼저 만들자.
- virtual을 통해 소멸자와, makeSound 구현
Animal.hpp
#ifndef ANIMAL_HPP
# define ANIMAL_HPP
#include <iostream>
class Animal {
protected:
std::string type;
public:
Animal(void);
Animal(const Animal &obj);
Animal& operator=(const Animal& obj);
virtual ~Animal(void);
virtual void makeSound(void) const;
std::string getType(void) const;
};
# endif
Animal.cpp
#include "Animal.hpp"
Animal::Animal(void)
{
this->type = "animal";
std::cout << "Animal Constructor called\\n";
}
Animal::Animal(Animal const &animal)
{
*this = animal;
std::cout << "Animal Constructor copy called\\n";
}
Animal::~Animal()
{
std::cout << "Animal Destructor called\\n";
}
Animal& Animal::operator=(Animal const &animal)
{
if (this == &animal)
return *this;
this->type = animal.getType();
std::cout << "Animal operator = " << type << " called\\n";
return (*this);
}
std::string Animal::getType(void) const
{
return (this->type);
}
void Animal::makeSound(void) const
{
std::cout << "Animal class can't make sound\\n";
}
- Dog. Cat 만들기
모든 클래스는 멤버 함수로 makeSound()를 가짐. 각 클래스에 따라 적절하게 출력하도록 만들면 된다, Animal 클래스는 아무것도 출력하면 안됨.
Cat.hpp
#ifndef CAT_HPP
# define CAT_HPP
#include "Animal.hpp"
class Cat : public Animal
{
public:
Cat(void);
Cat(const Cat &cat);
Cat& operator=(const Cat &cat);
~Cat(void);
void makeSound(void) const;
};
# endif
Cat.cpp
#include "Cat.hpp"
Cat::Cat(void)
{
this->type = "Cat";
std::cout << "Cat Constructor called\\n";
}
Cat::Cat(Cat const &cat)
{
*this = cat;
std::cout << "Cat Constructor copy called\\n";
}
Cat::~Cat()
{
std::cout << "Cat Destructor called\\n";
}
Cat& Cat::operator=(Cat const &cat)
{
if (this == &cat)
return *this;
this->type = cat.getType();
std::cout << "Cat operator = " << type << " called\\n";
return (*this);
}
void Cat::makeSound(void) const
{
std::cout << "nyang\\n";
}
Dog.cpp
#include "Dog.hpp"
Dog::Dog(void)
{
this->type = "Dog";
std::cout << "Dog Constructor called\\n";
}
Dog::Dog(Dog const &dog)
{
*this = dog;
std::cout << "Dog Constructor copy called\\n";
}
Dog::~Dog()
{
std::cout << "Dog Destructor called\\n";
}
Dog& Dog::operator=(Dog const &dog)
{
if (this == &dog)
return *this;
this->type = dog.getType();
std::cout << "Dog operator = " << type << " called\\n";
return (*this);
}
void Dog::makeSound(void) const
{
std::cout << "meong\\n";
}
Dog.hpp
#ifndef DOG_HPP
# define DOG_HPP
#include "Animal.hpp"
class Dog : public Animal
{
public:
Dog(void);
Dog(const Dog &dog);
Dog& operator=(const Dog &dog);
~Dog(void);
void makeSound(void) const;
};
# endif
- WrongAnimal.hpp, WrongAnimal.cpp, WrongCat.cpp, WrongCat.hpp
WrongAnimal.hpp
#ifndef WRONGANIMAL_HPP
# define WRONGANIMAL_HPP
#include <iostream>
class WrongAnimal
{
protected:
std::string type;
public:
WrongAnimal(void);
WrongAnimal(const WrongAnimal &wrongAnimal);
WrongAnimal& operator=(const WrongAnimal &wrongAnimal);
virtual ~WrongAnimal(void);
std::string getType(void) const;
void makeSound(void) const;
};
# endif
WrongAnimal.cpp
#include "WrongAnimal.hpp"
WrongAnimal::WrongAnimal(void)
{
this->type = "WrongAnimal";
std::cout << "WrongAnimal Constructor called\\n";
}
WrongAnimal::WrongAnimal(WrongAnimal const &wrongAnimal)
{
*this = wrongAnimal;
std::cout << "WrongAnimal Constructor copy called\\n";
}
WrongAnimal::~WrongAnimal()
{
std::cout << "WrongAnimal Destructor called\\n";
}
WrongAnimal& WrongAnimal::operator=(WrongAnimal const &wrongAnimal)
{
if (this == &wrongAnimal)
return *this;
this->type = wrongAnimal.getType();
std::cout << "WrongAnimal operator = " << type << " called\\n";
return (*this);
}
std::string WrongAnimal::getType(void) const
{
return (this->type);
}
void WrongAnimal::makeSound(void) const
{
std::cout << "WrongAnimal class can't make sound\\n";
}
WrongCat.hpp
#ifndef WRONGCAT_HPP
# define WRONGCAT_HPP
#include "WrongAnimal.hpp"
class WrongCat : public WrongAnimal
{
public:
WrongCat(void);
WrongCat(const WrongCat &wrongCat);
WrongCat& operator=(const WrongCat &wrongCat);
~WrongCat(void);
void makeSound(void) const;
};
# endif
WrongCat.cpp
#include "WrongCat.hpp"
WrongCat::WrongCat(void)
{
this->type = "WrongCat";
std::cout << "WrongCat Constructor called\\n";
}
WrongCat::WrongCat(WrongCat const &wrongCat)
{
*this = wrongCat;
std::cout << "WrongCat Constructor copy called\\n";
}
WrongCat::~WrongCat()
{
std::cout << "WrongCat Destructor called\\n";
}
WrongCat& WrongCat::operator=(WrongCat const &wrongCat)
{
if (this == &wrongCat)
return *this;
this->type = wrongCat.getType();
std::cout << "WrongCat operator = " << type << " called\\n";
return (*this);
}
void WrongCat::makeSound(void) const
{
std::cout << "WrongCat class can't make sound\\n";
}
반응형
'42Seoul > CPP Module 04' 카테고리의 다른 글
ex02 (0) | 2024.01.25 |
---|---|
ex01(깊은 복사) (0) | 2024.01.24 |