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

View File

View File

@ -1,17 +1,37 @@
#include "HumanA.hpp" #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() 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 #pragma once
# define HUMANA_HPP
# include "Weapon.hpp" #include "Weapon.hpp"
# include <string> #include <string>
class HumanA class HumanA
{ {
private: public:
Weapon &weapon; HumanA();
std::string name; HumanA(const std::string& name, const Weapon& weapon);
HumanA(const HumanA& src);
public:
HumanA(std::string name, Weapon &weapon);
~HumanA(); ~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 "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() HumanB::~HumanB()
{}
void HumanB::attack()
{ {
if (this->weapon == NULL) std::cout << "~HumanB()" << std::endl;
{
std::cerr << this->name << " can't attack without weapon" << std::endl;
return;
}
std::cout << this->name << " attacks with their " << this->weapon->getType() << 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 #pragma once
# define HUMANB_HPP
# include "Weapon.hpp" #include "Weapon.hpp"
# include <string> #include <string>
class HumanB class HumanB
{ {
private: public:
Weapon* weapon; HumanB();
std::string name; HumanB(const std::string& name);
HumanB(const HumanB& src);
public:
HumanB(std::string name);
~HumanB(); ~HumanB();
void setWeapon(Weapon& new_weapon); HumanB& operator=(const HumanB& src);
void attack();
void attack();
void setWeapon(const Weapon& weapon);
private:
Weapon _weapon;
std::string _name;
}; };
#endif

View File

@ -1,24 +1,40 @@
#include "Weapon.hpp" #include "Weapon.hpp"
#include <string>
Weapon::Weapon(std::string type) #include <iostream>
{ #include <string>
this->type = type;
}
Weapon::Weapon() 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() 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 #pragma once
# define WEAPON_HPP
# include <string> #include <string>
class Weapon class Weapon
{ {
private: public:
std::string type;
public:
Weapon(std::string type);
Weapon(); Weapon();
Weapon(const Weapon& src);
Weapon(const std::string& type);
~Weapon(); ~Weapon();
std::string getType(); Weapon& operator=(const Weapon& src);
void setType(std::string new_type);
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 "HumanA.hpp"
#include "HumanB.hpp" #include "HumanB.hpp"
int main() int main()
{ {
{ {