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

View File

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

View File

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

View File

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

0
ex03/obj/.gitkeep Normal file
View File

View File

@ -1,37 +1,17 @@
#include "HumanA.hpp" #include "HumanA.hpp"
#include "Weapon.hpp"
#include <iostream>
#include <string> #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->name = name;
this->_weapon = weapon;
}
HumanA::HumanA()
{
std::cout << "HumanA()" << std::endl;
}
HumanA::HumanA(const HumanA& src)
{
*this = src;
} }
HumanA::~HumanA() 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() 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 #ifndef HUMANA_HPP
# define HUMANA_HPP
# include "Weapon.hpp" # include "Weapon.hpp"
# include <string> # include <string>
class HumanA class HumanA
{ {
private:
Weapon &weapon;
std::string name;
public: public:
HumanA(); HumanA(std::string name, Weapon &weapon);
HumanA(const std::string& name, const Weapon& weapon);
HumanA(const HumanA& src);
~HumanA(); ~HumanA();
HumanA& operator=(const HumanA& src);
void attack(); void attack();
private:
Weapon _weapon;
std::string _name;
}; };
#endif

View File

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

View File

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

View File

@ -1,40 +1,24 @@
#include "Weapon.hpp" #include "Weapon.hpp"
#include <iostream>
#include <string> #include <string>
Weapon::Weapon(std::string type)
{
this->type = type;
}
Weapon::Weapon() 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() 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; this->type = new_type;
return *this;
}
void Weapon::setType(const std::string& newType)
{
this->_type = newType;
}
const std::string& Weapon::getType()
{
return this->_type;
} }

View File

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

View File

@ -1,5 +1,7 @@
#include "Weapon.hpp"
#include "HumanA.hpp" #include "HumanA.hpp"
#include "HumanB.hpp" #include "HumanB.hpp"
int main() 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);
}