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 <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->_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;

View File

@ -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;
};

View File

@ -4,18 +4,10 @@
#include <string>
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;
}

View File

@ -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;
};

View File

@ -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;
}

View File

@ -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:

View File

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