From: Lukas Jiriste Date: Thu, 10 Oct 2024 11:52:48 +0000 (+0200) Subject: Add solution to ex03 (without Makefile) X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=81fe5b531f5b54b2de6fca083ef6453d39cb6ad7;p=42%2FCPP_Module_01 Add solution to ex03 (without Makefile) A class that holds a reference cannot abide to orthodox canonical form as the reference has to be assigned something non-temporary (which cannot be created inside constructor except for on heap). --- diff --git a/ex03/HumanA.cpp b/ex03/HumanA.cpp new file mode 100644 index 0000000..113cd57 --- /dev/null +++ b/ex03/HumanA.cpp @@ -0,0 +1,17 @@ +#include "HumanA.h" +#include "Weapon.h" +#include +#include + +HumanA::HumanA(std::string name, Weapon &weapon): m_name(name), m_weapon(weapon) +{ +} + +HumanA::~HumanA() +{ +} + +void HumanA::attack(void) +{ + std::cout << m_name << " attacks with their " << m_weapon.getType() << '\n'; +} diff --git a/ex03/HumanA.h b/ex03/HumanA.h new file mode 100644 index 0000000..e8f4881 --- /dev/null +++ b/ex03/HumanA.h @@ -0,0 +1,20 @@ +#ifndef HUMANA_H +# define HUMANA_H + +#include "Weapon.h" +#include + +class HumanA +{ + private: + std::string m_name; + Weapon &m_weapon; + + public: + HumanA(std::string name, Weapon &weapon); + ~HumanA(); + + void attack(void); +}; + +#endif // HUMANA_H diff --git a/ex03/HumanB.cpp b/ex03/HumanB.cpp new file mode 100644 index 0000000..2d0025a --- /dev/null +++ b/ex03/HumanB.cpp @@ -0,0 +1,22 @@ +#include "HumanB.h" +#include "Weapon.h" +#include +#include + +HumanB::HumanB(std::string name, Weapon *weapon): m_name(name), m_weapon(weapon) +{ +} + +HumanB::~HumanB() +{ +} + +void HumanB::setWeapon(Weapon &weapon) +{ + m_weapon = &weapon; +} + +void HumanB::attack(void) +{ + std::cout << m_name << " attacks with their " << m_weapon->getType() << '\n'; +} diff --git a/ex03/HumanB.h b/ex03/HumanB.h new file mode 100644 index 0000000..241ece7 --- /dev/null +++ b/ex03/HumanB.h @@ -0,0 +1,21 @@ +#ifndef HUMANB_H +# define HUMANB_H + +#include "Weapon.h" +#include + +class HumanB +{ + private: + std::string m_name; + Weapon *m_weapon; + + public: + HumanB(std::string name = "human", Weapon *weapon = NULL); + ~HumanB(); + + void setWeapon(Weapon &weapon); + void attack(void); +}; + +#endif // HUMANB_H diff --git a/ex03/Weapon.cpp b/ex03/Weapon.cpp new file mode 100644 index 0000000..6acb37b --- /dev/null +++ b/ex03/Weapon.cpp @@ -0,0 +1,31 @@ +#include "Weapon.h" +#include + +Weapon::Weapon(std::string type): m_type(type) +{ +} + +Weapon::Weapon(const Weapon &weapon) +{ + *this = weapon; +} + +Weapon::~Weapon() +{ +} + +Weapon &Weapon::operator=(const Weapon &weapon) +{ + this->m_type = weapon.m_type; + return (*this); +} + +const std::string &Weapon::getType(void) +{ + return (m_type); +} + +void Weapon::setType(std::string type) +{ + m_type = type; +} diff --git a/ex03/Weapon.h b/ex03/Weapon.h new file mode 100644 index 0000000..a53dad8 --- /dev/null +++ b/ex03/Weapon.h @@ -0,0 +1,21 @@ +#ifndef WEAPON_H +# define WEAPON_H + +# include + +class Weapon +{ + private: + std::string m_type; + + public: + Weapon(std::string type = "none"); + Weapon(const Weapon &weapon); + ~Weapon(); + + Weapon &operator=(const Weapon &weapon); + const std::string &getType(void); + void setType(std::string type); +}; + +#endif // WEAPON_H diff --git a/ex03/main.cpp b/ex03/main.cpp new file mode 100644 index 0000000..2160600 --- /dev/null +++ b/ex03/main.cpp @@ -0,0 +1,23 @@ +#include "HumanA.h" +#include "HumanB.h" +#include "Weapon.h" + +int main() +{ + { + Weapon club = Weapon("crude spiked club"); + HumanA bob("Bob", club); + bob.attack(); + club.setType("some other type of club"); + bob.attack(); + } + { + Weapon club = Weapon("crude spiked club"); + HumanB jim("Jim"); + jim.setWeapon(club); + jim.attack(); + club.setType("some other type of club"); + jim.attack(); + } + return 0; +}