Add solution to ex03 (without Makefile)
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 10 Oct 2024 11:52:48 +0000 (13:52 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 10 Oct 2024 11:52:48 +0000 (13:52 +0200)
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).

ex03/HumanA.cpp [new file with mode: 0644]
ex03/HumanA.h [new file with mode: 0644]
ex03/HumanB.cpp [new file with mode: 0644]
ex03/HumanB.h [new file with mode: 0644]
ex03/Weapon.cpp [new file with mode: 0644]
ex03/Weapon.h [new file with mode: 0644]
ex03/main.cpp [new file with mode: 0644]

diff --git a/ex03/HumanA.cpp b/ex03/HumanA.cpp
new file mode 100644 (file)
index 0000000..113cd57
--- /dev/null
@@ -0,0 +1,17 @@
+#include "HumanA.h"
+#include "Weapon.h"
+#include <string>
+#include <iostream>
+
+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 (file)
index 0000000..e8f4881
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef HUMANA_H
+# define HUMANA_H
+
+#include "Weapon.h"
+#include <string>
+
+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 (file)
index 0000000..2d0025a
--- /dev/null
@@ -0,0 +1,22 @@
+#include "HumanB.h"
+#include "Weapon.h"
+#include <string>
+#include <iostream>
+
+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 (file)
index 0000000..241ece7
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef HUMANB_H
+# define HUMANB_H
+
+#include "Weapon.h"
+#include <string>
+
+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 (file)
index 0000000..6acb37b
--- /dev/null
@@ -0,0 +1,31 @@
+#include "Weapon.h"
+#include <string>
+
+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 (file)
index 0000000..a53dad8
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef WEAPON_H
+# define WEAPON_H
+
+# include <string>
+
+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 (file)
index 0000000..2160600
--- /dev/null
@@ -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;
+}