fix: ex03

This commit is contained in:
starnakin 2023-07-19 23:26:04 +02:00
parent eb2b035b75
commit efa91c0cb5
7 changed files with 20 additions and 48 deletions

View File

@ -3,34 +3,18 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
HumanA::HumanA(const std::string& name, const Weapon& weapon) HumanA::HumanA(const std::string& name, const Weapon& weapon):
_weapon(weapon)
{ {
this->_name = name; this->_name = name;
this->_weapon = weapon;
}
HumanA::HumanA()
{
std::cout << "HumanA()" << std::endl; std::cout << "HumanA()" << std::endl;
} }
HumanA::HumanA(const HumanA& src)
{
*this = src;
}
HumanA::~HumanA() HumanA::~HumanA()
{ {
std::cout << "~HumanA()" << std::endl; std::cout << "~HumanA()" << std::endl;
} }
HumanA& HumanA::operator=(const HumanA& src)
{
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
void HumanA::attack() void HumanA::attack()
{ {
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl; std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;

View File

@ -6,17 +6,14 @@
class HumanA class HumanA
{ {
public: public:
HumanA();
HumanA(const std::string& name, const Weapon& weapon); HumanA(const std::string& name, const Weapon& weapon);
HumanA(const HumanA& src); HumanA(const HumanA& src);
~HumanA(); ~HumanA();
HumanA& operator=(const HumanA& src);
void attack(); void attack();
private: private:
Weapon _weapon; const Weapon& _weapon;
std::string _name; std::string _name;
}; };

View File

@ -4,18 +4,10 @@
#include <string> #include <string>
HumanB::HumanB(const std::string& name) HumanB::HumanB(const std::string& name)
{
this->_name = name;
}
HumanB::HumanB()
{ {
std::cout << "HumanB()" << std::endl; std::cout << "HumanB()" << std::endl;
} this->_name = name;
this->_weapon = NULL;
HumanB::HumanB(const HumanB& src)
{
*this = src;
} }
HumanB::~HumanB() HumanB::~HumanB()
@ -23,21 +15,19 @@ HumanB::~HumanB()
std::cout << "~HumanB()" << std::endl; std::cout << "~HumanB()" << std::endl;
} }
HumanB& HumanB::operator=(const HumanB& src)
{
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
void HumanB::attack() 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) void HumanB::setWeapon(const Weapon &weapon)
{ {
this->_weapon = weapon; this->_weapon = &weapon;
} }

View File

@ -6,18 +6,15 @@
class HumanB class HumanB
{ {
public: public:
HumanB();
HumanB(const std::string& name); HumanB(const std::string& name);
HumanB(const HumanB& src); HumanB(const HumanB& src);
~HumanB(); ~HumanB();
HumanB& operator=(const HumanB& src);
void attack(); void attack();
void setWeapon(const Weapon& weapon); void setWeapon(const Weapon& weapon);
private: private:
Weapon _weapon; const Weapon* _weapon;
std::string _name; std::string _name;
}; };

View File

@ -10,17 +10,19 @@ Weapon::Weapon()
Weapon::Weapon(const Weapon& src) Weapon::Weapon(const Weapon& src)
{ {
std::cout << "Weapon()" << std::endl;
this->_type = src._type; this->_type = src._type;
} }
Weapon::Weapon(const std::string& type) Weapon::Weapon(const std::string& type)
{ {
std::cout << "Weapon()" << std::endl;
this->_type = type; this->_type = type;
} }
Weapon::~Weapon() Weapon::~Weapon()
{ {
std::cout << "Weapon()" << std::endl; std::cout << "~Weapon()" << std::endl;
} }
Weapon& Weapon::operator=(const Weapon &src) Weapon& Weapon::operator=(const Weapon &src)
@ -34,7 +36,7 @@ void Weapon::setType(const std::string& newType)
this->_type = newType; this->_type = newType;
} }
const std::string& Weapon::getType() const std::string& Weapon::getType() const
{ {
return this->_type; return this->_type;
} }

View File

@ -12,7 +12,7 @@ class Weapon
Weapon& operator=(const Weapon& src); Weapon& operator=(const Weapon& src);
const std::string& getType(); const std::string& getType() const;
void setType(const std::string& newType); void setType(const std::string& newType);
private: private:

View File

@ -17,5 +17,7 @@ int main()
club.setType("some other type of club"); club.setType("some other type of club");
jim.attack(); jim.attack();
} }
HumanB jim("Jim");
jim.attack();
return 0; return 0;
} }