debut d01 ex03
This commit is contained in:
61
d01/ex02/Makefile
Normal file
61
d01/ex02/Makefile
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
# . name = value . name is case sensitive #
|
||||||
|
# VARIABLES . or name = value \ . use VPATH only for .c #
|
||||||
|
# . value . or .cpp #
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
|
NAME = brain
|
||||||
|
|
||||||
|
CC = clang++
|
||||||
|
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 -g3
|
||||||
|
|
||||||
|
VPATH = $(D_SRCS)
|
||||||
|
|
||||||
|
LIBS =
|
||||||
|
|
||||||
|
INCLUDES = -I$(D_HEADERS)
|
||||||
|
|
||||||
|
D_SRCS = .
|
||||||
|
SRCS = main.cpp
|
||||||
|
|
||||||
|
D_HEADERS = .
|
||||||
|
HEADERS =
|
||||||
|
|
||||||
|
D_OBJS = builds
|
||||||
|
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
|
||||||
|
|
||||||
|
RM_D_OBJS = rm -rf $(D_OBJS)
|
||||||
|
ifeq "$(D_OBJS)" "."
|
||||||
|
RM_D_OBJS =
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
# . target: prerequisites . $@ : target #
|
||||||
|
# RULES . recipe . $< : 1st prerequisite #
|
||||||
|
# . recipe . $^ : all prerequisites #
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(D_OBJS):
|
||||||
|
mkdir $@
|
||||||
|
|
||||||
|
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
||||||
|
|
||||||
|
$(NAME): $(OBJS)
|
||||||
|
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(NAME)
|
||||||
|
$(RM_D_OBJS)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY : all clean fclean re bonus run valgrind
|
||||||
BIN
d01/ex02/brain
Executable file
BIN
d01/ex02/brain
Executable file
Binary file not shown.
26
d01/ex02/main.cpp
Normal file
26
d01/ex02/main.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
std::string brain = "HI THIS IS BRAIN";
|
||||||
|
std::string * stringPTR = &brain;
|
||||||
|
std::string & stringREF = brain;
|
||||||
|
|
||||||
|
// • L’adresse de la string en mémoire.
|
||||||
|
// • L’adresse stockée dans stringPTR.
|
||||||
|
// • L’adresse stockée dans stringREF.
|
||||||
|
// Puis :
|
||||||
|
// • La valeur de la string.
|
||||||
|
// • La valeur pointée par stringPTR.
|
||||||
|
// • La valeur pointée par stringREF.
|
||||||
|
|
||||||
|
std::cout << &brain << std::endl;
|
||||||
|
std::cout << stringPTR << std::endl;
|
||||||
|
std::cout << &stringREF << std::endl << std::endl;
|
||||||
|
std::cout << brain << std::endl;
|
||||||
|
std::cout << *stringPTR << std::endl;
|
||||||
|
std::cout << stringREF << std::endl;
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
15
d01/ex03/HumanA.cpp
Normal file
15
d01/ex03/HumanA.cpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include "HumanA.hpp"
|
||||||
|
|
||||||
|
HumanA::HumanA( std::string name, Weapon weapon ) {
|
||||||
|
|
||||||
|
this->_name = name;
|
||||||
|
this->_weapon = &weapon ;
|
||||||
|
|
||||||
|
}
|
||||||
|
HumanA::~HumanA() {}
|
||||||
|
|
||||||
|
void HumanA::attack( void ) {
|
||||||
|
|
||||||
|
std::cout << this->_name << " attacks with their " << this->_weapon->getType() << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
25
d01/ex03/HumanA.hpp
Normal file
25
d01/ex03/HumanA.hpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef HUMANA_HPP
|
||||||
|
# define HUMANA_HPP
|
||||||
|
|
||||||
|
#include "Weapon.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class HumanA {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
HumanA( std::string name, Weapon weapon );
|
||||||
|
~HumanA();
|
||||||
|
|
||||||
|
void attack( void );
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::string _name;
|
||||||
|
Weapon * _weapon;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
21
d01/ex03/HumanB.cpp
Normal file
21
d01/ex03/HumanB.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include "HumanB.hpp"
|
||||||
|
|
||||||
|
HumanB::HumanB( std::string name ) {
|
||||||
|
|
||||||
|
this->_name = name;
|
||||||
|
|
||||||
|
}
|
||||||
|
HumanB::~HumanB() {}
|
||||||
|
|
||||||
|
void setWeapon( Weapon weapon ) {
|
||||||
|
|
||||||
|
this->_weapon = &weapon;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void HumanB::attack( void ) {
|
||||||
|
|
||||||
|
std::cout << this->_name << " attacks with their " << this->_weapon->getType() << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
26
d01/ex03/HumanB.hpp
Normal file
26
d01/ex03/HumanB.hpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef HUMANB_HPP
|
||||||
|
# define HUMANB_HPP
|
||||||
|
|
||||||
|
#include "Weapon.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class HumanB {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
HumanB( std::string name );
|
||||||
|
~HumanB();
|
||||||
|
|
||||||
|
void attack( void );
|
||||||
|
void setWeapon( Weapon weapon );
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::string _name;
|
||||||
|
Weapon * _weapon;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
66
d01/ex03/Makefile
Normal file
66
d01/ex03/Makefile
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
# . name = value . name is case sensitive #
|
||||||
|
# VARIABLES . or name = value \ . use VPATH only for .c #
|
||||||
|
# . value . or .cpp #
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
|
NAME = war
|
||||||
|
|
||||||
|
CC = clang++
|
||||||
|
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 -g3
|
||||||
|
|
||||||
|
VPATH = $(D_SRCS)
|
||||||
|
|
||||||
|
LIBS =
|
||||||
|
|
||||||
|
INCLUDES = -I$(D_HEADERS)
|
||||||
|
|
||||||
|
D_SRCS = .
|
||||||
|
SRCS = main.cpp \
|
||||||
|
Weapon.cpp \
|
||||||
|
HumanA.cpp \
|
||||||
|
HumanB.cpp
|
||||||
|
|
||||||
|
D_HEADERS = .
|
||||||
|
HEADERS = Weapon.hpp \
|
||||||
|
HumanA.hpp \
|
||||||
|
HumanB.hpp
|
||||||
|
|
||||||
|
D_OBJS = builds
|
||||||
|
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
|
||||||
|
|
||||||
|
RM_D_OBJS = rm -rf $(D_OBJS)
|
||||||
|
ifeq "$(D_OBJS)" "."
|
||||||
|
RM_D_OBJS =
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
# . target: prerequisites . $@ : target #
|
||||||
|
# RULES . recipe . $< : 1st prerequisite #
|
||||||
|
# . recipe . $^ : all prerequisites #
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(D_OBJS):
|
||||||
|
mkdir $@
|
||||||
|
|
||||||
|
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
||||||
|
|
||||||
|
$(NAME): $(OBJS)
|
||||||
|
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(NAME)
|
||||||
|
$(RM_D_OBJS)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY : all clean fclean re bonus run valgrind
|
||||||
20
d01/ex03/Weapon.cpp
Normal file
20
d01/ex03/Weapon.cpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#include "Weapon.hpp"
|
||||||
|
|
||||||
|
Weapon::Weapon( std::string type ) {
|
||||||
|
|
||||||
|
this->_type = type;
|
||||||
|
|
||||||
|
}
|
||||||
|
Weapon::~Weapon() {}
|
||||||
|
|
||||||
|
std::string Weapon::getType( void ) {
|
||||||
|
|
||||||
|
return this->_type;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Weapon::setType( std::string type ) {
|
||||||
|
|
||||||
|
this->_type = type;
|
||||||
|
|
||||||
|
}
|
||||||
23
d01/ex03/Weapon.hpp
Normal file
23
d01/ex03/Weapon.hpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef WEAPON_HPP
|
||||||
|
# define WEAPON_HPP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Weapon {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Weapon( std::string type );
|
||||||
|
~Weapon();
|
||||||
|
|
||||||
|
std::string getType( void );
|
||||||
|
void setType( std::string type );
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::string _type;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
23
d01/ex03/main.cpp
Normal file
23
d01/ex03/main.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include "HumanA.hpp"
|
||||||
|
#include "HumanB.hpp"
|
||||||
|
#include "Weapon.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
Weapon club = Weapon("crude spiked club");
|
||||||
|
HumanA bob("Bob", club);
|
||||||
|
bob.attack();
|
||||||
|
club.setType("some other type of club");
|
||||||
|
bob.attack();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Weapon club = Weapon("crude spiked club");
|
||||||
|
HumanB jim("Jim");
|
||||||
|
jim.setWeapon(club);
|
||||||
|
jim.attack();
|
||||||
|
club.setType("some other type of club");
|
||||||
|
jim.attack();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user