recration of ex03

This commit is contained in:
starnakin 2023-07-17 21:35:02 +02:00
parent 98ece4839e
commit 3ef324e5e1
9 changed files with 142 additions and 85 deletions

View File

@ -1,25 +1,25 @@
CXX = c++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
OBJDIR = obj
BINDIR = bin
EXECUTABLE = Weapon
NAME = ex03
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(BINDIR)/$(EXECUTABLE)
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(BINDIR)/$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(BINDIR)/$(EXECUTABLE)
rm -fr $(NAME)
re: fclean all

View File

View File

@ -1,17 +1,37 @@
#include "HumanA.hpp"
#include "Weapon.hpp"
#include <string>
#include <iostream>
HumanA::HumanA(std::string name, Weapon &weapon) : weapon(weapon)
#include <iostream>
#include <string>
HumanA::HumanA(const std::string& name, const Weapon& weapon)
{
this->name = name;
this->_name = name;
this->_weapon = weapon;
}
HumanA::HumanA()
{
std::cout << "HumanA()" << std::endl;
}
HumanA::HumanA(const HumanA& src)
{
*this = src;
}
HumanA::~HumanA()
{}
void HumanA::attack()
{
std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl;
std::cout << "~HumanA()" << std::endl;
}
HumanA& HumanA::operator=(const HumanA& src)
{
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
void HumanA::attack()
{
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;
}

View File

@ -1,19 +1,22 @@
#ifndef HUMANA_HPP
# define HUMANA_HPP
# include "Weapon.hpp"
# include <string>
#pragma once
#include "Weapon.hpp"
#include <string>
class HumanA
{
private:
Weapon &weapon;
std::string name;
public:
HumanA(std::string name, Weapon &weapon);
public:
HumanA();
HumanA(const std::string& name, const Weapon& weapon);
HumanA(const HumanA& src);
~HumanA();
void attack();
};
HumanA& operator=(const HumanA& src);
#endif
void attack();
private:
Weapon _weapon;
std::string _name;
};

View File

@ -1,29 +1,43 @@
#include "HumanB.hpp"
#include "Weapon.hpp"
#include <cstddef>
#include <string>
#include <iostream>
HumanB::HumanB(std::string name)
#include <iostream>
#include <string>
HumanB::HumanB(const std::string& name)
{
this->weapon = NULL;
this->name = name;
this->_name = name;
}
HumanB::HumanB()
{
std::cout << "HumanB()" << std::endl;
}
HumanB::HumanB(const HumanB& src)
{
*this = src;
}
HumanB::~HumanB()
{}
void HumanB::attack()
{
if (this->weapon == NULL)
{
std::cerr << this->name << " can't attack without weapon" << std::endl;
return;
}
std::cout << this->name << " attacks with their " << this->weapon->getType() << std::endl;
std::cout << "~HumanB()" << std::endl;
}
void HumanB::setWeapon(Weapon& new_weapon)
HumanB& HumanB::operator=(const HumanB& src)
{
this->weapon = &new_weapon;
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
void HumanB::attack()
{
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;
}
void HumanB::setWeapon(const Weapon &weapon)
{
this->_weapon = weapon;
}

View File

@ -1,19 +1,23 @@
#ifndef HUMANB_HPP
# define HUMANB_HPP
# include "Weapon.hpp"
# include <string>
#pragma once
#include "Weapon.hpp"
#include <string>
class HumanB
{
private:
Weapon* weapon;
std::string name;
public:
HumanB(std::string name);
public:
HumanB();
HumanB(const std::string& name);
HumanB(const HumanB& src);
~HumanB();
void setWeapon(Weapon& new_weapon);
void attack();
HumanB& operator=(const HumanB& src);
void attack();
void setWeapon(const Weapon& weapon);
private:
Weapon _weapon;
std::string _name;
};
#endif

View File

@ -1,24 +1,40 @@
#include "Weapon.hpp"
#include <string>
Weapon::Weapon(std::string type)
{
this->type = type;
}
#include <iostream>
#include <string>
Weapon::Weapon()
{
std::cout << "Weapon()" << std::endl;
}
Weapon::Weapon(const Weapon& src)
{
this->_type = src._type;
}
Weapon::Weapon(const std::string& type)
{
this->_type = type;
}
Weapon::~Weapon()
{}
std::string Weapon::getType()
{
return (this->type);
std::cout << "Weapon()" << std::endl;
}
void Weapon::setType(std::string new_type)
Weapon& Weapon::operator=(const Weapon &src)
{
this->type = new_type;
this->_type = src._type;
return *this;
}
void Weapon::setType(const std::string& newType)
{
this->_type = newType;
}
const std::string& Weapon::getType()
{
return this->_type;
}

View File

@ -1,18 +1,20 @@
#ifndef WEAPON_HPP
# define WEAPON_HPP
# include <string>
#pragma once
#include <string>
class Weapon
{
private:
std::string type;
public:
Weapon(std::string type);
public:
Weapon();
Weapon(const Weapon& src);
Weapon(const std::string& type);
~Weapon();
std::string getType();
void setType(std::string new_type);
Weapon& operator=(const Weapon& src);
const std::string& getType();
void setType(const std::string& newType);
private:
std::string _type;
};
#endif

View File

@ -1,7 +1,5 @@
#include "Weapon.hpp"
#include "HumanA.hpp"
#include "HumanB.hpp"
int main()
{
{