From d23b79b9bf2db28fd0e6465606bebe6d21f32a84 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 24 Oct 2024 10:03:17 +0200 Subject: [PATCH] Move some functions of OCF to private I was not sure whether the Orthodox Canonical Form dictates that the 4 functions be defined so I defined to be sure even though I did not think it makes sense for Claptrap to have a copy constructor and assignment or even a default constructor. I learned that the methods only need to be present (in OCF), but need not be usable. In C++11 I would delete the methods, in C++98 I have to place the declaration inside private. --- ex00/ClapTrap.cpp | 17 ----------------- ex00/ClapTrap.h | 8 +++++--- ex00/main.cpp | 22 +++++++++++----------- ex01/ClapTrap.cpp | 29 ++++++----------------------- ex01/ClapTrap.h | 8 +++++--- ex01/ScavTrap.cpp | 14 -------------- ex01/ScavTrap.h | 8 +++++--- ex01/main.cpp | 24 ++++++++++++------------ ex02/ClapTrap.cpp | 29 ++++++----------------------- ex02/ClapTrap.h | 8 +++++--- ex02/FragTrap.cpp | 14 -------------- ex02/FragTrap.h | 9 ++++++--- ex02/ScavTrap.cpp | 14 -------------- ex02/ScavTrap.h | 8 +++++--- ex02/main.cpp | 44 ++++++++++++++++++++++---------------------- 15 files changed, 88 insertions(+), 168 deletions(-) diff --git a/ex00/ClapTrap.cpp b/ex00/ClapTrap.cpp index 47b25ba..9fb2e9d 100644 --- a/ex00/ClapTrap.cpp +++ b/ex00/ClapTrap.cpp @@ -8,28 +8,11 @@ ClapTrap::ClapTrap(std::string name): std::cout << "ClapTrap " << m_name << " has spawned.\n"; } -ClapTrap::ClapTrap(const ClapTrap &other) -{ - std::cout << "ClapTrap " << m_name << " has spawned.\n"; - *this = other; -} - ClapTrap::~ClapTrap() { std::cout << "ClapTrap " << m_name << " has despawned.\n"; } -ClapTrap &ClapTrap::operator=(const ClapTrap &other) -{ - if (this == &other) - return (*this); - m_name = other.m_name; - m_hp = other.m_hp; - m_energy = other.m_energy; - m_attack = other.m_attack; - return (*this); -} - void ClapTrap::attack(const std::string &target) { if (m_hp == 0) diff --git a/ex00/ClapTrap.h b/ex00/ClapTrap.h index aa781d1..2edecfe 100644 --- a/ex00/ClapTrap.h +++ b/ex00/ClapTrap.h @@ -11,13 +11,15 @@ class ClapTrap unsigned int m_energy; unsigned int m_attack; - public: - ClapTrap(std::string name = "DEFAULT"); + ClapTrap(); ClapTrap(const ClapTrap &other); - ~ClapTrap(); ClapTrap &operator=(const ClapTrap &other); + public: + ClapTrap(std::string name); + ~ClapTrap(); + void attack(const std::string &target); void takeDamage(unsigned int amount); void beRepaired(unsigned int amount); diff --git a/ex00/main.cpp b/ex00/main.cpp index 9c9b676..e1bfd1f 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -3,18 +3,18 @@ int main() { ClapTrap a("Alen"); - ClapTrap def; + ClapTrap b("Boris"); - def.attack("foo"); - def.takeDamage(9); - def.takeDamage(0); - def.beRepaired(6); - def.takeDamage(5); - def.takeDamage(1); - def.takeDamage(1); - def.takeDamage(1); - def.attack("foo"); - def.beRepaired(10); + b.attack("foo"); + b.takeDamage(9); + b.takeDamage(0); + b.beRepaired(6); + b.takeDamage(5); + b.takeDamage(1); + b.takeDamage(1); + b.takeDamage(1); + b.attack("foo"); + b.beRepaired(10); for (int i(0); i < 10; ++i) a.attack("bar"); a.takeDamage(9); diff --git a/ex01/ClapTrap.cpp b/ex01/ClapTrap.cpp index 390ca63..9fb2e9d 100644 --- a/ex01/ClapTrap.cpp +++ b/ex01/ClapTrap.cpp @@ -8,28 +8,11 @@ ClapTrap::ClapTrap(std::string name): std::cout << "ClapTrap " << m_name << " has spawned.\n"; } -ClapTrap::ClapTrap(const ClapTrap &other) -{ - std::cout << "ClapTrap " << m_name << " has spawned.\n"; - *this = other; -} - ClapTrap::~ClapTrap() { std::cout << "ClapTrap " << m_name << " has despawned.\n"; } -ClapTrap &ClapTrap::operator=(const ClapTrap &other) -{ - if (this == &other) - return (*this); - m_name = other.m_name; - m_hp = other.m_hp; - m_energy = other.m_energy; - m_attack = other.m_attack; - return (*this); -} - void ClapTrap::attack(const std::string &target) { if (m_hp == 0) @@ -49,17 +32,17 @@ void ClapTrap::attack(const std::string &target) void ClapTrap::takeDamage(unsigned int amount) { if (m_hp == 0) - std::cout << m_name << + std::cout << "ClapTrap " << m_name << " is already dead and cannot be damaged further.\n"; else if (amount >= m_hp) { - std::cout << m_name + std::cout << "ClapTrap " << m_name << " takes " << amount << " damage and dies\n"; m_hp = 0; } else { - std::cout << m_name + std::cout << "ClapTrap " << m_name << " takes " << amount << " damage.\n"; m_hp -= amount; } @@ -68,14 +51,14 @@ void ClapTrap::takeDamage(unsigned int amount) void ClapTrap::beRepaired(unsigned int amount) { if (m_hp == 0) - std::cout << m_name + std::cout << "ClapTrap " << m_name << " is dead, hence cannot repair itself.\n"; else if (m_energy == 0) - std::cout << m_name + std::cout << "ClapTrap " << m_name << " does not have enough energy to repair itself.\n"; else { - std::cout << m_name + std::cout << "ClapTrap " << m_name << " repairs iself for " << amount << " hit points.\n"; m_hp += amount; } diff --git a/ex01/ClapTrap.h b/ex01/ClapTrap.h index 59e7d33..cb2c736 100644 --- a/ex01/ClapTrap.h +++ b/ex01/ClapTrap.h @@ -11,13 +11,15 @@ class ClapTrap unsigned int m_energy; unsigned int m_attack; - public: - ClapTrap(std::string name = "DEFAULT"); + ClapTrap(); ClapTrap(const ClapTrap &other); - ~ClapTrap(); ClapTrap &operator=(const ClapTrap &other); + public: + ClapTrap(std::string name); + ~ClapTrap(); + void attack(const std::string &target); void takeDamage(unsigned int amount); void beRepaired(unsigned int amount); diff --git a/ex01/ScavTrap.cpp b/ex01/ScavTrap.cpp index f867bbe..7ca746f 100644 --- a/ex01/ScavTrap.cpp +++ b/ex01/ScavTrap.cpp @@ -12,25 +12,11 @@ ScavTrap::ScavTrap(std::string name): ClapTrap(name) m_is_guarding = 0; } -ScavTrap::ScavTrap(const ScavTrap &other) -{ - *this = other; -} - ScavTrap::~ScavTrap() { std::cout << "ScavTrap " << m_name << " has despawned.\n"; } -ScavTrap &ScavTrap::operator=(const ScavTrap &other) -{ - if (this == &other) - return (*this); - ClapTrap::operator=(other); - m_is_guarding = other.m_is_guarding; - return (*this); -} - void ScavTrap::attack(const std::string &target) { if (m_hp == 0) diff --git a/ex01/ScavTrap.h b/ex01/ScavTrap.h index 4a538e5..b7664f2 100644 --- a/ex01/ScavTrap.h +++ b/ex01/ScavTrap.h @@ -9,13 +9,15 @@ class ScavTrap : public ClapTrap private: bool m_is_guarding; - public: - ScavTrap(std::string name = "DEFAULT"); + ScavTrap(); ScavTrap(const ScavTrap &other); - ~ScavTrap(); ScavTrap &operator=(const ScavTrap &other); + public: + ScavTrap(std::string name); + ~ScavTrap(); + void attack(const std::string &target); void guardGate(); }; diff --git a/ex01/main.cpp b/ex01/main.cpp index 2f16fe4..38dcebb 100644 --- a/ex01/main.cpp +++ b/ex01/main.cpp @@ -3,19 +3,19 @@ int main() { - ScavTrap a("Alen"); - ClapTrap def; + ClapTrap a("Alen"); + ScavTrap b("Boris"); - def.attack("foo"); - def.takeDamage(9); - def.takeDamage(0); - def.beRepaired(6); - def.takeDamage(5); - def.takeDamage(1); - def.takeDamage(1); - def.takeDamage(1); - def.attack("foo"); - def.beRepaired(10); + b.attack("foo"); + b.takeDamage(9); + b.takeDamage(0); + b.beRepaired(6); + b.takeDamage(5); + b.takeDamage(1); + b.takeDamage(1); + b.takeDamage(1); + b.attack("foo"); + b.beRepaired(10); for (int i(0); i < 50; ++i) a.attack("bar"); a.takeDamage(9); diff --git a/ex02/ClapTrap.cpp b/ex02/ClapTrap.cpp index 390ca63..9fb2e9d 100644 --- a/ex02/ClapTrap.cpp +++ b/ex02/ClapTrap.cpp @@ -8,28 +8,11 @@ ClapTrap::ClapTrap(std::string name): std::cout << "ClapTrap " << m_name << " has spawned.\n"; } -ClapTrap::ClapTrap(const ClapTrap &other) -{ - std::cout << "ClapTrap " << m_name << " has spawned.\n"; - *this = other; -} - ClapTrap::~ClapTrap() { std::cout << "ClapTrap " << m_name << " has despawned.\n"; } -ClapTrap &ClapTrap::operator=(const ClapTrap &other) -{ - if (this == &other) - return (*this); - m_name = other.m_name; - m_hp = other.m_hp; - m_energy = other.m_energy; - m_attack = other.m_attack; - return (*this); -} - void ClapTrap::attack(const std::string &target) { if (m_hp == 0) @@ -49,17 +32,17 @@ void ClapTrap::attack(const std::string &target) void ClapTrap::takeDamage(unsigned int amount) { if (m_hp == 0) - std::cout << m_name << + std::cout << "ClapTrap " << m_name << " is already dead and cannot be damaged further.\n"; else if (amount >= m_hp) { - std::cout << m_name + std::cout << "ClapTrap " << m_name << " takes " << amount << " damage and dies\n"; m_hp = 0; } else { - std::cout << m_name + std::cout << "ClapTrap " << m_name << " takes " << amount << " damage.\n"; m_hp -= amount; } @@ -68,14 +51,14 @@ void ClapTrap::takeDamage(unsigned int amount) void ClapTrap::beRepaired(unsigned int amount) { if (m_hp == 0) - std::cout << m_name + std::cout << "ClapTrap " << m_name << " is dead, hence cannot repair itself.\n"; else if (m_energy == 0) - std::cout << m_name + std::cout << "ClapTrap " << m_name << " does not have enough energy to repair itself.\n"; else { - std::cout << m_name + std::cout << "ClapTrap " << m_name << " repairs iself for " << amount << " hit points.\n"; m_hp += amount; } diff --git a/ex02/ClapTrap.h b/ex02/ClapTrap.h index 59e7d33..cb2c736 100644 --- a/ex02/ClapTrap.h +++ b/ex02/ClapTrap.h @@ -11,13 +11,15 @@ class ClapTrap unsigned int m_energy; unsigned int m_attack; - public: - ClapTrap(std::string name = "DEFAULT"); + ClapTrap(); ClapTrap(const ClapTrap &other); - ~ClapTrap(); ClapTrap &operator=(const ClapTrap &other); + public: + ClapTrap(std::string name); + ~ClapTrap(); + void attack(const std::string &target); void takeDamage(unsigned int amount); void beRepaired(unsigned int amount); diff --git a/ex02/FragTrap.cpp b/ex02/FragTrap.cpp index 882213e..9bd67b9 100644 --- a/ex02/FragTrap.cpp +++ b/ex02/FragTrap.cpp @@ -12,25 +12,11 @@ FragTrap::FragTrap(std::string name): ClapTrap(name) m_is_guarding = 0; } -FragTrap::FragTrap(const FragTrap &other) -{ - *this = other; -} - FragTrap::~FragTrap() { std::cout << "FragTrap " << m_name << " has despawned.\n"; } -FragTrap &FragTrap::operator=(const FragTrap &other) -{ - if (this == &other) - return (*this); - ClapTrap::operator=(other); - m_is_guarding = other.m_is_guarding; - return (*this); -} - void FragTrap::attack(const std::string &target) { if (m_hp == 0) diff --git a/ex02/FragTrap.h b/ex02/FragTrap.h index 64984d3..c1bbc52 100644 --- a/ex02/FragTrap.h +++ b/ex02/FragTrap.h @@ -9,13 +9,16 @@ class FragTrap : public ClapTrap private: bool m_is_guarding; - public: - FragTrap(std::string name = "DEFAULT"); + FragTrap(); FragTrap(const FragTrap &other); - ~FragTrap(); FragTrap &operator=(const FragTrap &other); + public: + FragTrap(std::string name); + ~FragTrap(); + + void attack(const std::string &target); void highFivesGuys(); }; diff --git a/ex02/ScavTrap.cpp b/ex02/ScavTrap.cpp index f867bbe..7ca746f 100644 --- a/ex02/ScavTrap.cpp +++ b/ex02/ScavTrap.cpp @@ -12,25 +12,11 @@ ScavTrap::ScavTrap(std::string name): ClapTrap(name) m_is_guarding = 0; } -ScavTrap::ScavTrap(const ScavTrap &other) -{ - *this = other; -} - ScavTrap::~ScavTrap() { std::cout << "ScavTrap " << m_name << " has despawned.\n"; } -ScavTrap &ScavTrap::operator=(const ScavTrap &other) -{ - if (this == &other) - return (*this); - ClapTrap::operator=(other); - m_is_guarding = other.m_is_guarding; - return (*this); -} - void ScavTrap::attack(const std::string &target) { if (m_hp == 0) diff --git a/ex02/ScavTrap.h b/ex02/ScavTrap.h index 4a538e5..b7664f2 100644 --- a/ex02/ScavTrap.h +++ b/ex02/ScavTrap.h @@ -9,13 +9,15 @@ class ScavTrap : public ClapTrap private: bool m_is_guarding; - public: - ScavTrap(std::string name = "DEFAULT"); + ScavTrap(); ScavTrap(const ScavTrap &other); - ~ScavTrap(); ScavTrap &operator=(const ScavTrap &other); + public: + ScavTrap(std::string name); + ~ScavTrap(); + void attack(const std::string &target); void guardGate(); }; diff --git a/ex02/main.cpp b/ex02/main.cpp index 03093ee..94e005c 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -4,30 +4,30 @@ int main() { - ScavTrap a("Alen"); - ClapTrap def; - FragTrap b("Glenn"); + ClapTrap a("Alen"); + ScavTrap b("Boris"); + FragTrap c("Charles"); - def.attack("foo"); - def.takeDamage(9); - def.takeDamage(0); - def.beRepaired(6); - def.takeDamage(5); - def.takeDamage(1); - def.takeDamage(1); - def.takeDamage(1); - def.attack("foo"); - def.beRepaired(10); + a.attack("foo"); a.takeDamage(9); - a.guardGate(); - a.guardGate(); - a.attack("bar"); - a.beRepaired(1); - a.takeDamage(100); + a.takeDamage(0); + a.beRepaired(6); + a.takeDamage(5); a.takeDamage(1); - a.guardGate(); - a.attack("bar"); - a.beRepaired(1); - b.highFivesGuys(); + a.takeDamage(1); + a.takeDamage(1); + a.attack("foo"); + a.beRepaired(10); + b.takeDamage(9); + b.guardGate(); + b.guardGate(); + b.attack("bar"); + b.beRepaired(1); + b.takeDamage(100); + b.takeDamage(1); + b.guardGate(); + b.attack("bar"); + b.beRepaired(1); + c.highFivesGuys(); return (0); } -- 2.30.2