]> git.dkaiser.de - 42/cpp01.git/commitdiff
Implement HumanA class
authorDominik Kaiser <dkaiser@student.42heilbronn.de>
Mon, 10 Feb 2025 11:53:16 +0000 (12:53 +0100)
committerDominik Kaiser <dkaiser@student.42heilbronn.de>
Mon, 10 Feb 2025 11:53:16 +0000 (12:53 +0100)
ex03/HumanA.cpp
ex03/HumanA.hpp
ex03/Weapon.cpp
ex03/Weapon.hpp
ex03/main.cpp

index 38d2076977731dea5518469e8212bb7807ec20dd..84dec6142561a8ba57790aefb922dea9a27dac6c 100644 (file)
@@ -6,7 +6,19 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 12:00:00 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:00:02 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:51:24 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
+#include "HumanA.hpp"
+#include <iostream>
+
+HumanA::HumanA(const std::string &name, Weapon &weapon)
+    :weapon(&weapon),
+     name(name)
+{}
+
+void HumanA::attack(void) const
+{
+    std::cout << name << "attacks with their " << weapon->getType() << std::endl;
+}
index b373665889047636aecd06ad76f71072dc79697f..b127c67a993cfaba198ba5bbef6036e0ec586c82 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 11:59:28 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:25:54 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:51:15 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 class HumanA
 {
     public:
-        HumanA(const std::string &name, const Weapon &weapon);
+        HumanA(const std::string &name, Weapon &weapon);
         void attack(void) const;
     private:
-        Weapon weapon;
+        Weapon* weapon;
         std::string name;
 };
 
index e9dde06c114682b295888322d8d4ba1f46813e20..bed64b59ca877c264972157137b8800fee04ddcb 100644 (file)
@@ -6,13 +6,14 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 11:59:21 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:11:07 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:45:10 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "Weapon.hpp"
 
-Weapon::Weapon(void)
+Weapon::Weapon(const std::string &type)
+    :type(type)
 {}
 
 const std::string& Weapon::getType(void) const
index 917b367511e5bd27ce62ba3103f2506944b35441..945644cadc61f7d6d9b22e005082c773f4b9fd08 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 11:58:44 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:09:57 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:45:20 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,7 +18,7 @@
 class Weapon
 {
     public: 
-        Weapon(void);
+        Weapon(const std::string &type);
         const std::string& getType(void) const;
         void setType(const std::string &type);
     private:
index b2ae0011e3b3e412c813701e0c7c1ed8a851a6c5..e53d3a0c6830cf5fa65c1ef55aecd39fcf17c2ca 100644 (file)
@@ -6,9 +6,29 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 11:58:15 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 11:58:24 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:48:13 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-int main(void)
-{}
+#include "Weapon.hpp"
+#include "HumanA.hpp"
+
+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;
+}