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

index 84dec6142561a8ba57790aefb922dea9a27dac6c..e7b64110b03fe9dea876ca9e58949df7e22cef29 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 12:00:00 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:51:24 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:59:46 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -20,5 +20,6 @@ HumanA::HumanA(const std::string &name, Weapon &weapon)
 
 void HumanA::attack(void) const
 {
-    std::cout << name << "attacks with their " << weapon->getType() << std::endl;
+    std::cout << name << " attacks with their ";
+    std::cout << weapon->getType() << std::endl;
 }
index 4550c59c815b26ff9e0f1333ed18acd1736b699b..fad3f7898752e93e72a6746c07f75e1e87874995 100644 (file)
@@ -6,7 +6,24 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 12:00:11 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:01:05 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:59:59 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
+#include "HumanB.hpp"
+#include <iostream>
+
+HumanB::HumanB(const std::string &name)
+    :name(name)
+{}
+
+void HumanB::attack(void) const
+{
+    std::cout << name << " attacks with their ";
+    std::cout << weapon->getType() << std::endl;
+}
+
+void HumanB::setWeapon(Weapon &weapon)
+{
+    this->weapon = &weapon;
+}
index ce4edbdc4ddc05106686ababfb8c4085a0efcb19..c22b5c4395b2da6d1c21e3a057af4fdcdb560928 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 12:00:55 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:29:43 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:55:00 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,7 +21,7 @@ class HumanB
     public:
         HumanB(const std::string &name);
         void attack(void) const;
-        void setWeapon(Weapon *weapon);
+        void setWeapon(Weapon &weapon);
     private:
         Weapon* weapon;
         std::string name;
index e53d3a0c6830cf5fa65c1ef55aecd39fcf17c2ca..de69c158210ebe4dee958d3d44f6ccecb80e9730 100644 (file)
@@ -6,12 +6,13 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/02/10 11:58:15 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/02/10 12:48:13 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/02/10 12:58:13 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "Weapon.hpp"
 #include "HumanA.hpp"
+#include "HumanB.hpp"
 
 int main()
 {
@@ -22,13 +23,13 @@ int main()
         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;
+    {
+        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;
 }