diff --git a/ex01/Makefile b/ex01/Makefile index 36bbf08..e998f72 100644 --- a/ex01/Makefile +++ b/ex01/Makefile @@ -1,25 +1,25 @@ CXX = c++ -CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0 +CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g SRCDIR = src OBJDIR = obj -BINDIR = bin -EXECUTABLE = zombie +NAME = ex01 SRCS = $(wildcard $(SRCDIR)/*.cpp) OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) -all: $(BINDIR)/$(EXECUTABLE) +all: $(NAME) $(OBJDIR)/%.o: $(SRCDIR)/%.cpp - $(CXX) $(CXXFLAGS) -c $< -o $@ + mkdir -p obj + $(CXX) $(CPPFLAGS) -c $< -o $@ -$(BINDIR)/$(EXECUTABLE): $(OBJS) - $(CXX) $(CXXFLAGS) $^ -o $@ +$(NAME): $(OBJS) + $(CXX) $(CPPFLAGS) $^ -o $@ clean: rm -rf $(OBJDIR)/*.o fclean: clean - rm -fr $(BINDIR)/$(EXECUTABLE) + rm -fr $(NAME) re: fclean all diff --git a/ex01/bin/zombie b/ex01/bin/zombie deleted file mode 100755 index ffef431..0000000 Binary files a/ex01/bin/zombie and /dev/null differ diff --git a/ex01/src/Zombie.cpp b/ex01/src/Zombie.cpp index faaebee..79a3d29 100644 --- a/ex01/src/Zombie.cpp +++ b/ex01/src/Zombie.cpp @@ -1,30 +1,40 @@ -#include "Zombie.hpp" -#include +#include "./Zombie.hpp" #include - -Zombie::Zombie(std::string name) -{ - this->name = name; - std::cout << this->name << ": " << "Zombie()" << std::endl; -} +#include Zombie::Zombie() { - this->name = ""; - std::cout << this->name << ": " << "Zombie()" << std::endl; + std::cout << "Zombie()" << std::endl; +} + +Zombie::Zombie(const std::string& name) +{ + std::cout << "Zombie(" << name << ")" << std::endl; + this->_name = name; +} + +Zombie::Zombie(const Zombie& src) +{ + *this = src; } Zombie::~Zombie() { - std::cout << this->name << ": " << "~Zombie()" << std::endl; + std::cout << "~Zombie()" << std::endl; } -void Zombie::announce() const +Zombie& Zombie::operator=(const Zombie& src) { - std::cout << this->name << ": " << "BraiiiiiiinnnzzzZ..." << std::endl; + this->_name = src._name; + return *this; } -void Zombie::setName(std::string name) +void Zombie::announe() { - this->name = name; + std::cout << this->_name << ": BraiiiiiiinnnzzzZ..." << std::endl; +} + +void Zombie::setName(const std::string &name) +{ + this->_name = name; } diff --git a/ex01/src/Zombie.hpp b/ex01/src/Zombie.hpp index 90a0f38..3ca07cb 100644 --- a/ex01/src/Zombie.hpp +++ b/ex01/src/Zombie.hpp @@ -1,18 +1,22 @@ +#pragma once + #include -class Zombie { - +class Zombie +{ private: - std::string name; - - public: - Zombie(std::string name); + std::string _name; + + public: + Zombie(const Zombie& src); Zombie(); + Zombie(const std::string& name); ~Zombie(); - void setName(std::string name); - void announce(void) const; + Zombie& operator=(const Zombie& src); + void announe(void); + void setName(const std::string& name); }; -Zombie* zombieHorde(int N, std::string name); +Zombie* zombieHorde(int N, std::string name); diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index 164d1ca..e9563ee 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -1,13 +1,15 @@ #include "Zombie.hpp" -int main() +int main() { - Zombie* jean; - Zombie pierre("Pierre"); - - jean = new Zombie("jean"); - jean->announce(); - delete jean; - jean = zombieHorde(3, "jean"); + Zombie* jean = zombieHorde(2, "jean"); delete[] jean; + + Zombie* bob = zombieHorde(-1, "bob"); + if (bob == NULL) + return 1; + bob->announe(); + delete[] bob; + + return 0; } diff --git a/ex01/src/zombieHorde.cpp b/ex01/src/zombieHorde.cpp index 0ff3544..e795d29 100644 --- a/ex01/src/zombieHorde.cpp +++ b/ex01/src/zombieHorde.cpp @@ -1,15 +1,15 @@ #include "Zombie.hpp" -#include +#include Zombie* zombieHorde(int N, std::string name) { - size_t i; - Zombie *zombies; - if (0 > N) - return (NULL); - zombies = new Zombie[N]; - for (i = 0; i < (size_t) N; i++) + return NULL; + Zombie* zombies = new Zombie[N]; + for (int i = 0; i < N; i++) + { zombies[i].setName(name); - return (zombies); + zombies[i].announe(); + } + return zombies; }