ex01 done
This commit is contained in:
97
ex01/ClapTrap/ClapTrap.cpp
Normal file
97
ex01/ClapTrap/ClapTrap.cpp
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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)
|
||||||
|
{
|
||||||
|
std::cout << "Default constructor called for "
|
||||||
|
<< name << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 for "
|
||||||
|
<< this->name << 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;
|
||||||
|
if (amount >= this->health)
|
||||||
|
this->health = 0;
|
||||||
|
else
|
||||||
|
this->health -= amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClapTrap::beRepaired(unsigned int amount)
|
||||||
|
{
|
||||||
|
if (this->energy > 0)
|
||||||
|
{
|
||||||
|
std::cout << "ClapTrap " << this->name
|
||||||
|
<< " repairs itself, recovering " << amount
|
||||||
|
<< " hit points!" << std::endl;
|
||||||
|
|
||||||
|
this->health += amount;
|
||||||
|
this->energy -= 1;
|
||||||
|
} else {
|
||||||
|
std::cout << "ClapTrap " << this->name
|
||||||
|
<< " cant be repaired..." << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
40
ex01/ClapTrap/ClapTrap.hpp
Normal file
40
ex01/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
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
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
ex01/Makefile
Normal file
25
ex01/Makefile
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
NAME = scavtrap
|
||||||
|
|
||||||
|
SRCS = main.cpp ClapTrap/ClapTrap.cpp ScavTrap/ScavTrap.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
|
||||||
76
ex01/ScavTrap/ScavTrap.cpp
Normal file
76
ex01/ScavTrap/ScavTrap.cpp
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ScavTrap.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 "ScavTrap.hpp"
|
||||||
|
|
||||||
|
ScavTrap::ScavTrap() : ClapTrap("(NULL)")
|
||||||
|
{
|
||||||
|
this->health = 100;
|
||||||
|
this->energy = 50;
|
||||||
|
this->attackDamage = 20;
|
||||||
|
std::cout << "ScavTrap default constructor called " << this->name
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScavTrap::ScavTrap(std::string name) : ClapTrap(name)
|
||||||
|
{
|
||||||
|
this->health = 100;
|
||||||
|
this->energy = 50;
|
||||||
|
this->attackDamage = 20;
|
||||||
|
std::cout << "ScavTrap constructor for " << this->name
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScavTrap::ScavTrap(const ScavTrap &other) : ClapTrap(other)
|
||||||
|
{
|
||||||
|
*this = other;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScavTrap& ScavTrap::operator=(const ScavTrap &other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
this->name = other.name;
|
||||||
|
this->health = other.health;
|
||||||
|
this->energy = other.energy;
|
||||||
|
this->attackDamage = other.attackDamage;
|
||||||
|
}
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScavTrap::~ScavTrap()
|
||||||
|
{
|
||||||
|
std::cout << "ScavTrap destructor called for "
|
||||||
|
<< this->name << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScavTrap::attack(const std::string &target)
|
||||||
|
{
|
||||||
|
if (this->health > 0 && this->energy > 0)
|
||||||
|
{
|
||||||
|
std::cout << "ScavTrap " << this->name
|
||||||
|
<< " attacks " << target
|
||||||
|
<< ", causing " << this->attackDamage
|
||||||
|
<< " points of damage!" << std::endl;
|
||||||
|
|
||||||
|
this->energy -= 1;
|
||||||
|
} else {
|
||||||
|
std::cout << "ScavTrap " << this->name
|
||||||
|
<< " cant attack anymore..." << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScavTrap::guardGate()
|
||||||
|
{
|
||||||
|
std::cout << "ScavTrap " << this->name << " is in Gate keeper mode"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
34
ex01/ScavTrap/ScavTrap.hpp
Normal file
34
ex01/ScavTrap/ScavTrap.hpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ScavTrap.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 SCAVTRAP_HPP
|
||||||
|
|
||||||
|
# define SCAVTRAP_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include "../ClapTrap/ClapTrap.hpp"
|
||||||
|
|
||||||
|
class ScavTrap : public ClapTrap
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ScavTrap();
|
||||||
|
ScavTrap(std::string name);
|
||||||
|
ScavTrap(const ScavTrap &other);
|
||||||
|
ScavTrap& operator=(const ScavTrap &other);
|
||||||
|
~ScavTrap();
|
||||||
|
|
||||||
|
void attack(const std::string &target);
|
||||||
|
|
||||||
|
void guardGate();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
39
ex01/main.cpp
Normal file
39
ex01/main.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/09/12 13:17:34 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2025/09/19 12:34:26 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ClapTrap/ClapTrap.hpp"
|
||||||
|
#include "ScavTrap/ScavTrap.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << "--- Testing ClapTrap ---" << std::endl;
|
||||||
|
ClapTrap a("Alice");
|
||||||
|
a.attack("enemy");
|
||||||
|
a.takeDamage(3);
|
||||||
|
a.beRepaired(2);
|
||||||
|
a.attack("enemy");
|
||||||
|
|
||||||
|
std::cout << "\n--- Testing ScavTrap ---" << std::endl;
|
||||||
|
ScavTrap b("Bob");
|
||||||
|
b.attack("enemy");
|
||||||
|
b.takeDamage(20);
|
||||||
|
b.beRepaired(10);
|
||||||
|
b.guardGate();
|
||||||
|
|
||||||
|
std::cout << "\n--- Copy and Assignment Test ---" << std::endl;
|
||||||
|
ScavTrap c = b; // copy constructor
|
||||||
|
ScavTrap d;
|
||||||
|
d = b; // assignment operator
|
||||||
|
d.attack("another enemy");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user