Compare commits

...

5 Commits

Author SHA1 Message Date
636365d65c d 2023-07-18 03:20:06 +02:00
5d687d7c28 add: ex04 2023-07-18 03:20:06 +02:00
3ef324e5e1 recration of ex03 2023-07-18 03:20:06 +02:00
98ece4839e fix: ex02 2023-07-18 03:20:00 +02:00
76d822b831 add: ex02 2023-07-18 03:18:33 +02:00
21 changed files with 175 additions and 317 deletions

View File

@ -1,15 +1,17 @@
#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(-1, "bob"); Zombie* bob = zombieHorde(4, "bob");
if (bob == NULL) for (size_t i = 0; i < 4; i++)
return 1; bob[i].announe();
bob->announe();
delete[] bob; delete[] bob;
Zombie* pierre = zombieHorde(-1, "pierre");
if (pierre == NULL)
return 1;
return 0; return 0;
} }

View File

@ -7,9 +7,6 @@ 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 = ex00 NAME = ex02
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++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0 CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src SRCDIR = src
OBJDIR = obj OBJDIR = obj
BINDIR = bin NAME = ex03
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: $(BINDIR)/$(EXECUTABLE) all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@ mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(BINDIR)/$(EXECUTABLE): $(OBJS) $(NAME): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@ $(CXX) $(CPPFLAGS) $^ -o $@
clean: clean:
rm -rf $(OBJDIR)/*.o rm -rf $(OBJDIR)/*.o
fclean: clean fclean: clean
rm -fr $(BINDIR)/$(EXECUTABLE) rm -fr $(NAME)
re: fclean all re: fclean all

View File

View File

@ -1,17 +1,37 @@
#include "HumanA.hpp" #include "HumanA.hpp"
#include "Weapon.hpp"
#include <string>
#include <iostream>
HumanA::HumanA(std::string name, Weapon &weapon) : weapon(weapon) #include <iostream>
#include <string>
HumanA::HumanA(const std::string& name, const 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()
{}
void HumanA::attack()
{ {
std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl; 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;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

25
ex04/Makefile Normal file
View File

@ -0,0 +1,25 @@
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

View File

@ -1,25 +0,0 @@
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

View File

@ -1,47 +0,0 @@
#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;
}

View File

@ -1,16 +0,0 @@
#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;

View File

@ -1,19 +0,0 @@
#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;
}

View File

@ -1,25 +0,0 @@
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

View File

@ -1,47 +0,0 @@
#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;
}

View File

@ -1,16 +0,0 @@
#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;

View File

@ -1,28 +0,0 @@
#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);
}