ex02 done and modified 42header on files

This commit is contained in:
2025-09-19 13:10:16 +02:00
parent 7cce8266c4
commit 5373b7728d
12 changed files with 401 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */

View File

@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */

View File

@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */

View File

@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.hpp :+: :+: :+: */
/* ScavTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */

View File

@@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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;
}
}

View 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

View File

@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 "FragTrap.hpp"
FragTrap::FragTrap() : ClapTrap("(NULL)")
{
this->health = 100;
this->energy = 100;
this->attackDamage = 30;
std::cout << "FragTrap default constructor called " << this->name
<< std::endl;
}
FragTrap::FragTrap(std::string name) : ClapTrap(name)
{
this->health = 100;
this->energy = 100;
this->attackDamage = 30;
std::cout << "FragTrap constructor called " << this->name
<< std::endl;
}
FragTrap::FragTrap(const FragTrap &other) : ClapTrap(other) { }
FragTrap& FragTrap::operator=(const FragTrap &other)
{
if (this != &other)
{
this->name = other.name;
this->health = other.health;
this->energy = other.energy;
this->attackDamage = other.attackDamage;
}
return (*this);
}
FragTrap::~FragTrap()
{
std::cout << "FragTrap destructor called for "
<< this->name << std::endl;
}
void FragTrap::highFivesGuys(void)
{
std::cout << "FragTrap " << this->name << " has requested a high-five!"
<< std::endl;
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.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 FRAGTRAP_HPP
# define FRAGTRAP_HPP
# include <iostream>
# include "../ClapTrap/ClapTrab.hpp"
class FragTrap : public ClapTrap
{
public:
FragTrap();
FragTrap(std::string name);
FragTrap(const FragTrap &other);
FragTrap &operator=(const FragTrap &other);
~FragTrap();
void highFivesGuys(void);
};
#endif

25
ex02/Makefile Normal file
View File

@@ -0,0 +1,25 @@
NAME = fragtrap
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

View File

@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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) { }
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;
}

View 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
ex02/main.cpp Normal file
View 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;
}