42Seoul/CPP Module 03

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

재윤 2024. 1. 24. 15:55
반응형

ex01을 하기전 

 

상속에 대한 공부를 해보자

https://wo-dbs.tistory.com/147

 

C++ 상속

상속(inheritance)이란? “일정한 친족적 관계가 있는 사람 사이에 한 쪽이 사망하거나 법률상의 원인이 발생하였을 때 재산적 또는 친족적 권리와 의무를 계승하는 제도” 라고 네이버 사전에 정

wo-dbs.tistory.com

 

공부를 한 후 

 

상속 오버라이딩

https://wo-dbs.tistory.com/148

 

C++ 상속 오버라이딩

현재 ScavTrap이 ClapTrap을 상속받은 상태 ScavTrap.hpp #ifndef SCAVTRAP_HPP # define SCAVTRAP_HPP # include # include "ClapTrap.hpp" class ScavTrap : public ClapTrap { private: std::string name; unsigned int hitPoint; unsigned int energyPoint; uns

wo-dbs.tistory.com

 

상속으로 인한 메모리

https://wo-dbs.tistory.com/149

 

C++ 상속 부모, 자식 메모리

ScavTrap.hpp #ifndef SCAVTRAP_HPP # define SCAVTRAP_HPP # include # include "ClapTrap.hpp" class ScavTrap : public ClapTrap { private: std::string name; unsigned int hitPoint; unsigned int energyPoint; unsigned int attackDamage; public: ScavTrap(); ~ScavTr

wo-dbs.tistory.com

 

이 두 가지를 보고 한 번 코딩을 해보자

 

  1. 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