aboutsummaryrefslogtreecommitdiff
path: root/cpp01/ex03
diff options
context:
space:
mode:
Diffstat (limited to 'cpp01/ex03')
-rw-r--r--cpp01/ex03/Zombie.cpp9
-rw-r--r--cpp01/ex03/Zombie.hpp5
-rw-r--r--cpp01/ex03/ZombieHorde.cpp12
-rw-r--r--cpp01/ex03/ZombieHorde.hpp7
-rw-r--r--cpp01/ex03/main.cpp30
5 files changed, 51 insertions, 12 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
#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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <cstdlib>
# include <string>
+# include <iostream>
# 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <cstdlib>
#include <iostream>
+#include <fstream>
#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;
}