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 --- .gitignore | 1 + cpp00/ex00/Makefile | 12 ++++++++++++ cpp00/ex00/megaphone.cpp | 12 ++++++++++++ cpp00/ex01/Contact.hpp | 10 +++++----- cpp00/ex01/PhoneBook.cpp | 12 ++++++------ cpp00/ex01/PhoneBook.hpp | 14 +++++++------- cpp00/ex01/main.cpp | 7 +++---- cpp00/ex02/.keep | 0 cpp01/ex00/Pony.cpp | 22 ++++++++++++++-------- cpp01/ex00/Pony.hpp | 10 ++++++++-- cpp01/ex00/main.cpp | 9 ++++++--- cpp01/ex01/ex01.cpp | 2 +- cpp01/ex02/Zombie.cpp | 12 ++++++++---- cpp01/ex02/Zombie.hpp | 6 ++++-- cpp01/ex02/ZombieEvent.cpp | 8 ++++---- cpp01/ex02/ZombieEvent.hpp | 6 +++--- cpp01/ex02/main.cpp | 23 +++++++++++++++++------ 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 ++++++++++++++++++++++++++---- cpp01/ex04/ex04.cpp | 4 ++-- cpp01/ex05/Human.cpp | 11 ++++------- cpp01/ex05/Human.hpp | 6 +++--- cpp01/ex05/main.cpp | 6 +++--- cpp01/ex06/HumanA.cpp | 7 +++---- cpp01/ex06/HumanA.hpp | 6 +++--- cpp01/ex06/HumanB.cpp | 12 +++++++----- cpp01/ex06/HumanB.hpp | 6 +++--- cpp01/ex06/Weapon.cpp | 11 ++++------- cpp01/ex06/Weapon.hpp | 8 ++++---- cpp01/ex06/main.cpp | 2 +- cpp01/ex07/Makefile | 4 ++-- cpp01/ex07/main.cpp | 13 ++++++------- 35 files changed, 207 insertions(+), 118 deletions(-) delete mode 100644 cpp00/ex02/.keep diff --git a/.gitignore b/.gitignore index 9b6809c..e7fbed8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o +*.dSYM a.out megaphone crapyphonebook diff --git a/cpp00/ex00/Makefile b/cpp00/ex00/Makefile index 466a51e..2bfc080 100644 --- a/cpp00/ex00/Makefile +++ b/cpp00/ex00/Makefile @@ -1,3 +1,15 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/11/09 09:16:44 by cacharle #+# #+# # +# Updated: 2020/11/09 09:16:45 by cacharle ### ########.fr # +# # +# ############################################################################ # + RM = rm -f CC = clang++ diff --git a/cpp00/ex00/megaphone.cpp b/cpp00/ex00/megaphone.cpp index 32b4d14..1956510 100644 --- a/cpp00/ex00/megaphone.cpp +++ b/cpp00/ex00/megaphone.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* megaphone.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/09 09:15:55 by cacharle #+# #+# */ +/* Updated: 2020/11/09 09:16:00 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include #include diff --git a/cpp00/ex01/Contact.hpp b/cpp00/ex01/Contact.hpp index d9fbee6..4717e55 100644 --- a/cpp00/ex01/Contact.hpp +++ b/cpp00/ex01/Contact.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 07:23:11 by charles #+# #+# */ -/* Updated: 2020/04/13 08:57:01 by charles ### ########.fr */ +/* Updated: 2020/11/09 09:30:12 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,12 +22,12 @@ class Contact { public: static Contact prompt(); - void preview() const; - void put() const; + void preview() const; + void put() const; private: - static void promptString(std::string promptString, std::string &s); - static void promptInt(std::string promptString, int &i); + static void promptString(std::string promptString, std::string& s); + static void promptInt(std::string promptString, int& i); static std::string trimedName(std::string name); struct Address diff --git a/cpp00/ex01/PhoneBook.cpp b/cpp00/ex01/PhoneBook.cpp index a47307f..a28ce36 100644 --- a/cpp00/ex01/PhoneBook.cpp +++ b/cpp00/ex01/PhoneBook.cpp @@ -6,23 +6,23 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 07:23:13 by charles #+# #+# */ -/* Updated: 2020/04/13 08:43:05 by charles ### ########.fr */ +/* Updated: 2020/11/09 09:29:37 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "PhoneBook.hpp" -PhoneBook::PhoneBook(): m_size(0) -{ -} +PhoneBook::PhoneBook() : m_size(0) {} void PhoneBook::add(Contact contact) { + if (m_size >= CONTACTS_SIZE) + return; m_contacts[m_size] = contact; m_size++; } -Contact const &PhoneBook::get(int index) const +Contact const& PhoneBook::get(size_t index) const { return m_contacts[index]; } @@ -32,7 +32,7 @@ size_t PhoneBook::getSize() const return m_size; } -std::ostream &operator<<(std::ostream &out, PhoneBook const &p) +std::ostream& operator<<(std::ostream& out, PhoneBook const& p) { for (size_t i = 0; i < p.getSize(); i++) { diff --git a/cpp00/ex01/PhoneBook.hpp b/cpp00/ex01/PhoneBook.hpp index 8510710..abf83be 100644 --- a/cpp00/ex01/PhoneBook.hpp +++ b/cpp00/ex01/PhoneBook.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:06:23 by charles #+# #+# */ -/* Updated: 2020/04/13 09:06:39 by charles ### ########.fr */ +/* Updated: 2020/11/09 09:28:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,16 +22,16 @@ class PhoneBook { public: PhoneBook(); - void add(Contact contact); - Contact* search(std::string needle); - size_t getSize() const; - Contact const &get(int index) const; + void add(Contact contact); + Contact* search(std::string needle); + size_t getSize() const; + Contact const& get(size_t index) const; private: Contact m_contacts[CONTACTS_SIZE]; - int m_size; + size_t m_size; }; -std::ostream &operator<<(std::ostream &out, PhoneBook const &p); +std::ostream& operator<<(std::ostream& out, PhoneBook const& p); #endif diff --git a/cpp00/ex01/main.cpp b/cpp00/ex01/main.cpp index 5734d38..8b04fe2 100644 --- a/cpp00/ex01/main.cpp +++ b/cpp00/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 07:23:15 by charles #+# #+# */ -/* Updated: 2020/04/13 09:18:04 by charles ### ########.fr */ +/* Updated: 2020/11/09 09:22:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,7 @@ int main() { std::string input; - PhoneBook phoneBook; - int tmp; + PhoneBook phoneBook; while (true) { @@ -40,7 +39,7 @@ int main() else if (input == "SEARCH") { std::cout << phoneBook << std::flush; - tmp = getInt(); + int tmp = getInt(); if (tmp < 0 || size_t(tmp) >= phoneBook.getSize()) std::cout << "Error: Not valid index: " << tmp << std::endl; else diff --git a/cpp00/ex02/.keep b/cpp00/ex02/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/cpp01/ex00/Pony.cpp b/cpp01/ex00/Pony.cpp index b45f0e1..1b8df61 100644 --- a/cpp01/ex00/Pony.cpp +++ b/cpp01/ex00/Pony.cpp @@ -6,28 +6,34 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:06:06 by charles #+# #+# */ -/* Updated: 2020/04/13 09:33:59 by charles ### ########.fr */ +/* Updated: 2020/11/09 09:57:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include #include "Pony.hpp" -Pony::Pony(int weight, int maxSpeed) - : m_weight(weight), m_maxSpeed(maxSpeed) +Pony::Pony(std::string const& name, int weight, int maxSpeed) + : m_name(name), m_weight(weight), m_maxSpeed(maxSpeed) { + std::cout << "Hey there, my name is " << m_name << " and you?" << std::endl; +} + +Pony::~Pony() +{ + std::cout << "Oh no, I'M DYING! They called me " << m_name << "." << std::endl; } void Pony::sayHello() { - std::cout << "Hi, I'm a pony, I weight " << m_weight - << " and my speed limit is " << m_maxSpeed + std::cout << "Hi, I'm " << m_name + << ", I weight " << m_weight + << " and my speed limit is " << m_maxSpeed << std::endl; } void Pony::run() { for (int i = 0; i <= m_maxSpeed; i += m_maxSpeed / 10) - std::cout << "I'm running really fast at " << i - << ", look at me!" << std::endl; + std::cout << m_name << " is running really fast at " << i + << ", look at him!" << std::endl; } diff --git a/cpp01/ex00/Pony.hpp b/cpp01/ex00/Pony.hpp index 571414c..5c42a60 100644 --- a/cpp01/ex00/Pony.hpp +++ b/cpp01/ex00/Pony.hpp @@ -6,20 +6,26 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:26:16 by charles #+# #+# */ -/* Updated: 2020/04/13 09:27:49 by charles ### ########.fr */ +/* Updated: 2020/11/09 09:54:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PONY_HPP # define PONY_HPP +# include +# include + class Pony { public: - Pony(int weight, int maxSpeed); + Pony(std::string const& name, int weight, int maxSpeed); + ~Pony(); void sayHello(); void run(); + private: + std::string m_name; int m_weight; int m_maxSpeed; }; diff --git a/cpp01/ex00/main.cpp b/cpp01/ex00/main.cpp index 689f7f1..62a9e67 100644 --- a/cpp01/ex00/main.cpp +++ b/cpp01/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:26:11 by charles #+# #+# */ -/* Updated: 2020/04/13 09:29:58 by charles ### ########.fr */ +/* Updated: 2020/11/09 09:58:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,8 @@ void ponyOnTheHeap() { - Pony *p = new Pony(200, 100); + std::cout << "=== ponyOnTheHeap ===" << std::endl; + Pony *p = new Pony("ChunkyBoy", 200, 100); p->sayHello(); p->run(); delete p; @@ -23,7 +24,8 @@ void ponyOnTheHeap() void ponyOnTheStack() { - Pony p(200, 100); + std::cout << "=== ponyOnTheStack ===" << std::endl; + Pony p("Jean", 200, 100); p.sayHello(); p.run(); } @@ -31,6 +33,7 @@ void ponyOnTheStack() int main() { ponyOnTheHeap(); + std::cout << std::endl; ponyOnTheStack(); return 0; } diff --git a/cpp01/ex01/ex01.cpp b/cpp01/ex01/ex01.cpp index c3f416f..00b2b2e 100644 --- a/cpp01/ex01/ex01.cpp +++ b/cpp01/ex01/ex01.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:35:26 by charles #+# #+# */ -/* Updated: 2020/04/13 09:36:03 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:02:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp01/ex02/Zombie.cpp b/cpp01/ex02/Zombie.cpp index 40fb849..3736101 100644 --- a/cpp01/ex02/Zombie.cpp +++ b/cpp01/ex02/Zombie.cpp @@ -6,17 +6,21 @@ /* 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:27:15 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(); + announce(); +} + +Zombie::~Zombie() +{ + std::cout << "<" << m_name << " (" << m_type << ")> AAAAARG... ME DYING" << std::endl; } void Zombie::announce() diff --git a/cpp01/ex02/Zombie.hpp b/cpp01/ex02/Zombie.hpp index 5106c8f..bed4873 100644 --- a/cpp01/ex02/Zombie.hpp +++ b/cpp01/ex02/Zombie.hpp @@ -6,19 +6,21 @@ /* 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:26:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef ZOMBIE_HPP # define ZOMBIE_HPP +# include # include 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/ex02/ZombieEvent.cpp b/cpp01/ex02/ZombieEvent.cpp index acb8dd0..44d2c04 100644 --- a/cpp01/ex02/ZombieEvent.cpp +++ b/cpp01/ex02/ZombieEvent.cpp @@ -6,15 +6,15 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:41:33 by charles #+# #+# */ -/* Updated: 2020/04/13 09:49:36 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:25:24 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "ZombieEvent.hpp" -void ZombieEvent::setZombieType(std::string storedType) +void ZombieEvent::setZombieType(std::string const& type) { - m_storedType = storedType; + m_storedType = type; } Zombie* ZombieEvent::newZombie(std::string name) @@ -36,5 +36,5 @@ Zombie* ZombieEvent::randomChump() "yo", "rideaux" }; - return new Zombie(pool[rand() % 10], m_storedType); + return newZombie(pool[rand() % 10]); } diff --git a/cpp01/ex02/ZombieEvent.hpp b/cpp01/ex02/ZombieEvent.hpp index 7b2fe79..0d0e0ec 100644 --- a/cpp01/ex02/ZombieEvent.hpp +++ b/cpp01/ex02/ZombieEvent.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:42:36 by charles #+# #+# */ -/* Updated: 2020/04/13 09:45:36 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:10:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ class ZombieEvent { public: - void setZombieType(std::string type); - Zombie* newZombie(std::string name); + void setZombieType(std::string const& type); + Zombie* newZombie(std::string name); // subject prototype without const& Zombie* randomChump(); private: diff --git a/cpp01/ex02/main.cpp b/cpp01/ex02/main.cpp index 291fddb..e8fb2c9 100644 --- a/cpp01/ex02/main.cpp +++ b/cpp01/ex02/main.cpp @@ -6,29 +6,40 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 09:47:21 by charles #+# #+# */ -/* Updated: 2020/04/13 09:48:29 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:33:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include +#include #include "Zombie.hpp" #include "ZombieEvent.hpp" int main() { - Zombie* z; + Zombie* z; ZombieEvent zevent; - Zombie zStack("onTheStack", "IdontLikeHeap"); + Zombie zStack("onTheStack", "IdontLikeHeap"); - srand(time(NULL)); - zevent.setZombieType("standard"); + 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); + zevent.setZombieType("NotRandom"); z = zevent.newZombie("jean"); + zevent.setZombieType("random"); delete z; for (int i = 0; i < 5; i++) { z = zevent.randomChump(); delete z; } - return 0; } 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; } diff --git a/cpp01/ex04/ex04.cpp b/cpp01/ex04/ex04.cpp index 7eed286..bdc60e9 100644 --- a/cpp01/ex04/ex04.cpp +++ b/cpp01/ex04/ex04.cpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 12:43:03 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:01:19 by charles ### ########.fr */ +/* Updated: 2020/11/09 10:59:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,6 @@ int main() std::string& ref_s = s; std::cout << *ptr_s << std::endl; - std::cout << ref_s << std::endl; + std::cout << ref_s << std::endl; return 0; } diff --git a/cpp01/ex05/Human.cpp b/cpp01/ex05/Human.cpp index f4c0066..e7f55bd 100644 --- a/cpp01/ex05/Human.cpp +++ b/cpp01/ex05/Human.cpp @@ -6,23 +6,20 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 12:52:38 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:09:03 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:04:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "Human.hpp" -Human::Human() - : m_brain() -{ -} +Human::Human() : m_brain() {} -std::string Human::identify() +std::string Human::identify() const { return m_brain.identify(); } -const Brain& Human::getBrain() +const Brain& Human::getBrain() const { return m_brain; } diff --git a/cpp01/ex05/Human.hpp b/cpp01/ex05/Human.hpp index d7a4611..fc89d03 100644 --- a/cpp01/ex05/Human.hpp +++ b/cpp01/ex05/Human.hpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 12:52:39 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:08:07 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:03:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,8 @@ class Human { public: Human(); - std::string identify(); - const Brain& getBrain() ; + std::string identify() const; + const Brain& getBrain() const; private: const Brain m_brain; diff --git a/cpp01/ex05/main.cpp b/cpp01/ex05/main.cpp index 584320a..8ad1493 100644 --- a/cpp01/ex05/main.cpp +++ b/cpp01/ex05/main.cpp @@ -6,12 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 12:57:25 by cacharle #+# #+# */ -/* Updated: 2020/02/02 16:27:47 by cacharle ### ########.fr */ +/* Updated: 2020/11/09 10:59:52 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -# include "Brain.hpp" -# include "Human.hpp" +#include "Brain.hpp" +#include "Human.hpp" int main() { diff --git a/cpp01/ex06/HumanA.cpp b/cpp01/ex06/HumanA.cpp index 7e52494..ab4e5f3 100644 --- a/cpp01/ex06/HumanA.cpp +++ b/cpp01/ex06/HumanA.cpp @@ -6,16 +6,15 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 16:44:03 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:26:03 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:09:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "HumanA.hpp" -HumanA::HumanA(std::string name, Weapon& weapon) +HumanA::HumanA(std::string const& name, Weapon& weapon) : m_name(name), m_weapon(weapon) -{ -} +{} void HumanA::attack() { diff --git a/cpp01/ex06/HumanA.hpp b/cpp01/ex06/HumanA.hpp index f1691b3..d05d2fd 100644 --- a/cpp01/ex06/HumanA.hpp +++ b/cpp01/ex06/HumanA.hpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 16:37:23 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:24:13 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:08:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,11 @@ class HumanA { public: - HumanA(std::string name, Weapon& weapon); + HumanA(std::string const& name, Weapon& weapon); void attack(); private: std::string m_name; - Weapon& m_weapon; + Weapon& m_weapon; }; #endif diff --git a/cpp01/ex06/HumanB.cpp b/cpp01/ex06/HumanB.cpp index 98774aa..e6adb22 100644 --- a/cpp01/ex06/HumanB.cpp +++ b/cpp01/ex06/HumanB.cpp @@ -6,20 +6,22 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 16:46:02 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:25:54 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:13:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "HumanB.hpp" -HumanB::HumanB(std::string name) +HumanB::HumanB(std::string const& name) : m_name(name), m_weapon(NULL) -{ -} +{} void HumanB::attack() { - std::cout << m_name << " attack with his " << m_weapon->getType() << std::endl; + if (m_weapon == NULL) + std::cout << m_name << " doesn't have any weapon " << std::endl; + else + std::cout << m_name << " attack with his " << m_weapon->getType() << std::endl; } void HumanB::setWeapon(Weapon& weapon) diff --git a/cpp01/ex06/HumanB.hpp b/cpp01/ex06/HumanB.hpp index 269f4fc..fc9236c 100644 --- a/cpp01/ex06/HumanB.hpp +++ b/cpp01/ex06/HumanB.hpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 16:42:54 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:23:31 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:09:56 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,12 +19,12 @@ class HumanB { public: - HumanB(std::string name); + HumanB(std::string const& name); void attack(); void setWeapon(Weapon& weapon); private: std::string m_name; - Weapon* m_weapon; + Weapon* m_weapon; }; #endif diff --git a/cpp01/ex06/Weapon.cpp b/cpp01/ex06/Weapon.cpp index e517947..88b4357 100644 --- a/cpp01/ex06/Weapon.cpp +++ b/cpp01/ex06/Weapon.cpp @@ -6,23 +6,20 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 16:36:23 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:25:35 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:08:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "Weapon.hpp" -Weapon::Weapon(std::string t) - : type(t) -{ -} +Weapon::Weapon(std::string t) : type(t) {} -const std::string& Weapon::getType() const +std::string const& Weapon::getType() const { return type; } -void Weapon::setType(const std::string& t) +void Weapon::setType(std::string const& t) { type = t; } diff --git a/cpp01/ex06/Weapon.hpp b/cpp01/ex06/Weapon.hpp index d9ad476..08fef0c 100644 --- a/cpp01/ex06/Weapon.hpp +++ b/cpp01/ex06/Weapon.hpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 16:34:31 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:16:46 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:08:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,11 @@ class Weapon { public: Weapon(std::string t); - const std::string& getType() const; - void setType(const std::string& t); + std::string const& getType() const; + void setType(std::string const& t); private: - std::string type; // subject want litteraly `type` + std::string type; // subject wants litteraly `type` }; #endif diff --git a/cpp01/ex06/main.cpp b/cpp01/ex06/main.cpp index dfc03a0..5c92b43 100644 --- a/cpp01/ex06/main.cpp +++ b/cpp01/ex06/main.cpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 16:40:20 by cacharle #+# #+# */ -/* Updated: 2020/02/02 16:40:51 by cacharle ### ########.fr */ +/* Updated: 2020/11/09 11:13:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp01/ex07/Makefile b/cpp01/ex07/Makefile index 1617d62..15c5518 100644 --- a/cpp01/ex07/Makefile +++ b/cpp01/ex07/Makefile @@ -6,12 +6,12 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/02 17:05:31 by cacharle #+# #+# # -# Updated: 2020/04/13 10:32:03 by charles ### ########.fr # +# Updated: 2020/11/09 11:15:17 by cacharle ### ########.fr # # # # **************************************************************************** # CXX = clang++ -CXXFLAGS= -Wall -Wextra -Werror +CXXFLAGS = -Wall -Wextra -Werror SRC = $(shell find . -type f -name "*.cpp") OBJ = $(SRC:.cpp=.o) diff --git a/cpp01/ex07/main.cpp b/cpp01/ex07/main.cpp index 64e5ce8..d98168a 100644 --- a/cpp01/ex07/main.cpp +++ b/cpp01/ex07/main.cpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 17:08:28 by cacharle #+# #+# */ -/* Updated: 2020/04/13 10:31:55 by charles ### ########.fr */ +/* Updated: 2020/11/09 11:22:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,27 +21,26 @@ int main(int argc, char **argv) std::cerr << "Usage: " << argv[0] << " filename s1 s2" << std::endl; return 1; } + std::string filename(argv[1]); std::string s1(argv[2]); std::string s2(argv[3]); - if (s1 == "" || s2 == "") + if (filename.empty() || s1.empty() || s2.empty()) { - std::cerr << "Error: s1 and s2 should not be empty" << std::endl; + std::cerr << "Error: filename, s1 and s2 should not be empty" << std::endl; return 1; } std::ifstream file(filename); std::ofstream outfile(filename + ".replace"); - if (!file) + if (!file.is_open()) { std::cerr << "Could not open " << filename; - outfile.close(); return 1; } - if (!outfile) + if (!outfile.is_open()) { std::cerr << "Could not create " << filename << ".replace"; - file.close(); return 1; } -- cgit