diff --git a/d01/ex02/Makefile b/d01/ex02/Makefile new file mode 100644 index 0000000..de53531 --- /dev/null +++ b/d01/ex02/Makefile @@ -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 diff --git a/d01/ex02/brain b/d01/ex02/brain new file mode 100755 index 0000000..27c03b4 Binary files /dev/null and b/d01/ex02/brain differ diff --git a/d01/ex02/main.cpp b/d01/ex02/main.cpp new file mode 100644 index 0000000..f760523 --- /dev/null +++ b/d01/ex02/main.cpp @@ -0,0 +1,26 @@ +#include +#include + +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); +} diff --git a/d01/ex03/HumanA.cpp b/d01/ex03/HumanA.cpp new file mode 100644 index 0000000..63fb078 --- /dev/null +++ b/d01/ex03/HumanA.cpp @@ -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; + +} diff --git a/d01/ex03/HumanA.hpp b/d01/ex03/HumanA.hpp new file mode 100644 index 0000000..ece7e04 --- /dev/null +++ b/d01/ex03/HumanA.hpp @@ -0,0 +1,25 @@ +#ifndef HUMANA_HPP +# define HUMANA_HPP + +#include "Weapon.hpp" +#include +#include + +class HumanA { + +public: + + HumanA( std::string name, Weapon weapon ); + ~HumanA(); + + void attack( void ); + +private: + + std::string _name; + Weapon * _weapon; + +}; + +#endif + diff --git a/d01/ex03/HumanB.cpp b/d01/ex03/HumanB.cpp new file mode 100644 index 0000000..23eb7c1 --- /dev/null +++ b/d01/ex03/HumanB.cpp @@ -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; + +} + diff --git a/d01/ex03/HumanB.hpp b/d01/ex03/HumanB.hpp new file mode 100644 index 0000000..bc920b3 --- /dev/null +++ b/d01/ex03/HumanB.hpp @@ -0,0 +1,26 @@ +#ifndef HUMANB_HPP +# define HUMANB_HPP + +#include "Weapon.hpp" +#include +#include + +class HumanB { + +public: + + HumanB( std::string name ); + ~HumanB(); + + void attack( void ); + void setWeapon( Weapon weapon ); + +private: + + std::string _name; + Weapon * _weapon; + +}; + +#endif + diff --git a/d01/ex03/Makefile b/d01/ex03/Makefile new file mode 100644 index 0000000..82360fa --- /dev/null +++ b/d01/ex03/Makefile @@ -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 diff --git a/d01/ex03/Weapon.cpp b/d01/ex03/Weapon.cpp new file mode 100644 index 0000000..36cab26 --- /dev/null +++ b/d01/ex03/Weapon.cpp @@ -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; + +} diff --git a/d01/ex03/Weapon.hpp b/d01/ex03/Weapon.hpp new file mode 100644 index 0000000..9b65b7c --- /dev/null +++ b/d01/ex03/Weapon.hpp @@ -0,0 +1,23 @@ +#ifndef WEAPON_HPP +# define WEAPON_HPP + +#include +#include + +class Weapon { + +public: + + Weapon( std::string type ); + ~Weapon(); + + std::string getType( void ); + void setType( std::string type ); + +private: + + std::string _type; + +}; + +#endif diff --git a/d01/ex03/main.cpp b/d01/ex03/main.cpp new file mode 100644 index 0000000..539263c --- /dev/null +++ b/d01/ex03/main.cpp @@ -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; +}