ex00 done
This commit is contained in:
86
ex00/ClapTrap/ClapTrap.cpp
Normal file
86
ex00/ClapTrap/ClapTrap.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ClapTrap.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
|
||||
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ClapTrap.hpp"
|
||||
|
||||
ClapTrap::ClapTrap() :
|
||||
name("(NULL)"), health(10),
|
||||
energy(10), attackDamage(0)
|
||||
{
|
||||
std::cout << "Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ClapTrap::ClapTrap(std::string name) :
|
||||
name(name), health(10),
|
||||
energy(10), attackDamage(0)
|
||||
{}
|
||||
|
||||
ClapTrap::ClapTrap(const ClapTrap &target)
|
||||
{
|
||||
this->name = target.name;
|
||||
this->health = target.health;
|
||||
this->energy = target.energy;
|
||||
this->attackDamage = target.attackDamage;
|
||||
}
|
||||
|
||||
ClapTrap& ClapTrap::operator=(const ClapTrap &target)
|
||||
{
|
||||
if (this != &target)
|
||||
{
|
||||
this->name = target.name;
|
||||
this->health = target.health;
|
||||
this->energy = target.energy;
|
||||
this->attackDamage = target.attackDamage;
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
ClapTrap::~ClapTrap()
|
||||
{
|
||||
std::cout << "Destructor called" << std::endl;
|
||||
}
|
||||
|
||||
void ClapTrap::attack(const std::string &target)
|
||||
{
|
||||
if (this->health > 0 && this->energy > 0)
|
||||
{
|
||||
std::cout << "ClapTrap " << this->name
|
||||
<< " attacks " << target
|
||||
<< ", causing " << this->attackDamage
|
||||
<< " points of damage!" << std::endl;
|
||||
|
||||
this->energy -= 1;
|
||||
} else {
|
||||
std::cout << "ClapTrap " << this->name
|
||||
<< " cant attack anymore..." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ClapTrap::takeDamage(unsigned int amount)
|
||||
{
|
||||
std::cout << "ClapTrap " << this->name
|
||||
<< " recieves " << amount
|
||||
<< " points of damage!" << std::endl;
|
||||
this->health -= amount;
|
||||
}
|
||||
|
||||
void ClapTrap::beRepaired(unsigned int amount)
|
||||
{
|
||||
if (this->energy > 0)
|
||||
{
|
||||
this->health += amount;
|
||||
this->energy -= 1;
|
||||
} else {
|
||||
std::cout << "ClapTrap " << this->name
|
||||
<< " cant be repaired..." << std::endl;
|
||||
}
|
||||
}
|
||||
40
ex00/ClapTrap/ClapTrap.hpp
Normal file
40
ex00/ClapTrap/ClapTrap.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ClapTrap.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
|
||||
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CLAPTRAP_HPP
|
||||
|
||||
# define CLAPTRAP_HPP
|
||||
|
||||
# include <iostream>
|
||||
|
||||
class ClapTrap
|
||||
{
|
||||
private:
|
||||
std::string name;
|
||||
unsigned int health;
|
||||
unsigned int energy;
|
||||
unsigned int attackDamage;
|
||||
|
||||
public:
|
||||
ClapTrap();
|
||||
ClapTrap(std::string name);
|
||||
ClapTrap(const ClapTrap &target);
|
||||
ClapTrap& operator=(const ClapTrap &target);
|
||||
~ClapTrap();
|
||||
|
||||
void attack(const std::string &target);
|
||||
void takeDamage(unsigned int amount);
|
||||
void beRepaired(unsigned int amount);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
25
ex00/Makefile
Normal file
25
ex00/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
NAME = claptrap
|
||||
|
||||
SRCS = main.cpp ClapTrap/ClapTrap.cpp
|
||||
OBJS=$(SRCS:.cpp=.o)
|
||||
|
||||
CC=c++
|
||||
CFLAGS=-Wall -Wextra -Werror -std=c++98
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
25
ex00/main.cpp
Normal file
25
ex00/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/12 13:17:34 by aortigos #+# #+# */
|
||||
/* Updated: 2025/09/16 23:54:56 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ClapTrap/ClapTrap.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
ClapTrap hadi("Angel");
|
||||
|
||||
hadi.attack("google");
|
||||
hadi.takeDamage(3);
|
||||
hadi.beRepaired(5);
|
||||
hadi.attack("Yahoo");
|
||||
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user