반응형
ex01을 하기전
상속에 대한 공부를 해보자
https://wo-dbs.tistory.com/147
공부를 한 후
상속 오버라이딩
https://wo-dbs.tistory.com/148
상속으로 인한 메모리
https://wo-dbs.tistory.com/149
이 두 가지를 보고 한 번 코딩을 해보자
- ClapTrap을 상속 받는 ScavTrap을 만들어야함.
→ 문제에서 요구하는 거 그대로 코드로 만들자.
ScavTrap.cpp
#include "ScavTrap.hpp"
ScavTrap::ScavTrap()
{
this->name = "basic";
this->hitPoint = 100;
this->energyPoint = 50;
this->attackDamage = 20;
std::cout << "ScavTrap " << this->name << " constructor called\\n";
}
ScavTrap::ScavTrap(std::string name)
{
this->name = name;
this->hitPoint = 100;
this->energyPoint = 50;
this->attackDamage = 20;
std::cout << "ScavTrap " << this->name << " constructor called\\n";
}
ScavTrap::ScavTrap(ScavTrap const &scavTrap)
{
this->name = scavTrap.getName();
this->hitPoint = scavTrap.getHitPoint();
this->energyPoint = scavTrap.getEnergyPoint();
this->attackDamage = scavTrap.getAttackDamage();
std::cout << "ScavTrap " << this->name << " copy constructor called\\n";
}
ScavTrap::~ScavTrap()
{
std::cout << "ScavTrap " << this->name << " destructor called\\n";
}
ScavTrap& ScavTrap::operator=(const ScavTrap &ScavTrap)
{
if (this == &ScavTrap)
return *this;
this->name = ScavTrap.getName();
this->attackDamage = ScavTrap.getAttackDamage();
this->hitPoint = ScavTrap.getHitPoint();
this->energyPoint = ScavTrap.getEnergyPoint();
std::cout << "ScavTrap operator = " << name << " called" << std::endl;
return (*this);
}
void ScavTrap::attack(const std::string &target)
{
if (!this->hitPoint)
{
std::cout << "ScavTrap " << this->name << " has no hitPoint died" << std::endl;
}
else if (!this->energyPoint)
{
std::cout << "ScavTrap " << this->name << " has no Energy Points" << std::endl;
}
else
{
std::cout << "ScavTrap " << this->name << " attacks " << target;
std::cout << ", causing " << this->attackDamage << " points of damage!\\n";
this->energyPoint--;
}
}
void ScavTrap::guardGate()
{
if (!this->hitPoint)
{
std::cout << "ScavTrap " << this->name << " has no hitPoint died\\n" << std::endl;
}
else if (!this->energyPoint)
{
std::cout << "ScavTrap " << this->name << " has no Energy Points\\n" << std::endl;
}
else
{
std::cout << "ScavTrap " << this->name << " is now in Gate Keeper mode.\\n";
}
}
ScavTrap.hpp
#ifndef SCAVTRAP_HPP
# define SCAVTRAP_HPP
# include <iostream>
# include "ClapTrap.hpp"
class ScavTrap : public ClapTrap
{
public:
ScavTrap();
~ScavTrap();
ScavTrap(std::string name);
ScavTrap(const ScavTrap& scavTrap);
ScavTrap& operator=(const ScavTrap& obj);
void attack(const std::string& target);
void guardGate();
};
# endif
main.cpp
#include "ScavTrap.hpp"
int main(void)
{
ScavTrap a("A");
ClapTrap b("B");
ScavTrap c;
c = a;
a.attack("B");
b.takeDamage(0);
b.takeDamage(2);
b.beRepaired(2);
b.takeDamage(10);
b.takeDamage(50);
c.attack("B");
c.guardGate();
}
ClapTrap.hpp
#ifndef CLAPTRAP_HPP
# define CLAPTRAP_HPP
# include <iostream>
class ClapTrap{
protected:
std::string name;
unsigned int hitPoint;
unsigned int energyPoint;
unsigned int attackDamage;
public:
ClapTrap();
~ClapTrap();
ClapTrap(std::string name);
ClapTrap(const ClapTrap& ClapTrap);
ClapTrap& operator=(const ClapTrap& obj);
void attack(const std::string& target);
void takeDamage(unsigned int amount);
void beRepaired(unsigned int amount);
std::string getName() const;
unsigned int getHitPoint() const;
unsigned int getEnergyPoint() const;
unsigned int getAttackDamage() const;
};
# endif
ClapTrap.cpp
#include "ClapTrap.hpp"
ClapTrap::ClapTrap()
{
this->name = "basic";
this->hitPoint = 10;
this->energyPoint = 10;
this->attackDamage = 0;
std::cout << "ClapTrap " << this->name << " constructor called\\n";
}
ClapTrap::ClapTrap(std::string name)
{
this->name = name;
this->hitPoint = 10;
this->energyPoint = 10;
this->attackDamage = 0;
std::cout << "ClapTrap " << this->name << " constructor called\\n";
}
ClapTrap::ClapTrap(ClapTrap const &obj)
{
this->name = obj.getName();
this->hitPoint = obj.getHitPoint();
this->energyPoint = obj.getEnergyPoint();
this->attackDamage = obj.getAttackDamage();
std::cout << "ClapTrap " << this->name << " copy constructor called\\n";
}
ClapTrap::~ClapTrap(){
std::cout << "ClapTrap " << this->name << " destructor called\\n";
}
ClapTrap& ClapTrap::operator=(const ClapTrap &obj)
{
if (this == &obj)
return *this;
this->name = obj.getName();
this->attackDamage = obj.getAttackDamage();
this->hitPoint = obj.getHitPoint();
this->energyPoint = obj.getEnergyPoint();
std::cout << "ClapTrap operator = " << name << " called" << std::endl;
return (*this);
}
void ClapTrap::attack(const std::string& target)
{
if (!this->hitPoint)
{
std::cout << "ClapTrap " << this->name << " has no hitPoint died" << std::endl;
}
else if (!this->energyPoint)
{
std::cout << "ClapTrap " << this->name << " has no Energy Points\\n" << std::endl;
}
else
{
std::cout << "ClapTrap " << this->name << " attacks " << target;
std::cout << ", causing " << this->attackDamage << " points of damage!\\n";
this->energyPoint--;
}
}
void ClapTrap::takeDamage(unsigned int amount)
{
if (!this->hitPoint)
{
std::cout << "ClapTrap " << this->name << " has no hitPoint died" << std::endl;
}
else
{
if (this->hitPoint < amount)
this->hitPoint = 0;
else
this->hitPoint -= amount;
std::cout << "ClapTrap " << this->name << " has taken Damage " << amount << "\\n";
if (!this->hitPoint)
std::cout << "ClapTrap " << this->name << " died\\n";
}
}
void ClapTrap::beRepaired(unsigned int amount)
{
if (!this->hitPoint)
{
std::cout << "ClapTrap " << this->name << " has no hitPoint died" << std::endl;
}
else if (!this->energyPoint)
{
std::cout << "ClapTrap " << this->name << " has no Energy Points" << std::endl;
}
else
{
this->hitPoint += amount;
this->energyPoint--;
std::cout << "ClapTrap " << this->name << " has been repaired of " << amount << " His points. It has now " \\
<< this->hitPoint << " Hit points" << std::endl;
}
}
std::string ClapTrap::getName() const {
return this->name;
}
unsigned int ClapTrap::getHitPoint() const {
return this->hitPoint;
}
unsigned int ClapTrap::getEnergyPoint() const {
return this->energyPoint;
}
unsigned int ClapTrap::getAttackDamage() const {
return this->attackDamage;
}
반응형
'42Seoul > CPP Module 03' 카테고리의 다른 글
ex02 (1) | 2024.01.24 |
---|---|
ex00 (0) | 2024.01.24 |