42Seoul/CPP Module 03

ex00

재윤 2024. 1. 24. 15:45
반응형
  • 00에서 나오는 개념은 없다 문제대로 구현해주자.
  1. ClapTrap 구현

ClapTrap.hpp

#ifndef CLAPTRAP_HPP
# define CLAPTRAP_HPP

# include <iostream>

class ClapTrap{
private:
	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" << 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;
}

 

main.cpp

#include "ClapTrap.hpp"

int	main(void)
{
	ClapTrap a("A");
	ClapTrap b("B");

	a.attack("B");
	std::cout << "a : getAttackDamage " << a.getAttackDamage() << std::endl;
	std::cout << "a : getEnergyPoint " << a.getEnergyPoint() << std::endl;
	std::cout << "a : getHitPoint " << a.getHitPoint() << std::endl;
	b.takeDamage(5);
	std::cout << "b : getAttackDamage " << b.getAttackDamage() << std::endl;
	std::cout << "b : getEnergyPoint " << b.getEnergyPoint() << std::endl;
	std::cout << "b : getHitPoint " << b.getHitPoint() << std::endl;
	b.beRepaired(3);
	std::cout << "b : getAttackDamage " << b.getAttackDamage() << std::endl;
	std::cout << "b : getEnergyPoint " << b.getEnergyPoint() << std::endl;
	std::cout << "b : getHitPoint " << b.getHitPoint() << std::endl;
	b.attack("A");
	a.takeDamage(10);
	std::cout << "a : getAttackDamage " << a.getAttackDamage() << std::endl;
	std::cout << "a : getEnergyPoint " << a.getEnergyPoint() << std::endl;
	std::cout << "a : getHitPoint " << a.getHitPoint() << std::endl;
	a.beRepaired(10);
	std::cout << "a : getAttackDamage " << a.getAttackDamage() << std::endl;
	std::cout << "a : getEnergyPoint " << a.getEnergyPoint() << std::endl;
	std::cout << "a : getHitPoint " << a.getHitPoint() << std::endl;
}
반응형

'42Seoul > CPP Module 03' 카테고리의 다른 글

ex02  (1) 2024.01.24
ex01(상속, 오버라이딩, 가상 함수)  (1) 2024.01.24