recreation of ex01

This commit is contained in:
starnakin 2023-07-12 02:45:13 +02:00
parent 55a7c73656
commit cfee0e2ec4
6 changed files with 64 additions and 48 deletions

View File

@ -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

Binary file not shown.

View File

@ -1,30 +1,40 @@
#include "Zombie.hpp"
#include <string>
#include "./Zombie.hpp"
#include <iostream>
Zombie::Zombie(std::string name)
{
this->name = name;
std::cout << this->name << ": " << "Zombie()" << std::endl;
}
#include <string>
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;
}

View File

@ -1,18 +1,22 @@
#pragma once
#include <string>
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);

View File

@ -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;
}

View File

@ -1,15 +1,15 @@
#include "Zombie.hpp"
#include <cstddef>
#include <string>
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;
}