From b799c007a1b6911fcbe5141429ea541e1277ebdd Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 9 Nov 2020 11:26:50 +0100 Subject: Fixing some edge cases in cpp00 and cpp01, Updated formatting --- cpp01/ex03/Zombie.cpp | 9 +++++++-- cpp01/ex03/Zombie.hpp | 5 +++-- cpp01/ex03/ZombieHorde.cpp | 12 +++++++++++- cpp01/ex03/ZombieHorde.hpp | 7 ++++--- cpp01/ex03/main.cpp | 30 ++++++++++++++++++++++++++---- 5 files changed, 51 insertions(+), 12 deletions(-) (limited to 'cpp01/ex03') diff --git a/cpp01/ex03/Zombie.cpp b/cpp01/ex03/Zombie.cpp index 40fb849..79f6ad2 100644 --- a/cpp01/ex03/Zombie.cpp +++ b/cpp01/ex03/Zombie.cpp @@ -6,19 +6,24 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:38:16 by charles #+# #+# */ -/* Updated: 2020/04/13 09:40:20 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:38:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "Zombie.hpp" -Zombie::Zombie(std::string name, std::string type) +Zombie::Zombie(std::string const& name, std::string const& type) : m_name(name), m_type(type) { announce(); } +Zombie::~Zombie() +{ + std::cout << "<" << m_name << " (" << m_type << ")> AAAAARG... ME DYING" << std::endl; +} + void Zombie::announce() { std::cout << "<" << m_name << " (" << m_type << ")> Braiiiiiiinnnssss..." << std::endl; diff --git a/cpp01/ex03/Zombie.hpp b/cpp01/ex03/Zombie.hpp index 5106c8f..e87200e 100644 --- a/cpp01/ex03/Zombie.hpp +++ b/cpp01/ex03/Zombie.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:40:26 by charles #+# #+# */ -/* Updated: 2020/04/13 09:40:49 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:38:52 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,8 @@ class Zombie { public: - Zombie(std::string name, std::string type); + Zombie(std::string const& name, std::string const& type); + ~Zombie(); void announce(); private: diff --git a/cpp01/ex03/ZombieHorde.cpp b/cpp01/ex03/ZombieHorde.cpp index 955355b..0492fb3 100644 --- a/cpp01/ex03/ZombieHorde.cpp +++ b/cpp01/ex03/ZombieHorde.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:52:11 by charles #+# #+# */ -/* Updated: 2020/04/13 10:02:01 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:57:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,12 @@ ZombieHorde::ZombieHorde(int n) : m_size(n) { + if (n < 0) + { + std::cerr << "Error: n should be > 0" << std::endl; + m_horde = NULL; + return; + } std::string name_pool[10] = { "Jordan", "Mr.poopybutthole", @@ -40,6 +46,8 @@ ZombieHorde::ZombieHorde(int n) ZombieHorde::~ZombieHorde() { + if (m_horde == NULL) + return; for (size_t i = 0; i < m_size; i++) delete m_horde[i]; delete [] m_horde; @@ -47,6 +55,8 @@ ZombieHorde::~ZombieHorde() void ZombieHorde::announce() { + if (m_horde == NULL) + return; for (size_t i = 0; i < m_size; i++) m_horde[i]->announce(); } diff --git a/cpp01/ex03/ZombieHorde.hpp b/cpp01/ex03/ZombieHorde.hpp index 991c2ec..bb6bbde 100644 --- a/cpp01/ex03/ZombieHorde.hpp +++ b/cpp01/ex03/ZombieHorde.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:52:18 by charles #+# #+# */ -/* Updated: 2020/04/13 09:59:53 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:53:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,17 +15,18 @@ # include # include +# include # include "Zombie.hpp" class ZombieHorde { public: - ZombieHorde(int n); + ZombieHorde(int n); // subject wants int ~ZombieHorde(); void announce(); private: - size_t m_size; + size_t m_size; Zombie** m_horde; }; diff --git a/cpp01/ex03/main.cpp b/cpp01/ex03/main.cpp index 20db57c..cef8577 100644 --- a/cpp01/ex03/main.cpp +++ b/cpp01/ex03/main.cpp @@ -6,25 +6,47 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:56:14 by charles #+# #+# */ -/* Updated: 2020/04/13 09:58:56 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:57:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include #include "Zombie.hpp" #include "ZombieHorde.hpp" int main() { - srand(time(NULL)); + int seed; + std::ifstream devRandom("/dev/random"); + if (devRandom.is_open()) + { + devRandom.read((char*)&seed, sizeof(int)); + devRandom.close(); + } + else + seed = time(NULL); + srand(seed); - std::cout << "Stack horde" << std::endl; + std::cout << "=== Stack horde ===" << std::endl; ZombieHorde horde(5); horde.announce(); - std::cout << std::endl << "Heap horde" << std::endl; + std::cout << std::endl << "=== Heap horde ===" << std::endl; ZombieHorde *heap_horde = new ZombieHorde(7); heap_horde->announce(); delete heap_horde; + + std::cout << std::endl << "=== Empty horde ===" << std::endl; + ZombieHorde *empty_horde = new ZombieHorde(0); + empty_horde->announce(); + delete empty_horde; + + std::cout << std::endl << "=== Error horde ===" << std::endl; + ZombieHorde *negative_horde = new ZombieHorde(-13); + negative_horde->announce(); + delete negative_horde; + + return 0; } -- cgit