Compare commits
19 Commits
84b0db8c9c
...
master
Author | SHA1 | Date | |
---|---|---|---|
35cbcfd02d | |||
cebe9f6ea7 | |||
cc5928a671 | |||
5522ebf3ba | |||
a7e4a9cd75 | |||
7a10311d56 | |||
42a06c46cd | |||
efa91c0cb5 | |||
eb2b035b75 | |||
636365d65c | |||
5d687d7c28 | |||
3ef324e5e1 | |||
98ece4839e | |||
76d822b831 | |||
095cc3dc18 | |||
ea247970e4 | |||
cfee0e2ec4 | |||
55a7c73656 | |||
dcfb2af057 |
@ -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 = ex00
|
||||||
EXECUTABLE = zombie
|
|
||||||
|
|
||||||
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
|
||||||
|
@ -1,19 +1,24 @@
|
|||||||
#include "Zombie.hpp"
|
#include "./Zombie.hpp"
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
Zombie::Zombie(std::string name)
|
Zombie::Zombie()
|
||||||
{
|
{
|
||||||
this->name = name;
|
std::cout << "Zombie()" << std::endl;
|
||||||
std::cout << this->name << ": " << "Zombie()" << std::endl;
|
}
|
||||||
|
|
||||||
|
Zombie::Zombie(const std::string& name)
|
||||||
|
{
|
||||||
|
std::cout << "Zombie(" << name << ")" << std::endl;
|
||||||
|
this->_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
Zombie::~Zombie()
|
Zombie::~Zombie()
|
||||||
{
|
{
|
||||||
std::cout << this->name << ": " << "~Zombie()" << std::endl;
|
std::cout << "~Zombie()" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Zombie::announce() const
|
void Zombie::announce()
|
||||||
{
|
{
|
||||||
std::cout << this->name << ": " << "BraiiiiiiinnnzzzZ..." << std::endl;
|
std::cout << this->_name << ": BraiiiiiiinnnzzzZ..." << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Zombie {
|
class Zombie
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
std::string name;
|
std::string _name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Zombie(std::string name);
|
Zombie(const std::string& name);
|
||||||
|
Zombie();
|
||||||
~Zombie();
|
~Zombie();
|
||||||
|
|
||||||
void announce(void) const;
|
void announce(void);
|
||||||
void setName(std::string name);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Zombie* newZombie(std::string name);
|
void randomChump(std::string name);
|
||||||
void randomChump(std::string name);
|
Zombie* newZombie(std::string name);
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
#include "Zombie.hpp"
|
#include "Zombie.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Zombie* jean;
|
Zombie* jean = newZombie("jean");
|
||||||
Zombie pierre("Pierre");
|
|
||||||
|
|
||||||
jean = new Zombie("jean");
|
|
||||||
jean->announce();
|
jean->announce();
|
||||||
delete jean;
|
delete jean;
|
||||||
randomChump("francois");
|
|
||||||
|
Zombie no_name;
|
||||||
|
no_name.announce();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,5 @@
|
|||||||
|
|
||||||
Zombie* newZombie(std::string name)
|
Zombie* newZombie(std::string name)
|
||||||
{
|
{
|
||||||
Zombie* zombie;
|
return new Zombie(name);
|
||||||
|
|
||||||
zombie = new Zombie(name);
|
|
||||||
return (zombie);
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#include "Zombie.hpp"
|
#include "Zombie.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
void randomChump(std::string name)
|
void randomChump(std::string name)
|
||||||
{
|
{
|
||||||
Zombie zombie(name);
|
Zombie zombie(name);
|
||||||
|
|
||||||
zombie.announce();
|
zombie.announce();
|
||||||
}
|
}
|
||||||
|
@ -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 = ex01
|
||||||
EXECUTABLE = zombie
|
|
||||||
|
|
||||||
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
|
||||||
|
BIN
ex01/bin/zombie
BIN
ex01/bin/zombie
Binary file not shown.
@ -1,30 +1,29 @@
|
|||||||
#include "Zombie.hpp"
|
#include "./Zombie.hpp"
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
Zombie::Zombie(std::string name)
|
|
||||||
{
|
|
||||||
this->name = name;
|
|
||||||
std::cout << this->name << ": " << "Zombie()" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
Zombie::Zombie()
|
Zombie::Zombie()
|
||||||
{
|
{
|
||||||
this->name = "";
|
std::cout << "Zombie()" << std::endl;
|
||||||
std::cout << this->name << ": " << "Zombie()" << std::endl;
|
}
|
||||||
|
|
||||||
|
Zombie::Zombie(const std::string& name)
|
||||||
|
{
|
||||||
|
std::cout << "Zombie(" << name << ")" << std::endl;
|
||||||
|
this->_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
Zombie::~Zombie()
|
Zombie::~Zombie()
|
||||||
{
|
{
|
||||||
std::cout << this->name << ": " << "~Zombie()" << std::endl;
|
std::cout << "~Zombie()" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Zombie::announce() const
|
void Zombie::announce()
|
||||||
{
|
{
|
||||||
std::cout << this->name << ": " << "BraiiiiiiinnnzzzZ..." << std::endl;
|
std::cout << this->_name << ": BraiiiiiiinnnzzzZ..." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Zombie::setName(std::string name)
|
void Zombie::setName(const std::string &name)
|
||||||
{
|
{
|
||||||
this->name = name;
|
this->_name = name;
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Zombie {
|
class Zombie
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
std::string name;
|
std::string _name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Zombie(std::string name);
|
|
||||||
Zombie();
|
Zombie();
|
||||||
|
Zombie(const std::string& name);
|
||||||
~Zombie();
|
~Zombie();
|
||||||
|
|
||||||
void setName(std::string name);
|
void announce(void);
|
||||||
void announce(void) const;
|
void setName(const std::string& name);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Zombie* zombieHorde(int N, std::string name);
|
Zombie* zombieHorde(int N, std::string name);
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
#include "Zombie.hpp"
|
#include "Zombie.hpp"
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Zombie* jean;
|
Zombie* jean = zombieHorde(2, "jean");
|
||||||
Zombie pierre("Pierre");
|
|
||||||
|
|
||||||
jean = new Zombie("jean");
|
|
||||||
jean->announce();
|
|
||||||
delete jean;
|
|
||||||
jean = zombieHorde(3, "jean");
|
|
||||||
delete[] jean;
|
delete[] jean;
|
||||||
|
|
||||||
|
Zombie* bob = zombieHorde(4, "bob");
|
||||||
|
for (size_t i = 0; i < 4; i++)
|
||||||
|
bob[i].announce();
|
||||||
|
delete[] bob;
|
||||||
|
Zombie* pierre = zombieHorde(-1, "pierre");
|
||||||
|
if (pierre == NULL)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,12 @@
|
|||||||
#include "Zombie.hpp"
|
#include "Zombie.hpp"
|
||||||
#include <cstddef>
|
#include <string>
|
||||||
|
|
||||||
Zombie* zombieHorde(int N, std::string name)
|
Zombie* zombieHorde(int N, std::string name)
|
||||||
{
|
{
|
||||||
size_t i;
|
|
||||||
Zombie *zombies;
|
|
||||||
|
|
||||||
if (0 > N)
|
if (0 > N)
|
||||||
return (NULL);
|
return NULL;
|
||||||
zombies = new Zombie[N];
|
Zombie* zombies = new Zombie[N];
|
||||||
for (i = 0; i < (size_t) N; i++)
|
for (int i = 0; i < N; i++)
|
||||||
zombies[i].setName(name);
|
zombies[i].setName(name);
|
||||||
return (zombies);
|
return zombies;
|
||||||
}
|
}
|
||||||
|
@ -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 = ex02
|
||||||
EXECUTABLE = ref_vs_ptr
|
|
||||||
|
|
||||||
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
|
||||||
|
Binary file not shown.
@ -1,18 +1,19 @@
|
|||||||
#include <ostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::string string = "HI THIS IS BRAIN";
|
std::string string = "HI THIS IS BRAIN";
|
||||||
std::string* stringPTR = &string;
|
std::string* stringPTR = &string;
|
||||||
std::string& stringREF = string;
|
std::string& stringREF = string;
|
||||||
|
|
||||||
std::cout << &string << std::endl;
|
std::cout << "ADDRESS" << std::endl;
|
||||||
std::cout << stringPTR << std::endl;
|
std::cout << "original: " << &string << std::endl;
|
||||||
std::cout << &stringREF << std::endl;
|
std::cout << "pointer: " << stringPTR << std::endl;
|
||||||
|
std::cout << "reference: " << &stringREF << std::endl;
|
||||||
std::cout << string << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << *stringPTR << std::endl;
|
std::cout << "VALUE" << std::endl;
|
||||||
std::cout << stringREF << std::endl;
|
std::cout << "original: " << string << std::endl;
|
||||||
|
std::cout << "pointer: " << *stringPTR << std::endl;
|
||||||
|
std::cout << "reference: " << stringREF << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -1,17 +1,21 @@
|
|||||||
#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):
|
||||||
|
_weapon(weapon)
|
||||||
{
|
{
|
||||||
this->name = name;
|
this->_name = name;
|
||||||
|
std::cout << "HumanA()" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
HumanA::~HumanA()
|
HumanA::~HumanA()
|
||||||
{}
|
|
||||||
|
|
||||||
void HumanA::attack()
|
|
||||||
{
|
{
|
||||||
std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl;
|
std::cout << "~HumanA()" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HumanA::attack()
|
||||||
|
{
|
||||||
|
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
#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(const std::string& name, const Weapon& weapon);
|
||||||
std::string name;
|
|
||||||
|
|
||||||
public:
|
|
||||||
HumanA(std::string name, Weapon &weapon);
|
|
||||||
~HumanA();
|
~HumanA();
|
||||||
|
|
||||||
void attack();
|
void attack();
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
private:
|
||||||
|
const Weapon& _weapon;
|
||||||
|
std::string _name;
|
||||||
|
|
||||||
|
};
|
||||||
|
@ -1,29 +1,33 @@
|
|||||||
#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;
|
std::cout << "HumanB()" << std::endl;
|
||||||
this->name = name;
|
this->_name = name;
|
||||||
|
this->_weapon = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
void HumanB::attack()
|
||||||
{
|
{
|
||||||
this->weapon = &new_weapon;
|
std::cout << this->_name << " attacks with their ";
|
||||||
|
if (this->_weapon == NULL)
|
||||||
|
std::cout << "hands";
|
||||||
|
else
|
||||||
|
std::cout << this->_weapon->getType();
|
||||||
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HumanB::setWeapon(const Weapon &weapon)
|
||||||
|
{
|
||||||
|
this->_weapon = &weapon;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
#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(const std::string& name);
|
||||||
std::string name;
|
|
||||||
|
|
||||||
public:
|
|
||||||
HumanB(std::string name);
|
|
||||||
~HumanB();
|
~HumanB();
|
||||||
|
|
||||||
void setWeapon(Weapon& new_weapon);
|
void attack();
|
||||||
void attack();
|
void setWeapon(const Weapon& weapon);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Weapon* _weapon;
|
||||||
|
std::string _name;
|
||||||
|
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
|
@ -1,24 +1,42 @@
|
|||||||
#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)
|
||||||
|
{
|
||||||
|
std::cout << "Weapon()" << std::endl;
|
||||||
|
this->_type = src._type;
|
||||||
|
}
|
||||||
|
|
||||||
|
Weapon::Weapon(const std::string& type)
|
||||||
|
{
|
||||||
|
std::cout << "Weapon()" << std::endl;
|
||||||
|
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() const
|
||||||
|
{
|
||||||
|
return this->_type;
|
||||||
}
|
}
|
||||||
|
@ -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() const;
|
||||||
|
void setType(const std::string& newType);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string _type;
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
#include "Weapon.hpp"
|
|
||||||
#include "HumanA.hpp"
|
#include "HumanA.hpp"
|
||||||
#include "HumanB.hpp"
|
#include "HumanB.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
@ -19,5 +17,7 @@ int main()
|
|||||||
club.setType("some other type of club");
|
club.setType("some other type of club");
|
||||||
jim.attack();
|
jim.attack();
|
||||||
}
|
}
|
||||||
|
HumanB jim("Jim");
|
||||||
|
jim.attack();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
25
ex04/Makefile
Normal file
25
ex04/Makefile
Normal 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
|
@ -8,17 +8,13 @@
|
|||||||
static int get_text(const char *path, std::string &text)
|
static int get_text(const char *path, std::string &text)
|
||||||
{
|
{
|
||||||
std::ifstream file(path);
|
std::ifstream file(path);
|
||||||
std::string line;
|
|
||||||
|
|
||||||
if (!file.is_open())
|
if (!file.is_open())
|
||||||
{
|
{
|
||||||
std::cerr << "sed: file error" << std::endl;
|
std::cerr << "sed: file error" << std::endl;
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
while (std::getline(file, line))
|
std::getline(file, text, '\0');
|
||||||
text += line + "\n";
|
|
||||||
if (text.size() != 0)
|
|
||||||
text.erase(text.size() - 1);
|
|
||||||
file.close();
|
file.close();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -60,7 +56,12 @@ int main(int ac, char **av)
|
|||||||
}
|
}
|
||||||
if (get_text(av[1], text))
|
if (get_text(av[1], text))
|
||||||
return (2);
|
return (2);
|
||||||
std::cout << text << std::endl;
|
|
||||||
replaced = replace_text(text.c_str(), av[2], av[3]);
|
replaced = replace_text(text.c_str(), av[2], av[3]);
|
||||||
std::cout << replaced << std::endl;
|
std::ofstream file((std::string(av[1]) + std::string(".replace")).c_str());
|
||||||
|
if (!file.is_open())
|
||||||
|
{
|
||||||
|
std::cerr << "sed: file error" << std::endl;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
file << replaced;
|
||||||
}
|
}
|
||||||
|
25
ex05/Makefile
Normal file
25
ex05/Makefile
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
CXX = c++
|
||||||
|
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
|
||||||
|
SRCDIR = src
|
||||||
|
OBJDIR = obj
|
||||||
|
NAME = ex05
|
||||||
|
|
||||||
|
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
|
46
ex05/src/Harl.cpp
Normal file
46
ex05/src/Harl.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include "Harl.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
std::cout << "I cannot believe adding extra bacon costs more money. You didn’t put enough bacon in my burger ! If you did, I wouldn’t be asking for more !" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Harl::warning()
|
||||||
|
{
|
||||||
|
std::cout << "I think I deserve to have some extra bacon for free. I’ve been coming for years whereas you started working here since last month." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Harl::error()
|
||||||
|
{
|
||||||
|
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
|
||||||
|
}
|
17
ex05/src/Harl.hpp
Normal file
17
ex05/src/Harl.hpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Harl
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
void debug();
|
||||||
|
void info();
|
||||||
|
void warning();
|
||||||
|
void error();
|
||||||
|
|
||||||
|
public:
|
||||||
|
void complain(std::string level);
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (Harl::*HarlMethod)();
|
19
ex05/src/main.cpp
Normal file
19
ex05/src/main.cpp
Normal 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
25
ex06/Makefile
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
CXX = c++
|
||||||
|
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
|
||||||
|
SRCDIR = src
|
||||||
|
OBJDIR = obj
|
||||||
|
NAME = ex06
|
||||||
|
|
||||||
|
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
|
51
ex06/src/Harl.cpp
Normal file
51
ex06/src/Harl.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include "Harl.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void Harl::complain(std::string level)
|
||||||
|
{
|
||||||
|
static const std::string strings[4] = {
|
||||||
|
"DEBUG",
|
||||||
|
"INFO",
|
||||||
|
"WARNING",
|
||||||
|
"ERROR"
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
for (; i < 4; i++)
|
||||||
|
if (strings[i] == level)
|
||||||
|
break;
|
||||||
|
switch(i) {
|
||||||
|
case 0:
|
||||||
|
debug();
|
||||||
|
case 1:
|
||||||
|
info();
|
||||||
|
case 2:
|
||||||
|
warning();
|
||||||
|
case 3:
|
||||||
|
error();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cout << "Not a level" << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Harl::debug()
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
std::cout << "I cannot believe adding extra bacon costs more money. You didn’t put enough bacon in my burger ! If you did, I wouldn’t be asking for more !" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Harl::warning()
|
||||||
|
{
|
||||||
|
std::cout << "I think I deserve to have some extra bacon for free. I’ve been coming for years whereas you started working here since last month." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Harl::error()
|
||||||
|
{
|
||||||
|
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
|
||||||
|
}
|
17
ex06/src/Harl.hpp
Normal file
17
ex06/src/Harl.hpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Harl
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
void debug();
|
||||||
|
void info();
|
||||||
|
void warning();
|
||||||
|
void error();
|
||||||
|
|
||||||
|
public:
|
||||||
|
void complain(std::string level);
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (Harl::*HarlMethod)();
|
10
ex06/src/main.cpp
Normal file
10
ex06/src/main.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "Harl.hpp"
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc <= 1)
|
||||||
|
return 1;
|
||||||
|
Harl harl;
|
||||||
|
harl.complain(argv[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user