Compare commits

..

No commits in common. "636365d65c672763149763e1c76414dcb7ffe662" and "095cc3dc182134ffb1afe43ea6a3f545241b9bf7" have entirely different histories.

21 changed files with 314 additions and 172 deletions

View File

@ -1,17 +1,15 @@
#include "Zombie.hpp"
#include <cstddef>
int main()
{
Zombie* jean = zombieHorde(2, "jean");
delete[] jean;
Zombie* bob = zombieHorde(4, "bob");
for (size_t i = 0; i < 4; i++)
bob[i].announe();
delete[] bob;
Zombie* pierre = zombieHorde(-1, "pierre");
if (pierre == NULL)
Zombie* bob = zombieHorde(-1, "bob");
if (bob == NULL)
return 1;
bob->announe();
delete[] bob;
return 0;
}

View File

@ -7,6 +7,9 @@ Zombie* zombieHorde(int N, std::string name)
return NULL;
Zombie* zombies = new Zombie[N];
for (int i = 0; i < N; i++)
{
zombies[i].setName(name);
zombies[i].announe();
}
return zombies;
}

View File

@ -2,7 +2,7 @@ CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
OBJDIR = obj
NAME = ex02
NAME = ex00
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))

View File

@ -1,25 +1,25 @@
CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
NAME = ex03
BINDIR = bin
EXECUTABLE = Weapon
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
all: $(BINDIR)/$(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(CXX) $(CXXFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
$(BINDIR)/$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(NAME)
rm -fr $(BINDIR)/$(EXECUTABLE)
re: fclean all

0
ex03/obj/.gitkeep Normal file
View File

View File

@ -1,37 +1,17 @@
#include "HumanA.hpp"
#include <iostream>
#include "Weapon.hpp"
#include <string>
#include <iostream>
HumanA::HumanA(const std::string& name, const Weapon& weapon)
HumanA::HumanA(std::string name, Weapon &weapon) : weapon(weapon)
{
this->_name = name;
this->_weapon = weapon;
}
HumanA::HumanA()
{
std::cout << "HumanA()" << std::endl;
}
HumanA::HumanA(const HumanA& src)
{
*this = src;
this->name = name;
}
HumanA::~HumanA()
{
std::cout << "~HumanA()" << std::endl;
}
HumanA& HumanA::operator=(const HumanA& src)
{
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
{}
void HumanA::attack()
{
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;
std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl;
}

View File

@ -1,22 +1,19 @@
#pragma once
#include "Weapon.hpp"
#include <string>
#ifndef HUMANA_HPP
# define HUMANA_HPP
# include "Weapon.hpp"
# include <string>
class HumanA
{
private:
Weapon &weapon;
std::string name;
public:
HumanA();
HumanA(const std::string& name, const Weapon& weapon);
HumanA(const HumanA& src);
HumanA(std::string name, Weapon &weapon);
~HumanA();
HumanA& operator=(const HumanA& src);
void attack();
private:
Weapon _weapon;
std::string _name;
};
#endif

View File

@ -1,43 +1,29 @@
#include "HumanB.hpp"
#include <iostream>
#include "Weapon.hpp"
#include <cstddef>
#include <string>
#include <iostream>
HumanB::HumanB(const std::string& name)
HumanB::HumanB(std::string name)
{
this->_name = name;
}
HumanB::HumanB()
{
std::cout << "HumanB()" << std::endl;
}
HumanB::HumanB(const HumanB& src)
{
*this = src;
this->weapon = NULL;
this->name = name;
}
HumanB::~HumanB()
{
std::cout << "~HumanB()" << std::endl;
}
HumanB& HumanB::operator=(const HumanB& src)
{
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
{}
void HumanB::attack()
{
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;
if (this->weapon == NULL)
{
std::cerr << this->name << " can't attack without weapon" << std::endl;
return;
}
std::cout << this->name << " attacks with their " << this->weapon->getType() << std::endl;
}
void HumanB::setWeapon(const Weapon &weapon)
void HumanB::setWeapon(Weapon& new_weapon)
{
this->_weapon = weapon;
this->weapon = &new_weapon;
}

View File

@ -1,23 +1,19 @@
#pragma once
#include "Weapon.hpp"
#include <string>
#ifndef HUMANB_HPP
# define HUMANB_HPP
# include "Weapon.hpp"
# include <string>
class HumanB
{
private:
Weapon* weapon;
std::string name;
public:
HumanB();
HumanB(const std::string& name);
HumanB(const HumanB& src);
HumanB(std::string name);
~HumanB();
HumanB& operator=(const HumanB& src);
void setWeapon(Weapon& new_weapon);
void attack();
void setWeapon(const Weapon& weapon);
private:
Weapon _weapon;
std::string _name;
};
#endif

View File

@ -1,40 +1,24 @@
#include "Weapon.hpp"
#include <iostream>
#include <string>
Weapon::Weapon(std::string type)
{
this->type = type;
}
Weapon::Weapon()
{
std::cout << "Weapon()" << std::endl;
}
Weapon::Weapon(const Weapon& src)
{
this->_type = src._type;
}
Weapon::Weapon(const std::string& type)
{
this->_type = type;
}
Weapon::~Weapon()
{}
std::string Weapon::getType()
{
std::cout << "Weapon()" << std::endl;
return (this->type);
}
Weapon& Weapon::operator=(const Weapon &src)
void Weapon::setType(std::string new_type)
{
this->_type = src._type;
return *this;
}
void Weapon::setType(const std::string& newType)
{
this->_type = newType;
}
const std::string& Weapon::getType()
{
return this->_type;
this->type = new_type;
}

View File

@ -1,20 +1,18 @@
#pragma once
#include <string>
#ifndef WEAPON_HPP
# define WEAPON_HPP
# include <string>
class Weapon
{
private:
std::string type;
public:
Weapon(std::string type);
Weapon();
Weapon(const Weapon& src);
Weapon(const std::string& type);
~Weapon();
Weapon& operator=(const Weapon& src);
const std::string& getType();
void setType(const std::string& newType);
private:
std::string _type;
std::string getType();
void setType(std::string new_type);
};
#endif

View File

@ -1,5 +1,7 @@
#include "Weapon.hpp"
#include "HumanA.hpp"
#include "HumanB.hpp"
int main()
{
{

View File

@ -1,25 +0,0 @@
CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
OBJDIR = obj
NAME = sed
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(NAME)
re: fclean all

25
ex05/Makefile Normal file
View File

@ -0,0 +1,25 @@
CXX = c++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
BINDIR = bin
EXECUTABLE = harl
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(BINDIR)/$(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
$(BINDIR)/$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(BINDIR)/$(EXECUTABLE)
re: fclean all

47
ex05/src/Harl.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "Harl.hpp"
#include <iostream>
#include <string>
void Harl::complain(std::string level)
{
static const HarlMethod methods[4] = {
&Harl::debug,
&Harl::info,
&Harl::warning,
&Harl::error,
};
static const std::string strings[4] = {
"DEBUG",
"INFO",
"WARNING",
"ERROR"
};
for (size_t i = 0; i < 4; i++)
{
if (strings[i] == level)
{
(this->*methods[i])();
return;
}
}
}
void Harl::debug() const
{
std::cout << "I love having extra bacon for my 7XL-double-cheese-triple-pickle-special-ketchup burger. I really do !" << std::endl;
}
void Harl::info() const
{
std::cout << "I cannot believe adding extra bacon costs more money. You didnt put enough bacon in my burger ! If you did, I wouldnt be asking for more !" << std::endl;
}
void Harl::warning() const
{
std::cout << "I think I deserve to have some extra bacon for free. Ive been coming for years whereas you started working here since last month." << std::endl;
}
void Harl::error() const
{
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
}

16
ex05/src/Harl.hpp Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <string>
class Harl
{
private:
void debug() const;
void info() const;
void warning() const;
void error() const;
public:
void complain(std::string level);
};
typedef void (Harl::*HarlMethod)() const;

19
ex05/src/main.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Harl.hpp"
int main()
{
Harl harl;
static const std::string levels[] = {
"DEBUG",
"INFO",
"WARNING",
"GOLEM",
"ERROR",
"PASLU",
"FLEMME",
"PANIC"
};
for (int i = 0; i < 6; i++)
harl.complain(levels[i]);
return 0;
}

25
ex06/Makefile Normal file
View File

@ -0,0 +1,25 @@
CXX = c++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
BINDIR = bin
EXECUTABLE = harl
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(BINDIR)/$(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
$(BINDIR)/$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(BINDIR)/$(EXECUTABLE)
re: fclean all

47
ex06/src/Harl.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "Harl.hpp"
#include <iostream>
#include <string>
void Harl::complain(std::string level)
{
static const HarlMethod methods[4] = {
&Harl::debug,
&Harl::info,
&Harl::warning,
&Harl::error,
};
static const std::string strings[4] = {
"DEBUG",
"INFO",
"WARNING",
"ERROR"
};
for (size_t i = 0; i < 4; i++)
{
if (strings[i] == level)
{
(this->*methods[i])();
return;
}
}
}
void Harl::debug() const
{
std::cout << "I love having extra bacon for my 7XL-double-cheese-triple-pickle-special-ketchup burger. I really do !" << std::endl;
}
void Harl::info() const
{
std::cout << "I cannot believe adding extra bacon costs more money. You didnt put enough bacon in my burger ! If you did, I wouldnt be asking for more !" << std::endl;
}
void Harl::warning() const
{
std::cout << "I think I deserve to have some extra bacon for free. Ive been coming for years whereas you started working here since last month." << std::endl;
}
void Harl::error() const
{
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
}

16
ex06/src/Harl.hpp Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <string>
class Harl
{
private:
void debug() const;
void info() const;
void warning() const;
void error() const;
public:
void complain(std::string level);
};
typedef void (Harl::*HarlMethod)() const;

28
ex06/src/main.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "Harl.hpp"
#include <iostream>
int main(int ac, char **av)
{
Harl harl;
static const std::string levels[] = {
"DEBUG",
"INFO",
"WARNING",
"GOLEM",
"ERROR",
"PASLU",
"FLEMME",
"PANIC"
};
for (int i = 0; i < 7; i++)
{
if (ac > 1 && levels[i] != av[1])
harl.complain(levels[i]);
else
{
std::cerr << "ERROR" << std::endl;
return (1);
}
}
return (0);
}