feat(ex03): it works
This commit is contained in:
parent
5cd051a7f7
commit
7fb5f99a7a
11 changed files with 320 additions and 0 deletions
1
ex03/.gitignore
vendored
Normal file
1
ex03/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
ex03
|
||||||
69
ex03/ClapTrap.cpp
Normal file
69
ex03/ClapTrap.cpp
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
#include "ClapTrap.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
ClapTrap::ClapTrap()
|
||||||
|
: name("[name unset]"), hit_points(10), energy_points(10),
|
||||||
|
attack_damage(0) {
|
||||||
|
std::cout << "ClapTrap::ClapTrap()" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
ClapTrap::ClapTrap(std::string name)
|
||||||
|
: name(name), hit_points(10), energy_points(10), attack_damage(0) {
|
||||||
|
std::cout << "ClapTrap::ClapTrap(" << name << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
ClapTrap::~ClapTrap() { std::cout << "ClapTrap::~ClapTrap()" << std::endl; }
|
||||||
|
|
||||||
|
ClapTrap &ClapTrap::operator=(const ClapTrap &other) {
|
||||||
|
std::cout << "ClapTrap::operator=()" << std::endl;
|
||||||
|
name = other.name;
|
||||||
|
hit_points = other.hit_points;
|
||||||
|
energy_points = other.energy_points;
|
||||||
|
attack_damage = other.attack_damage;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClapTrap::attack(const std::string &target) {
|
||||||
|
if (energy_points > 0) {
|
||||||
|
std::cout << "ClapTrap " << name << " attacks " << target << ", causing "
|
||||||
|
<< attack_damage << " points of damage!" << std::endl;
|
||||||
|
energy_points--;
|
||||||
|
} else {
|
||||||
|
std::cout << "ClapTrap " << name << " tries to attack " << target
|
||||||
|
<< ", but has no more energy! " << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClapTrap::takeDamage(unsigned int amount) {
|
||||||
|
std::cout << "ClapTrap " << name << " took " << amount << " points of damage!"
|
||||||
|
<< std::endl;
|
||||||
|
if (amount > hit_points)
|
||||||
|
amount = hit_points;
|
||||||
|
hit_points -= amount;
|
||||||
|
if (hit_points == 0)
|
||||||
|
std::cout << "ClapTrap " << name << " died!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClapTrap::beRepaired(unsigned int amount) {
|
||||||
|
|
||||||
|
if (energy_points > 0) {
|
||||||
|
std::cout << "ClapTrap " << name << " is repaired for " << amount
|
||||||
|
<< " hit points!" << std::endl;
|
||||||
|
hit_points += amount;
|
||||||
|
energy_points--;
|
||||||
|
} else {
|
||||||
|
std::cout << "ClapTrap " << name
|
||||||
|
<< " tries to repair itself, but has no more energy! "
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int ClapTrap::get_hit_points() const { return hit_points; }
|
||||||
|
unsigned int ClapTrap::get_energy_points() const { return energy_points; }
|
||||||
|
unsigned int ClapTrap::get_attack_damage() const { return attack_damage; }
|
||||||
|
|
||||||
|
void ClapTrap::showStats() const {
|
||||||
|
std::cout << "ClapTrap " << name << " stats: hit_points: " << hit_points
|
||||||
|
<< " energy_points: " << energy_points
|
||||||
|
<< " attack_damage: " << attack_damage << std::endl;
|
||||||
|
}
|
||||||
30
ex03/ClapTrap.hpp
Normal file
30
ex03/ClapTrap.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef CLAPTRAP_HPP
|
||||||
|
#define CLAPTRAP_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class ClapTrap {
|
||||||
|
protected:
|
||||||
|
std::string name;
|
||||||
|
unsigned int hit_points;
|
||||||
|
unsigned int energy_points;
|
||||||
|
unsigned int attack_damage;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ClapTrap();
|
||||||
|
ClapTrap(std::string name);
|
||||||
|
~ClapTrap();
|
||||||
|
ClapTrap &operator=(const ClapTrap &other);
|
||||||
|
|
||||||
|
void attack(const std::string &target);
|
||||||
|
void takeDamage(unsigned int amount);
|
||||||
|
void beRepaired(unsigned int amount);
|
||||||
|
|
||||||
|
unsigned int get_hit_points() const;
|
||||||
|
unsigned int get_energy_points() const;
|
||||||
|
unsigned int get_attack_damage() const;
|
||||||
|
|
||||||
|
void showStats() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
34
ex03/DiamondTrap.cpp
Normal file
34
ex03/DiamondTrap.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include "DiamondTrap.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
DiamondTrap::DiamondTrap() : FragTrap(), ScavTrap() {
|
||||||
|
const FragTrap fragtrap = FragTrap();
|
||||||
|
|
||||||
|
hit_points = fragtrap.get_hit_points();
|
||||||
|
energy_points = ScavTrap::energy_points;
|
||||||
|
attack_damage = fragtrap.get_attack_damage();
|
||||||
|
|
||||||
|
ClapTrap::name = name + "_clap_name";
|
||||||
|
std::cout << "DiamondTrap::DiamondTrap()" << std::endl;
|
||||||
|
}
|
||||||
|
DiamondTrap::DiamondTrap(std::string name) : name(name) {
|
||||||
|
const FragTrap fragtrap = FragTrap(name);
|
||||||
|
|
||||||
|
hit_points = fragtrap.get_hit_points();
|
||||||
|
energy_points = ScavTrap::energy_points;
|
||||||
|
attack_damage = fragtrap.get_attack_damage();
|
||||||
|
|
||||||
|
ClapTrap::name = name + "_clap_name";
|
||||||
|
std::cout << "DiamondTrap::DiamondTrap(" << name << ")" << std::endl;
|
||||||
|
}
|
||||||
|
DiamondTrap &DiamondTrap::operator=(DiamondTrap &other) {
|
||||||
|
this->name = other.name;
|
||||||
|
ClapTrap::operator=(other);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
DiamondTrap::~DiamondTrap() {}
|
||||||
|
|
||||||
|
void DiamondTrap::whoAmI() {
|
||||||
|
std::cout << "ClapName: " << ClapTrap::name << " DiamondName: " << name
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
22
ex03/DiamondTrap.hpp
Normal file
22
ex03/DiamondTrap.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#ifndef DIAMONDTRAP_HPP
|
||||||
|
#define DIAMONDTRAP_HPP
|
||||||
|
|
||||||
|
#include "FragTrap.hpp"
|
||||||
|
#include "ScavTrap.hpp"
|
||||||
|
|
||||||
|
class DiamondTrap : public FragTrap, public ScavTrap {
|
||||||
|
private:
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DiamondTrap();
|
||||||
|
DiamondTrap(std::string name);
|
||||||
|
DiamondTrap &operator=(DiamondTrap &other);
|
||||||
|
~DiamondTrap();
|
||||||
|
|
||||||
|
using ScavTrap::attack;
|
||||||
|
|
||||||
|
void whoAmI();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
31
ex03/FragTrap.cpp
Normal file
31
ex03/FragTrap.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#include "FragTrap.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
FragTrap::FragTrap() {
|
||||||
|
std::cout << "FragTrap::FragTrap()" << std::endl;
|
||||||
|
hit_points = 100;
|
||||||
|
energy_points = 100;
|
||||||
|
attack_damage = 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
FragTrap::FragTrap(std::string name) {
|
||||||
|
std::cout << "FragTrap::FragTrap(" << name << ")" << std::endl;
|
||||||
|
hit_points = 100;
|
||||||
|
energy_points = 100;
|
||||||
|
attack_damage = 30;
|
||||||
|
this->name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
FragTrap::~FragTrap() { std::cout << "FragTrap::~FragTrap()" << std::endl; }
|
||||||
|
|
||||||
|
FragTrap &FragTrap::operator=(const FragTrap &other) {
|
||||||
|
this->hit_points = other.hit_points;
|
||||||
|
this->energy_points = other.energy_points;
|
||||||
|
this->attack_damage = other.attack_damage;
|
||||||
|
this->name = other.name;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FragTrap::highFivesGuys() {
|
||||||
|
std::cout << "FragTrap " << name << ": High-Five Guys?" << std::endl;
|
||||||
|
}
|
||||||
16
ex03/FragTrap.hpp
Normal file
16
ex03/FragTrap.hpp
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef FRAGTRAP_HPP
|
||||||
|
#define FRAGTRAP_HPP
|
||||||
|
|
||||||
|
#include "ClapTrap.hpp"
|
||||||
|
|
||||||
|
class FragTrap : public virtual ClapTrap {
|
||||||
|
public:
|
||||||
|
FragTrap();
|
||||||
|
FragTrap(std::string name);
|
||||||
|
~FragTrap();
|
||||||
|
FragTrap &operator=(const FragTrap &other);
|
||||||
|
|
||||||
|
void highFivesGuys();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
42
ex03/Makefile
Normal file
42
ex03/Makefile
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
NAME = ex03
|
||||||
|
ifeq ($(CPPFLAGS),)
|
||||||
|
CPPFLAGS = -Wall -Wextra -Werror -Wshadow -std=c++98 -g
|
||||||
|
endif
|
||||||
|
ifeq ($(CXX),)
|
||||||
|
CXX = c++
|
||||||
|
endif
|
||||||
|
# g++ is the default on 42 computers
|
||||||
|
ifeq ($(CXX),g++)
|
||||||
|
CXX = c++
|
||||||
|
endif
|
||||||
|
srcs = \
|
||||||
|
ClapTrap.cpp \
|
||||||
|
ScavTrap.cpp \
|
||||||
|
FragTrap.cpp \
|
||||||
|
DiamondTrap.cpp \
|
||||||
|
|
||||||
|
main_objs = main.o $(srcs:.cpp=.o)
|
||||||
|
all_objs = $(main_objs)
|
||||||
|
deps = $(all_objs:.o=.d)
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
-include $(deps)
|
||||||
|
|
||||||
|
$(NAME): $(main_objs)
|
||||||
|
$(CXX) $(CPPFLAGS) -o $@ $^
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp
|
||||||
|
$(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d
|
||||||
|
|
||||||
|
clean:
|
||||||
|
find . -name '*.o' -print -delete
|
||||||
|
find . -name '*.d' -print -delete
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(NAME)
|
||||||
|
|
||||||
|
re:
|
||||||
|
+make fclean
|
||||||
|
+make all
|
||||||
32
ex03/ScavTrap.cpp
Normal file
32
ex03/ScavTrap.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
#include "ScavTrap.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
ScavTrap::ScavTrap() {
|
||||||
|
std::cout << "ScavTrap::ScavTrap()" << std::endl;
|
||||||
|
hit_points = 100;
|
||||||
|
energy_points = 50;
|
||||||
|
attack_damage = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScavTrap::ScavTrap(std::string name) {
|
||||||
|
std::cout << "ScavTrap::ScavTrap(" << name << ")" << std::endl;
|
||||||
|
hit_points = 100;
|
||||||
|
energy_points = 50;
|
||||||
|
attack_damage = 20;
|
||||||
|
this->name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScavTrap::~ScavTrap() { std::cout << "ScavTrap::~ScavTrap()" << std::endl; }
|
||||||
|
|
||||||
|
ScavTrap &ScavTrap::operator=(const ScavTrap &other) {
|
||||||
|
this->hit_points = other.hit_points;
|
||||||
|
this->energy_points = other.energy_points;
|
||||||
|
this->attack_damage = other.attack_damage;
|
||||||
|
this->name = other.name;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScavTrap::guardGate() {
|
||||||
|
std::cout << "ScavTrap " << name << " is now in gate-guarding mode."
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
16
ex03/ScavTrap.hpp
Normal file
16
ex03/ScavTrap.hpp
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef SCAVTRAP_HPP
|
||||||
|
#define SCAVTRAP_HPP
|
||||||
|
|
||||||
|
#include "ClapTrap.hpp"
|
||||||
|
|
||||||
|
class ScavTrap : public virtual ClapTrap {
|
||||||
|
public:
|
||||||
|
ScavTrap();
|
||||||
|
ScavTrap(std::string name);
|
||||||
|
~ScavTrap();
|
||||||
|
ScavTrap &operator=(const ScavTrap &other);
|
||||||
|
|
||||||
|
void guardGate();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
27
ex03/main.cpp
Normal file
27
ex03/main.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
#include "DiamondTrap.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
{
|
||||||
|
FragTrap fragtrap = FragTrap("Fragattor");
|
||||||
|
fragtrap.showStats();
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
{
|
||||||
|
ScavTrap scavtrap = ScavTrap("Scavenger");
|
||||||
|
scavtrap.showStats();
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
{
|
||||||
|
DiamondTrap diamondtrap = DiamondTrap("Dizzy");
|
||||||
|
diamondtrap.showStats();
|
||||||
|
diamondtrap.attack("training dummy");
|
||||||
|
diamondtrap.takeDamage(3);
|
||||||
|
diamondtrap.beRepaired(5);
|
||||||
|
diamondtrap.highFivesGuys();
|
||||||
|
diamondtrap.guardGate();
|
||||||
|
diamondtrap.whoAmI();
|
||||||
|
diamondtrap.showStats();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue