init ex00
This commit is contained in:
		
							
								
								
									
										25
									
								
								ex00/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								ex00/Makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
CXX = c++
 | 
			
		||||
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
 | 
			
		||||
SRCDIR = src
 | 
			
		||||
OBJDIR = obj
 | 
			
		||||
NAME = ex00
 | 
			
		||||
 | 
			
		||||
SRCS = $(wildcard $(SRCDIR)/*.cpp)
 | 
			
		||||
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
 | 
			
		||||
 | 
			
		||||
all: $(NAME)
 | 
			
		||||
 | 
			
		||||
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
 | 
			
		||||
	mkdir -p obj
 | 
			
		||||
	$(CXX) $(CPPFLAGS) -c $< -o $@
 | 
			
		||||
 | 
			
		||||
$(NAME): $(OBJS)
 | 
			
		||||
	$(CXX) $(CPPFLAGS) $^ -o $@
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	rm -rf $(OBJDIR)/*.o
 | 
			
		||||
 | 
			
		||||
fclean: clean
 | 
			
		||||
	rm -fr $(NAME)
 | 
			
		||||
 | 
			
		||||
re: fclean all
 | 
			
		||||
							
								
								
									
										76
									
								
								ex00/src/ClapTrap.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								ex00/src/ClapTrap.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,76 @@
 | 
			
		||||
#include "ClapTrap.hpp"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
ClapTrap::ClapTrap():
 | 
			
		||||
	_name(""),
 | 
			
		||||
	_life(10),
 | 
			
		||||
	_energy(10),
 | 
			
		||||
	_damage(0)
 | 
			
		||||
{
 | 
			
		||||
	std::cout << "ClapTrap()" << std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ClapTrap::ClapTrap(const std::string& name):
 | 
			
		||||
	_name(name),
 | 
			
		||||
	_life(10),
 | 
			
		||||
	_energy(10),
 | 
			
		||||
	_damage(0)
 | 
			
		||||
{
 | 
			
		||||
	std::cout << "ClapTrap(" << name << ")" << std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ClapTrap::ClapTrap(const ClapTrap& src)
 | 
			
		||||
{
 | 
			
		||||
	std::cout << "ClapTrap(ClapTrap)" << std::endl;
 | 
			
		||||
	*this = src;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ClapTrap::~ClapTrap()
 | 
			
		||||
{
 | 
			
		||||
	std::cout << "~ClapTrap()" << std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ClapTrap& ClapTrap::operator=(const ClapTrap& src)
 | 
			
		||||
{
 | 
			
		||||
	this->_name = src._name;
 | 
			
		||||
	this->_life = src._life;
 | 
			
		||||
	this->_energy = src._energy;
 | 
			
		||||
	this->_damage = src._damage;
 | 
			
		||||
 | 
			
		||||
	return *this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void ClapTrap::beRepaired(unsigned int amount)
 | 
			
		||||
{
 | 
			
		||||
	if (this->_life == 0 || this->_energy == 0)
 | 
			
		||||
	{
 | 
			
		||||
		std::cerr << "error: ClapTrap " << this->_name << " dead" << std::endl;
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	this->_life += amount;
 | 
			
		||||
	this->_energy--;
 | 
			
		||||
	std::cerr << "ClapTrap " << this->_name << " beRepaired causing life is now " << this->_life << std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ClapTrap::takeDamage(unsigned int amount)
 | 
			
		||||
{
 | 
			
		||||
	if (this->_life == 0 || this->_energy == 0)
 | 
			
		||||
	{
 | 
			
		||||
		std::cerr << "error: ClapTrap " << this->_name << " dead" << std::endl;
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	this->_life -= amount;
 | 
			
		||||
	std::cout << "ClapTrap " << this->_name << " be attacked causing " << amount << " points of damage!" << std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ClapTrap::attack(const std::string &target)
 | 
			
		||||
{
 | 
			
		||||
	if (this->_life == 0 || this->_energy == 0)
 | 
			
		||||
	{
 | 
			
		||||
		std::cerr << "error: ClapTrap " << this->_name << " dead" << std::endl;
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	this->_energy--;
 | 
			
		||||
	std::cerr << "ClapTrap " << this->_name << " attacks " << target << ", causing " << this->_damage << " points of damage" << std::endl;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								ex00/src/ClapTrap.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								ex00/src/ClapTrap.hpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
class ClapTrap
 | 
			
		||||
{
 | 
			
		||||
	private:
 | 
			
		||||
		std::string _name;
 | 
			
		||||
		int			_life;
 | 
			
		||||
		int			_energy;
 | 
			
		||||
		int			_damage;
 | 
			
		||||
 | 
			
		||||
	public:
 | 
			
		||||
		ClapTrap();
 | 
			
		||||
		ClapTrap(const ClapTrap& src);
 | 
			
		||||
		ClapTrap(const std::string& name);
 | 
			
		||||
		~ClapTrap();
 | 
			
		||||
 | 
			
		||||
		ClapTrap &operator=(const ClapTrap& src);
 | 
			
		||||
 | 
			
		||||
		void attack(const std::string& target);
 | 
			
		||||
		void takeDamage(unsigned int amount);
 | 
			
		||||
		void beRepaired(unsigned int amount);
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										20
									
								
								ex00/src/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								ex00/src/main.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
#include "ClapTrap.hpp"
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    ClapTrap claptrap1("Claptrap1");
 | 
			
		||||
    ClapTrap claptrap2("Claptrap2");
 | 
			
		||||
 | 
			
		||||
    claptrap1.attack("Bandit");
 | 
			
		||||
    claptrap2.takeDamage(20);
 | 
			
		||||
    claptrap1.beRepaired(10);
 | 
			
		||||
    claptrap1.attack("bozoman");
 | 
			
		||||
    claptrap2.takeDamage(20);
 | 
			
		||||
    claptrap1.beRepaired(1000);
 | 
			
		||||
 | 
			
		||||
	ClapTrap tmp(claptrap1);
 | 
			
		||||
	tmp.attack("bozo");
 | 
			
		||||
	tmp = claptrap2;
 | 
			
		||||
	tmp.attack("bozo");
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user