반응형
- ClapTrap을 상속받은 FragTrap를 만든다. guardGate 대신 highFivesGuys를 구현.
FragTrap.hpp
#ifndef FRAGTRAP_HPP
# define FRAGTRAP_HPP
# include <iostream>
# include "ClapTrap.hpp"
class FragTrap : public ClapTrap
{
public:
FragTrap();
~FragTrap();
FragTrap(std::string name);
FragTrap(const FragTrap& FragTrap);
FragTrap& operator=(const FragTrap& obj);
void highFivesGuys(void);
};
# endif
FragTrap.cpp
#include "FragTrap.hpp"
FragTrap::FragTrap()
{
this->name = "basic";
this->hitPoint = 100;
this->energyPoint = 100;
this->attackDamage = 30;
std::cout << "FragTrap " << this->name << " constructor called\\n";
}
FragTrap::FragTrap(std::string name)
{
this->name = name;
this->hitPoint = 100;
this->energyPoint = 100;
this->attackDamage = 30;
std::cout << "FragTrap " << this->name << " constructor called\\n";
}
FragTrap::FragTrap(FragTrap const &fragTrap)
{
this->name = fragTrap.getName();
this->hitPoint = fragTrap.getHitPoint();
this->energyPoint = fragTrap.getEnergyPoint();
this->attackDamage = fragTrap.getAttackDamage();
std::cout << "FragTrap " << this->name << " copy constructor called\\n";
}
FragTrap& FragTrap::operator=(FragTrap const &obj)
{
if (this == &obj)
return *this;
this->name = obj.getName();
this->hitPoint = obj.getHitPoint();
this->energyPoint = obj.getEnergyPoint();
this->attackDamage = obj.getAttackDamage();
std::cout << "FragTrap operator = " << name << " called" << std::endl;
return (*this);
}
FragTrap::~FragTrap() {
std::cout << "FragTrap " << this->name << " destructor called\\n";
}
void FragTrap::highFivesGuys(void) {
std::cout << "FragTrap " << this->name << " requests a positive high fives\\n";
}
main.cpp
#include "ScavTrap.hpp"
#include "FragTrap.hpp"
int main(void)
{
ScavTrap A("A");
ClapTrap B("B");
FragTrap C("C");
ClapTrap D(B);
A.attack("B");
C.takeDamage(0);
C.takeDamage(2);
C.beRepaired(2);
C.takeDamage(10);
A.takeDamage(50);
C.attack("A");
A.guardGate();
D.attack("B");
C.highFivesGuys();
}
반응형
'42Seoul > CPP Module 03' 카테고리의 다른 글
ex01(상속, 오버라이딩, 가상 함수) (1) | 2024.01.24 |
---|---|
ex00 (0) | 2024.01.24 |