commit 4f3824ac2acaa9474f9ac7e52dfcafed69b75cdb Author: Camille Chauvet Date: Mon Aug 7 21:04:12 2023 +0200 init ex00 diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..b10e2f7 --- /dev/null +++ b/ex00/Makefile @@ -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 diff --git a/ex00/src/ClapTrap.cpp b/ex00/src/ClapTrap.cpp new file mode 100644 index 0000000..feab9e2 --- /dev/null +++ b/ex00/src/ClapTrap.cpp @@ -0,0 +1,76 @@ +#include "ClapTrap.hpp" +#include + +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; +} diff --git a/ex00/src/ClapTrap.hpp b/ex00/src/ClapTrap.hpp new file mode 100644 index 0000000..7bfdede --- /dev/null +++ b/ex00/src/ClapTrap.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +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); +}; diff --git a/ex00/src/main.cpp b/ex00/src/main.cpp new file mode 100644 index 0000000..3cdf88e --- /dev/null +++ b/ex00/src/main.cpp @@ -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; +}