From efa91c0cb5e723cce35b2ae0d24020f0954ce475 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 19 Jul 2023 23:26:04 +0200 Subject: [PATCH] fix: ex03 --- ex03/src/HumanA.cpp | 20 ++------------------ ex03/src/HumanA.hpp | 5 +---- ex03/src/HumanB.cpp | 28 +++++++++------------------- ex03/src/HumanB.hpp | 5 +---- ex03/src/Weapon.cpp | 6 ++++-- ex03/src/Weapon.hpp | 2 +- ex03/src/main.cpp | 2 ++ 7 files changed, 20 insertions(+), 48 deletions(-) diff --git a/ex03/src/HumanA.cpp b/ex03/src/HumanA.cpp index ff0aeda..3aa4b30 100644 --- a/ex03/src/HumanA.cpp +++ b/ex03/src/HumanA.cpp @@ -3,34 +3,18 @@ #include #include -HumanA::HumanA(const std::string& name, const Weapon& weapon) +HumanA::HumanA(const std::string& name, const Weapon& weapon): + _weapon(weapon) { this->_name = name; - this->_weapon = weapon; -} - -HumanA::HumanA() -{ std::cout << "HumanA()" << std::endl; } -HumanA::HumanA(const HumanA& src) -{ - *this = src; -} - HumanA::~HumanA() { 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; diff --git a/ex03/src/HumanA.hpp b/ex03/src/HumanA.hpp index 268b092..06467ae 100644 --- a/ex03/src/HumanA.hpp +++ b/ex03/src/HumanA.hpp @@ -6,17 +6,14 @@ class HumanA { public: - HumanA(); HumanA(const std::string& name, const Weapon& weapon); HumanA(const HumanA& src); ~HumanA(); - HumanA& operator=(const HumanA& src); - void attack(); private: - Weapon _weapon; + const Weapon& _weapon; std::string _name; }; diff --git a/ex03/src/HumanB.cpp b/ex03/src/HumanB.cpp index 49eb6cf..20b40b4 100644 --- a/ex03/src/HumanB.cpp +++ b/ex03/src/HumanB.cpp @@ -4,18 +4,10 @@ #include HumanB::HumanB(const std::string& name) -{ - this->_name = name; -} - -HumanB::HumanB() { std::cout << "HumanB()" << std::endl; -} - -HumanB::HumanB(const HumanB& src) -{ - *this = src; + this->_name = name; + this->_weapon = NULL; } HumanB::~HumanB() @@ -23,21 +15,19 @@ HumanB::~HumanB() std::cout << "~HumanB()" << std::endl; } -HumanB& HumanB::operator=(const HumanB& src) -{ - 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; + std::cout << this->_name << " attacks with their "; + if (this->_weapon == NULL) + std::cout << "hands"; + else + std::cout << this->_weapon->getType(); + std::cout << std::endl; } void HumanB::setWeapon(const Weapon &weapon) { - this->_weapon = weapon; + this->_weapon = &weapon; } diff --git a/ex03/src/HumanB.hpp b/ex03/src/HumanB.hpp index 5d2baf5..8f4ff65 100644 --- a/ex03/src/HumanB.hpp +++ b/ex03/src/HumanB.hpp @@ -6,18 +6,15 @@ class HumanB { public: - HumanB(); HumanB(const std::string& name); HumanB(const HumanB& src); ~HumanB(); - HumanB& operator=(const HumanB& src); - void attack(); void setWeapon(const Weapon& weapon); private: - Weapon _weapon; + const Weapon* _weapon; std::string _name; }; diff --git a/ex03/src/Weapon.cpp b/ex03/src/Weapon.cpp index ffd371e..5f52b16 100644 --- a/ex03/src/Weapon.cpp +++ b/ex03/src/Weapon.cpp @@ -10,17 +10,19 @@ Weapon::Weapon() Weapon::Weapon(const Weapon& src) { + std::cout << "Weapon()" << std::endl; this->_type = src._type; } Weapon::Weapon(const std::string& type) { + std::cout << "Weapon()" << std::endl; this->_type = type; } Weapon::~Weapon() { - std::cout << "Weapon()" << std::endl; + std::cout << "~Weapon()" << std::endl; } Weapon& Weapon::operator=(const Weapon &src) @@ -34,7 +36,7 @@ void Weapon::setType(const std::string& newType) this->_type = newType; } -const std::string& Weapon::getType() +const std::string& Weapon::getType() const { return this->_type; } diff --git a/ex03/src/Weapon.hpp b/ex03/src/Weapon.hpp index 0230a99..33a4f77 100644 --- a/ex03/src/Weapon.hpp +++ b/ex03/src/Weapon.hpp @@ -12,7 +12,7 @@ class Weapon Weapon& operator=(const Weapon& src); - const std::string& getType(); + const std::string& getType() const; void setType(const std::string& newType); private: diff --git a/ex03/src/main.cpp b/ex03/src/main.cpp index 9a1723f..bfc33df 100644 --- a/ex03/src/main.cpp +++ b/ex03/src/main.cpp @@ -17,5 +17,7 @@ int main() club.setType("some other type of club"); jim.attack(); } + HumanB jim("Jim"); + jim.attack(); return 0; }