mastring the simple heritage

This commit is contained in:
Raphael 2025-01-24 21:02:57 +01:00
parent 245ac4c3a9
commit d161579a2a
13 changed files with 376 additions and 4 deletions

55
cpp03/ex02/ClapTrap.cpp Normal file
View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/10 13:59:49 by rparodi #+# #+# */
/* Updated: 2025/01/24 18:32:35 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
#include <string>
#include <iostream>
ClapTrap::ClapTrap() {
}
ClapTrap::ClapTrap(std::string name) {
_name = name;
_hit_point = 10;
_energy_point = 10;
_attack_damage = 0;
std::cout << "\n[Init] ClapTrap:\n\t" << "Name: " << _name << std::endl;
}
ClapTrap::~ClapTrap() {
std::cout << "\n[Delete] ClapTrap:\n\t" << "Name: " << _name << std::endl;
}
void ClapTrap::attack(const std::string& target) {
if (_energy_point <= 0 || _hit_point <= 0) {
std::cerr << "\n[Attack] ClapTrap:\n\t" << "Not enough energy to attack" << std::endl;
return ;
}
_energy_point--;
std::cout << "\n[Attack] ClapTrap:\n\t" << _name << " attacks " << target << ", causing " << _attack_damage << " points of damage!" << std::endl;
}
void ClapTrap::takeDamage(unsigned int amount) {
_hit_point -= amount;
std::cout << "\n[Hurt] ClapTrap:\n\t" << _name << " take " << amount << " now hit_point are " << _hit_point << std::endl;
}
void ClapTrap::beRepaired(unsigned int amount) {
if (_energy_point <= 0 || _hit_point <= 0) {
std::cerr << "\n[Repair] ClapTrap:\n\t" << "Not enough energy to repair" << std::endl;
return ;
}
_energy_point--;
_hit_point += amount;
std::cout << "\n[Repair] ClapTrap:\n\t" << _name << " repair, for an amount " << amount << " now hit_point are " << _hit_point << std::endl;
}

35
cpp03/ex02/ClapTrap.hpp Normal file
View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ClapTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/10 14:54:21 by rparodi #+# #+# */
/* Updated: 2025/01/24 18:32:41 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CLAPTRAP_HPP
#define CLAPTRAP_HPP
#include <string>
class ClapTrap {
public:
ClapTrap();
ClapTrap(std::string name);
~ClapTrap();
void attack(const std::string& target);
void takeDamage(unsigned int amount);
void beRepaired(unsigned int amount);
protected:
std::string _name;
int _hit_point;
int _energy_point;
int _attack_damage;
private:
};
#endif

25
cpp03/ex02/FragTrap.cpp Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 17:41:01 by rparodi #+# #+# */
/* Updated: 2025/01/24 21:01:58 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "FragTrap.hpp"
FragTrap::FragTrap (std::string name) {
_name = name;
_hit_point = 100;
_energy_point = 100;
_attack_damage = 30;
std::cout << "\n[Init] FragTrap:\n\t" << "Name: " << _name << std::endl;
}
void FragTrap::highFivesGuys() {
std::cout << "\n[Clap] FragTrap:\n\t" << "Name:" << _name << "Hey bro can i get an high five ? Thanks u brother !" << _name << "Hey bro can i get an high five ? Thanks u brother !" << std::endl;
}

29
cpp03/ex02/FragTrap.hpp Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* FragTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/20 23:13:16 by rparodi #+# #+# */
/* Updated: 2025/01/24 20:54:17 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FRAGTRAP_HPP
#define FRAGTRAP_HPP
#include <iostream>
#include "ClapTrap.hpp"
class FragTrap: public ClapTrap {
public:
FragTrap(std::string name);
void highFivesGuys(void);
protected:
private:
};
#endif

124
cpp03/ex02/Makefile Normal file
View file

@ -0,0 +1,124 @@
# **************************************************************************** #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/12/21 19:50:58 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = open
# Commands
CXX = c++
RM = rm -rf
# Flags
# Mandatory flags for 42 CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
# Flags to debug and have the dependences (can be removed for correction)
CXXFLAGS += -MMD -g3
# Flag to debug (TO REMOVE)
# CXXFLAGS += -D DEBUG=1
# Sources
SRC = ClapTrap.cpp \
FragTrap.cpp \
ScavTrap.cpp \
main.cpp
# Objects
OBJDIRNAME = ./build
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
# Colors
GREEN = \033[32m
GREY = \033[0;90m
RED = \033[0;31m
GOLD = \033[38;5;220m
END = \033[0m
# Rules
# All (make all)
all: header $(NAME) footer
# Bonus (make bonus)
bonus: header $(OBJ) footer
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Clean (make clean)
clean:
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
@$(RM) $(OBJDIRNAME)
# Clean (make fclean)
fclean: clean
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
@$(RM) $(NAME)
@echo ""
# Restart (make re)
re: header fclean all
# Dependences for all
$(NAME): $(OBJ)
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Creating the objects
$(OBJDIRNAME)/%.o: %.cpp
@mkdir -p $(dir $@)
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
@$(CXX) $(CXXFLAGS) -o $@ -c $<
# Header
header:
@clear
@printf '\n\n'
@printf '$(GOLD) ******* ****** ******* $(END)\n'
@printf '$(GOLD) ****** *** ******* $(END)\n'
@printf '$(GOLD) ******* * ******* $(END)\n'
@printf '$(GOLD) ****** ******* $(END)\n'
@printf '$(GOLD) ******* ******* $(END)\n'
@printf '$(GOLD) ******************* ******* * $(END)\n'
@printf '$(GOLD) ******************* ******* *** $(END)\n'
@printf '$(GOLD) ****** ******* ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GREY) Made by rparodi$(END)\n\n'
# Footer
footer:
@printf "\n"
@printf "$(GOLD) ,_ _,$(END)\n"
@printf "$(GOLD) | \\___//|$(END)\n"
@printf "$(GOLD) |=6 6=|$(END)\n"
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
@printf "$(GOLD) ) \` ( ,$(END)\n"
@printf "$(GOLD) / \\ (('$(END)\n"
@printf "$(GOLD) | | ))$(END)\n"
@printf "$(GOLD) /| | | |\\_//$(END)\n"
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
@printf "$(GOLD) '\"' '\"'$(END)\n"
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
clangd:
@echo \
"CompileFlags:\n" \
" Add:\n" \
" - \"-std=c++98 -Wall -Wextra -Werror\"\n" \
" - \"-I"$(shell pwd)"/includes\"\n" \
" - \"-xc++\"\n" \
> .clangd
# Phony
.PHONY: all bonus clean fclean re clangd
-include ${OBJ:.o=.d}

27
cpp03/ex02/ScavTrap.cpp Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 17:41:01 by rparodi #+# #+# */
/* Updated: 2025/01/24 19:24:05 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
ScavTrap::ScavTrap (std::string name) {
_name = name;
_hit_point = 100;
_energy_point = 50;
_attack_damage = 20;
_gateKeeperMode = false;
std::cout << "\n[Init] ScavTrap:\n\t" << "Name: " << _name << std::endl;
}
void ScavTrap::guardGate() {
_gateKeeperMode = !_gateKeeperMode;
std::cout << "\n[Mode] ScavTrap:\n\t" << "Name: " << _name << " the mode gate keeper is now: " << (_gateKeeperMode ? "enable" : "disable") << std::endl;
}

30
cpp03/ex02/ScavTrap.hpp Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScavTrap.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/20 23:13:16 by rparodi #+# #+# */
/* Updated: 2025/01/24 20:57:35 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SCAVTRAP_HPP
#define SCAVTRAP_HPP
#include <iostream>
#include "ClapTrap.hpp"
class ScavTrap : public ClapTrap {
public:
ScavTrap(std::string name);
void guardGate();
protected:
private:
bool _gateKeeperMode;
};
#endif

37
cpp03/ex02/main.cpp Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/10 15:50:07 by rparodi #+# #+# */
/* Updated: 2025/01/24 21:02:23 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ClapTrap.hpp"
#include "ScavTrap.hpp"
#include "FragTrap.hpp"
#include <iostream>
int main(void) {
std::cout << "[Broadcast]\n\tIn the Blue corner the actual cat of the 42 school !" << std::endl;
ClapTrap test("Moulinette");
test.beRepaired(32);
std::cout << "\n\n[Broadcast]\n\tIn the Red corner the first cat of the 42 school !" << std::endl;
ScavTrap test2("Norminet");
test2.attack("Moulinette");
test2.takeDamage(42);
test2.beRepaired(42);
test2.guardGate();
test2.guardGate();
test2.guardGate();
std::cout << "\n\n[Broadcast]\n\t And for the referee of this legendary fight the others cats of Made !" << std::endl;
FragTrap test3("Norminet");
test3.attack("Moulinette");
test3.takeDamage(42);
test3.beRepaired(42);
test3.highFivesGuys();
return (0);
}