diff options
Diffstat (limited to 'cpp04/ex01')
| -rw-r--r-- | cpp04/ex01/AWeapon.cpp | 46 | ||||
| -rw-r--r-- | cpp04/ex01/AWeapon.hpp | 13 | ||||
| -rw-r--r-- | cpp04/ex01/Character.cpp | 40 | ||||
| -rw-r--r-- | cpp04/ex01/Character.hpp | 8 | ||||
| -rw-r--r-- | cpp04/ex01/Enemy.cpp | 39 | ||||
| -rw-r--r-- | cpp04/ex01/Enemy.hpp | 11 | ||||
| -rw-r--r-- | cpp04/ex01/PlasmaRifle.cpp | 19 | ||||
| -rw-r--r-- | cpp04/ex01/PlasmaRifle.hpp | 4 | ||||
| -rw-r--r-- | cpp04/ex01/PowerFist.cpp | 19 | ||||
| -rw-r--r-- | cpp04/ex01/PowerFist.hpp | 4 | ||||
| -rw-r--r-- | cpp04/ex01/RadScorpion.cpp | 11 | ||||
| -rw-r--r-- | cpp04/ex01/RadScorpion.hpp | 4 | ||||
| -rw-r--r-- | cpp04/ex01/SuperMutant.cpp | 11 | ||||
| -rw-r--r-- | cpp04/ex01/SuperMutant.hpp | 4 | ||||
| -rw-r--r-- | cpp04/ex01/main.cpp | 52 |
15 files changed, 118 insertions, 167 deletions
diff --git a/cpp04/ex01/AWeapon.cpp b/cpp04/ex01/AWeapon.cpp index 9c53839..5d43cd5 100644 --- a/cpp04/ex01/AWeapon.cpp +++ b/cpp04/ex01/AWeapon.cpp @@ -6,48 +6,30 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 12:16:04 by charles #+# #+# */ -/* Updated: 2020/04/14 13:18:07 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:32:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "AWeapon.hpp" -AWeapon::AWeapon(AWeapon const& other) -{ - *this = other; -} - -void AWeapon::operator=(AWeapon const& other) -{ - m_name = other.m_name; - m_apcost = other.m_apcost; - m_damage = other.m_damage; -} - -AWeapon::~AWeapon() -{ -} - AWeapon::AWeapon(std::string const& name, int apcost, int damage) : m_name(name), m_apcost(apcost), m_damage(damage) -{ -} +{} -std::string const& AWeapon::getName() const -{ - return m_name; -} +AWeapon::AWeapon(AWeapon const& other) { *this = other; } -int AWeapon::getAPCost() const +AWeapon& AWeapon::operator=(AWeapon const& other) { - return m_apcost; + m_name = other.m_name; + m_apcost = other.m_apcost; + m_damage = other.m_damage; + return *this; } -int AWeapon::getDamage() const -{ - return m_damage; -} +AWeapon::~AWeapon() {} -AWeapon::AWeapon() -{ -} +std::string const& AWeapon::getName() const { return m_name; } +int AWeapon::getAPCost() const { return m_apcost; } +int AWeapon::getDamage() const { return m_damage; } + +AWeapon::AWeapon() {} diff --git a/cpp04/ex01/AWeapon.hpp b/cpp04/ex01/AWeapon.hpp index 468f966..0296cd5 100644 --- a/cpp04/ex01/AWeapon.hpp +++ b/cpp04/ex01/AWeapon.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 11:22:06 by charles #+# #+# */ -/* Updated: 2020/04/14 14:33:07 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:34:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ class AWeapon { public: AWeapon(AWeapon const& other); - void operator=(AWeapon const& other); + AWeapon& operator=(AWeapon const& other); virtual ~AWeapon(); AWeapon(std::string const& name, int apcost, int damage); @@ -29,10 +29,13 @@ public: virtual void attack() const = 0; protected: - AWeapon(); std::string m_name; - int m_apcost; - int m_damage; + int m_apcost; + int m_damage; + +private: + AWeapon(); + }; #endif diff --git a/cpp04/ex01/Character.cpp b/cpp04/ex01/Character.cpp index e06be50..5f865c7 100644 --- a/cpp04/ex01/Character.cpp +++ b/cpp04/ex01/Character.cpp @@ -6,29 +6,26 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:44:35 by charles #+# #+# */ -/* Updated: 2020/04/14 14:13:39 by charles ### ########.fr */ +/* Updated: 2020/11/12 14:28:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "Character.hpp" -Character::Character(Character const& other) -{ - *this = other; -} +Character::Character(std::string const& name) : + m_name(name), m_ap(40), m_weapon(NULL) {} -void Character::operator=(Character const& other) -{ - m_name = other.m_name; -} +Character::Character(Character const& other) { *this = other; } -Character::~Character() +Character& Character::operator=(Character const& other) { + m_name = other.m_name; + m_ap = other.m_ap; + m_weapon = other.m_weapon; + return *this; } -Character::Character(std::string const& name) : m_name(name), m_ap(40), m_weapon(NULL) -{ -} +Character::~Character() {} void Character::recoverAP() { @@ -54,20 +51,9 @@ void Character::attack(Enemy *enemy) m_ap -= m_weapon->getAPCost(); } -std::string const& Character::getName() const -{ - return m_name; -} - -int Character::getAP() const -{ - return m_ap; -} - -AWeapon* Character::getWeapon() const -{ - return m_weapon; -} +std::string const& Character::getName() const { return m_name; } +int Character::getAP() const { return m_ap; } +AWeapon* Character::getWeapon() const { return m_weapon; } std::ostream& operator<<(std::ostream& out, Character const& c) { diff --git a/cpp04/ex01/Character.hpp b/cpp04/ex01/Character.hpp index 04da9fc..817fd53 100644 --- a/cpp04/ex01/Character.hpp +++ b/cpp04/ex01/Character.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:42:25 by charles #+# #+# */ -/* Updated: 2020/04/14 14:09:51 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:56:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ class Character { public: Character(Character const& other); - void operator=(Character const& other); + Character& operator=(Character const& other); ~Character(); Character(std::string const& name); @@ -37,8 +37,8 @@ private: Character(); std::string m_name; - int m_ap; - AWeapon *m_weapon; + int m_ap; + AWeapon* m_weapon; }; std::ostream& operator<<(std::ostream& out, Character const& c); diff --git a/cpp04/ex01/Enemy.cpp b/cpp04/ex01/Enemy.cpp index 321ca68..c04f01b 100644 --- a/cpp04/ex01/Enemy.cpp +++ b/cpp04/ex01/Enemy.cpp @@ -6,49 +6,36 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:23:47 by charles #+# #+# */ -/* Updated: 2020/04/14 14:11:19 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:50:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "Enemy.hpp" -Enemy::Enemy(Enemy const& other) -{ - *this = other; -} +Enemy::Enemy(int hp, std::string const& type) : m_hp(hp), m_type(type) {} -void Enemy::operator=(Enemy const& other) -{ - m_hp = other.m_hp; - m_type = other.m_type; -} +Enemy::Enemy(Enemy const& other) { *this = other; } -Enemy::~Enemy() +Enemy& Enemy::operator=(Enemy const& other) { + m_hp = other.m_hp; + m_type = other.m_type; + return *this; } -Enemy::Enemy(int hp, std::string const& type) - : m_hp(hp), m_type(type) -{ -} +Enemy::~Enemy() {} -std::string const& Enemy::getType() const -{ - return m_type; -} -int Enemy::getHP() const -{ - return m_hp; -} +std::string const& Enemy::getType() const { return m_type; } +int Enemy::getHP() const { return m_hp; } void Enemy::takeDamage(int amount) { if (amount < 0) return; m_hp -= amount; + if (m_hp < 0) + m_hp = 0; } -Enemy::Enemy() -{ -} +Enemy::Enemy() {} diff --git a/cpp04/ex01/Enemy.hpp b/cpp04/ex01/Enemy.hpp index 582a61c..8b0dbf1 100644 --- a/cpp04/ex01/Enemy.hpp +++ b/cpp04/ex01/Enemy.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:23:40 by charles #+# #+# */ -/* Updated: 2020/04/14 14:13:07 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:55:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ class Enemy { public: Enemy(Enemy const& other); - void operator=(Enemy const& other); + Enemy& operator=(Enemy const& other); virtual ~Enemy(); Enemy(int hp, std::string const& type); @@ -30,10 +30,11 @@ public: virtual void takeDamage(int amount); protected: - Enemy(); - - int m_hp; + int m_hp; std::string m_type; + +private: + Enemy(); }; #endif diff --git a/cpp04/ex01/PlasmaRifle.cpp b/cpp04/ex01/PlasmaRifle.cpp index 6629905..fc6bddd 100644 --- a/cpp04/ex01/PlasmaRifle.cpp +++ b/cpp04/ex01/PlasmaRifle.cpp @@ -6,30 +6,23 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:07:24 by charles #+# #+# */ -/* Updated: 2020/04/14 13:18:53 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:35:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "PlasmaRifle.hpp" -PlasmaRifle::PlasmaRifle() - : AWeapon("Plasma Rifle", 5, 21) -{ -} +PlasmaRifle::PlasmaRifle() : AWeapon("Plasma Rifle", 5, 21) {} -PlasmaRifle::PlasmaRifle(PlasmaRifle const& other) -{ - *this = other; -} +PlasmaRifle::PlasmaRifle(PlasmaRifle const& other) : AWeapon(other) {} -void PlasmaRifle::operator=(PlasmaRifle const& other) +PlasmaRifle& PlasmaRifle::operator=(PlasmaRifle const& other) { AWeapon::operator=(other); + return *this; } -PlasmaRifle::~PlasmaRifle() -{ -} +PlasmaRifle::~PlasmaRifle() {} void PlasmaRifle::attack() const { diff --git a/cpp04/ex01/PlasmaRifle.hpp b/cpp04/ex01/PlasmaRifle.hpp index ef7caab..8ce7b0d 100644 --- a/cpp04/ex01/PlasmaRifle.hpp +++ b/cpp04/ex01/PlasmaRifle.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 12:15:47 by charles #+# #+# */ -/* Updated: 2020/04/14 14:04:39 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:36:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ class PlasmaRifle : public AWeapon public: PlasmaRifle(); PlasmaRifle(PlasmaRifle const& other); - void operator=(PlasmaRifle const& other); + PlasmaRifle& operator=(PlasmaRifle const& other); ~PlasmaRifle(); virtual void attack() const; diff --git a/cpp04/ex01/PowerFist.cpp b/cpp04/ex01/PowerFist.cpp index d14deba..2ae0337 100644 --- a/cpp04/ex01/PowerFist.cpp +++ b/cpp04/ex01/PowerFist.cpp @@ -6,30 +6,23 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:15:42 by charles #+# #+# */ -/* Updated: 2020/04/14 14:09:51 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:40:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "PowerFist.hpp" -PowerFist::PowerFist() - : AWeapon("Power Fist", 8, 50) -{ -} +PowerFist::PowerFist() : AWeapon("Power Fist", 8, 50) {} -PowerFist::PowerFist(PowerFist const& other) -{ - *this = other; -} +PowerFist::PowerFist(PowerFist const& other) : AWeapon(other) {} -void PowerFist::operator=(PowerFist const& other) +PowerFist& PowerFist::operator=(PowerFist const& other) { AWeapon::operator=(other); + return *this; } -PowerFist::~PowerFist() -{ -} +PowerFist::~PowerFist() {} void PowerFist::attack() const { diff --git a/cpp04/ex01/PowerFist.hpp b/cpp04/ex01/PowerFist.hpp index 2fbf212..100933a 100644 --- a/cpp04/ex01/PowerFist.hpp +++ b/cpp04/ex01/PowerFist.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:14:42 by charles #+# #+# */ -/* Updated: 2020/04/14 14:04:27 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:40:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ class PowerFist : public AWeapon public: PowerFist(); PowerFist(PowerFist const& other); - void operator=(PowerFist const& other); + PowerFist& operator=(PowerFist const& other); ~PowerFist(); virtual void attack() const; diff --git a/cpp04/ex01/RadScorpion.cpp b/cpp04/ex01/RadScorpion.cpp index 8422fb7..7e2ea84 100644 --- a/cpp04/ex01/RadScorpion.cpp +++ b/cpp04/ex01/RadScorpion.cpp @@ -6,27 +6,26 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:36:11 by charles #+# #+# */ -/* Updated: 2020/04/14 13:37:54 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:55:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "RadScorpion.hpp" -RadScorpion::RadScorpion() - : Enemy(80, "RadScorpion") +RadScorpion::RadScorpion() : Enemy(80, "RadScorpion") { std::cout << "* click click click *" << std::endl; } -RadScorpion::RadScorpion(RadScorpion const& other) +RadScorpion::RadScorpion(RadScorpion const& other) : Enemy(other) { - *this = other; std::cout << "* click click click *" << std::endl; } -void RadScorpion::operator=(RadScorpion const& other) +RadScorpion& RadScorpion::operator=(RadScorpion const& other) { Enemy::operator=(other); + return *this; } RadScorpion::~RadScorpion() diff --git a/cpp04/ex01/RadScorpion.hpp b/cpp04/ex01/RadScorpion.hpp index 71b98db..f07891e 100644 --- a/cpp04/ex01/RadScorpion.hpp +++ b/cpp04/ex01/RadScorpion.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:35:04 by charles #+# #+# */ -/* Updated: 2020/04/14 13:37:58 by charles ### ########.fr */ +/* Updated: 2020/11/12 14:24:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ class RadScorpion : public Enemy public: RadScorpion(); RadScorpion(RadScorpion const& other); - void operator=(RadScorpion const& other); + RadScorpion& operator=(RadScorpion const& other); ~RadScorpion(); private: diff --git a/cpp04/ex01/SuperMutant.cpp b/cpp04/ex01/SuperMutant.cpp index ba0a8a9..c9ce83c 100644 --- a/cpp04/ex01/SuperMutant.cpp +++ b/cpp04/ex01/SuperMutant.cpp @@ -6,27 +6,26 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:30:47 by charles #+# #+# */ -/* Updated: 2020/04/14 13:34:32 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:54:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "SuperMutant.hpp" -SuperMutant::SuperMutant() - : Enemy(170, "Super Mutant") +SuperMutant::SuperMutant() : Enemy(170, "Super Mutant") { std::cout << "Gaaah. Me want smash heads!" << std::endl; } -SuperMutant::SuperMutant(SuperMutant const& other) +SuperMutant::SuperMutant(SuperMutant const& other) : Enemy(other) { std::cout << "Gaaah. Me want smash heads!" << std::endl; - *this = other; } -void SuperMutant::operator=(SuperMutant const& other) +SuperMutant& SuperMutant::operator=(SuperMutant const& other) { Enemy::operator=(other); + return *this; } SuperMutant::~SuperMutant() diff --git a/cpp04/ex01/SuperMutant.hpp b/cpp04/ex01/SuperMutant.hpp index 1a890cc..64198de 100644 --- a/cpp04/ex01/SuperMutant.hpp +++ b/cpp04/ex01/SuperMutant.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 13:29:53 by charles #+# #+# */ -/* Updated: 2020/04/14 13:37:56 by charles ### ########.fr */ +/* Updated: 2020/11/12 13:50:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ class SuperMutant : public Enemy public: SuperMutant(); SuperMutant(SuperMutant const& other); - void operator=(SuperMutant const& other); + SuperMutant& operator=(SuperMutant const& other); ~SuperMutant(); virtual void takeDamage(int amount); diff --git a/cpp04/ex01/main.cpp b/cpp04/ex01/main.cpp index 6798b57..ca5b35e 100644 --- a/cpp04/ex01/main.cpp +++ b/cpp04/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 09:05:58 by charles #+# #+# */ -/* Updated: 2020/04/14 14:09:51 by charles ### ########.fr */ +/* Updated: 2020/11/12 14:36:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,27 +17,35 @@ int main() { - Character* me = new Character("me"); - - std::cout << *me; - - Enemy* b = new RadScorpion(); - - AWeapon* pr = new PlasmaRifle(); - AWeapon* pf = new PowerFist(); - - me->equip(pr); - std::cout << *me; - me->equip(pf); - - me->attack(b); - std::cout << *me; - me->equip(pr); - std::cout << *me; - me->attack(b); - std::cout << *me; - me->attack(b); - std::cout << *me; + { + std::cout << "================ SUBJECT MAIN =====================" << std::endl; + Character* me = new Character("me"); + std::cout << *me; + Enemy* b = new RadScorpion(); + AWeapon* pr = new PlasmaRifle(); + AWeapon* pf = new PowerFist(); + me->equip(pr); + std::cout << *me; + me->equip(pf); + me->attack(b); + std::cout << *me; + me->equip(pr); + std::cout << *me; + me->attack(b); + std::cout << *me; + me->attack(b); + std::cout << *me; + delete me; + ((RadScorpion*)b)->~RadScorpion(); + delete pr; + delete pf; + } + + std::cout << std::endl; + + { + + } return 0; } |
