diff --git a/d00/ex00/Makefile b/d00/ex00/Makefile index 0803c5b..e512c07 100644 --- a/d00/ex00/Makefile +++ b/d00/ex00/Makefile @@ -6,8 +6,21 @@ NAME = megaphone -CC = c++ -CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 +#TYPE = c +TYPE = cpp + +ifeq "$(TYPE)" "c" + CC = c + EXT = c +else ifeq "$(TYPE)" "cpp" + CC = c++ + EXT = cpp +endif + +CFLAGS = -Wall -Wextra -Werror $(INCLUDES) +ifeq "$(TYPE)" "cpp" + CFLAGS += -std=c++98 +endif VPATH = $(D_SRCS) @@ -22,11 +35,12 @@ D_SRCS = . SRCS = megaphone.cpp D_OBJS = builds -OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o) +OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) -RM_D_OBJS = rm -rf $(D_OBJS) ifeq "$(D_OBJS)" "." -RM_D_OBJS = + RM_OBJS = rm -f $(OBJS) +else + RM_OBJS = rm -rf $(D_OBJS) endif @@ -38,7 +52,7 @@ endif all: $(NAME) -$(D_OBJS)/%.o: %.cpp | $(D_OBJS) +$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) $(CC) $(CFLAGS) -c $< -o $@ $(D_OBJS): @@ -50,11 +64,10 @@ $(NAME): $(OBJS) $(CC) $(OBJS) -o $@ $(LIBS) clean: - rm -f $(OBJS) + $(RM_OBJS) fclean: clean rm -f $(NAME) - $(RM_D_OBJS) re: fclean all diff --git a/d00/ex00/megaphone b/d00/ex00/megaphone new file mode 100755 index 0000000..b2f63e0 Binary files /dev/null and b/d00/ex00/megaphone differ diff --git a/d04/Animal.cpp b/d04/Animal.cpp new file mode 100644 index 0000000..3f9502f --- /dev/null +++ b/d04/Animal.cpp @@ -0,0 +1,46 @@ +#include "Animal.hpp" + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Animal::Animal() { + type = "animal"; + return; +} + +Animal::Animal( Animal const & src ) { + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Animal::~Animal() { + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Animal & Animal::operator=( Animal const & rhs ) { + if ( this != &rhs ) + { + type = rhs.getType(); + } + return *this; +} + +std::string Animal::getType() const {return type;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void Animal::makeSound() const { + std::cout << "*sound*\n"; +} + diff --git a/d04/Animal.hpp b/d04/Animal.hpp new file mode 100644 index 0000000..6d72ba1 --- /dev/null +++ b/d04/Animal.hpp @@ -0,0 +1,28 @@ +#ifndef ANIMAL_HPP +# define ANIMAL_HPP + +#include +#include + +class Animal { + +public: + + Animal( void ); + Animal( Animal const & src ); + ~Animal( void ); + Animal & operator=( Animal const & rhs ); + + virtual void makeSound() const; + std::string getType() const; + +protected: + + std::string type; + +private: + +}; + +#endif + diff --git a/d04/Cat.cpp b/d04/Cat.cpp new file mode 100644 index 0000000..71212f1 --- /dev/null +++ b/d04/Cat.cpp @@ -0,0 +1,42 @@ +#include "Cat.hpp" + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Cat::Cat() { + type = "cat"; + return; +} + +Cat::Cat( Cat const & src ) { + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Cat::~Cat() { + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Cat & Cat::operator=( Cat const & rhs ) { + Animal::operator=(rhs); + + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void Cat::makeSound() const { + std::cout << "*miaow*\n"; +} + diff --git a/d04/Cat.hpp b/d04/Cat.hpp new file mode 100644 index 0000000..0c7012e --- /dev/null +++ b/d04/Cat.hpp @@ -0,0 +1,27 @@ +#ifndef CAT_HPP +# define CAT_HPP + +# include "Animal.hpp" + +#include +#include + +class Cat : public Animal { + +public: + + Cat( void ); + Cat( Cat const & src ); + ~Cat( void ); + Cat & operator=( Cat const & rhs ); + + void makeSound() const; + +protected: + +private: + +}; + +#endif + diff --git a/d04/Dog.cpp b/d04/Dog.cpp new file mode 100644 index 0000000..dc4f62a --- /dev/null +++ b/d04/Dog.cpp @@ -0,0 +1,41 @@ +#include "Dog.hpp" + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Dog::Dog() { + type = "dog"; + return; +} + +Dog::Dog( Dog const & src ) { + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Dog::~Dog() { + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Dog & Dog::operator=( Dog const & rhs ) { + Animal::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void Dog::makeSound() const { + std::cout << "*woof*\n"; +} + diff --git a/d04/Dog.hpp b/d04/Dog.hpp new file mode 100644 index 0000000..6a977df --- /dev/null +++ b/d04/Dog.hpp @@ -0,0 +1,27 @@ +#ifndef DOG_HPP +# define DOG_HPP + +# include "Animal.hpp" + +# include +# include + +class Dog : public Animal { + +public: + + Dog( void ); + Dog( Dog const & src ); + ~Dog( void ); + Dog & operator=( Dog const & rhs ); + + void makeSound() const; + +protected: + +private: + +}; + +#endif + diff --git a/d04/Makefile b/d04/Makefile new file mode 100644 index 0000000..50eca1c --- /dev/null +++ b/d04/Makefile @@ -0,0 +1,84 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = poly + +#TYPE = c +TYPE = cpp + +ifeq "$(TYPE)" "c" + CC = c + EXT = c +else ifeq "$(TYPE)" "cpp" + CC = c++ + EXT = cpp +endif + +CFLAGS = -Wall -Wextra -Werror $(INCLUDES) +ifeq "$(TYPE)" "cpp" + CFLAGS += -std=c++98 +endif + +VPATH = $(D_SRCS) + +LIBS = + +INCLUDES = -I$(D_HEADERS) + +D_SRCS = . +SRCS = main.cpp \ + Animal.cpp \ + Dog.cpp \ + Cat.cpp \ + WrongAnimal.cpp \ + WrongCat.cpp + +D_HEADERS = . +HEADERS = Animal.hpp \ + Dog.hpp \ + Cat.hpp \ + WrongAnimal.hpp \ + WrongCat.hpp + +D_OBJS = builds +OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) + +ifeq "$(D_OBJS)" "." + RM_OBJS = rm -f $(OBJS) +else + RM_OBJS = rm -rf $(D_OBJS) +endif + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . target: prerequisites . $@ : target # +# RULES . recipe . $< : 1st prerequisite # +# . recipe . $^ : all prerequisites # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +all: $(NAME) + +$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) + $(CC) $(CFLAGS) -c $< -o $@ + +$(D_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(D_HEADERS)/%) + +$(NAME): $(OBJS) + $(CC) $(OBJS) -o $@ $(LIBS) + +clean: + $(RM_OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d04/WrongAnimal.cpp b/d04/WrongAnimal.cpp new file mode 100644 index 0000000..61928ff --- /dev/null +++ b/d04/WrongAnimal.cpp @@ -0,0 +1,50 @@ +#include "WrongAnimal.hpp" + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +WrongAnimal::WrongAnimal() { + type = "wrong_animal"; + return; +} + +WrongAnimal::WrongAnimal( WrongAnimal const & src ) { + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +WrongAnimal::~WrongAnimal() { + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +WrongAnimal & WrongAnimal::operator=( WrongAnimal const & rhs ) { + if ( this != &rhs ) + { + type = rhs.getType(); + } + return *this; +} + +/********************************************* + * ACCESSORS + *********************************************/ + +std::string WrongAnimal::getType() const {return type;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void WrongAnimal::makeSound() const { + std::cout << "*sound*\n"; +} + diff --git a/d04/WrongAnimal.hpp b/d04/WrongAnimal.hpp new file mode 100644 index 0000000..8dd40db --- /dev/null +++ b/d04/WrongAnimal.hpp @@ -0,0 +1,26 @@ +#ifndef WRONG_ANIMAL_HPP +# define WRONG_ANIMAL_HPP + +#include +#include + +class WrongAnimal { + +public: + + WrongAnimal( void ); + WrongAnimal( WrongAnimal const & src ); + ~WrongAnimal( void ); + WrongAnimal & operator=( WrongAnimal const & rhs ); + + void makeSound() const; + std::string getType() const; + +protected: + + std::string type; + +}; + +#endif + diff --git a/d04/WrongCat.cpp b/d04/WrongCat.cpp new file mode 100644 index 0000000..8d13eef --- /dev/null +++ b/d04/WrongCat.cpp @@ -0,0 +1,40 @@ +#include "WrongCat.hpp" + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +WrongCat::WrongCat() { + type = "wrong_cat"; + return; +} + +WrongCat::WrongCat( WrongCat const & src ) { + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +WrongCat::~WrongCat() { + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +WrongCat & WrongCat::operator=( WrongCat const & rhs ) { + WrongAnimal::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void WrongCat::makeSound() const { + std::cout << "*miaow*\n"; +} diff --git a/d04/WrongCat.hpp b/d04/WrongCat.hpp new file mode 100644 index 0000000..158267d --- /dev/null +++ b/d04/WrongCat.hpp @@ -0,0 +1,23 @@ +#ifndef WRONG_CAT_HPP +# define WRONG_CAT_HPP + +# include "WrongAnimal.hpp" + +#include +#include + +class WrongCat : public WrongAnimal { + +public: + + WrongCat( void ); + WrongCat( WrongCat const & src ); + ~WrongCat( void ); + WrongCat & operator=( WrongCat const & rhs ); + + void makeSound() const; + +}; + +#endif + diff --git a/d04/main.cpp b/d04/main.cpp new file mode 100644 index 0000000..672ad4a --- /dev/null +++ b/d04/main.cpp @@ -0,0 +1,52 @@ +#include "Animal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" +#include "WrongAnimal.hpp" +#include "WrongCat.hpp" + +#include +#include + +int main() { + const Animal* meta = new Animal(); + const Animal* j = new Dog(); + const Animal* i = new Cat(); + std::cout << j->getType() << " " << std::endl; + std::cout << i->getType() << " " << std::endl; + i->makeSound(); //will output the cat sound! + j->makeSound(); + meta->makeSound(); + + { + std::cout << std::endl; + const Cat* i = new Cat(); + const Animal* j = new Cat(); + std::cout << j->getType() << " " << std::endl; + std::cout << i->getType() << " " << std::endl; + i->makeSound(); //will output the cat sound! + j->makeSound(); + } + + { + std::cout << std::endl; + const WrongAnimal* i = new WrongAnimal(); + const WrongAnimal* j = new WrongCat(); + std::cout << i->getType() << " " << std::endl; + std::cout << j->getType() << " " << std::endl; + i->makeSound(); //will output the cat sound! + j->makeSound(); + } + + { + std::cout << std::endl; + const WrongCat* i = new WrongCat(); + const WrongAnimal* j = new WrongCat(); + std::cout << j->getType() << " " << std::endl; + std::cout << i->getType() << " " << std::endl; + i->makeSound(); //will output the cat sound! + j->makeSound(); + } + + return 0; +} + diff --git a/d04/poly b/d04/poly new file mode 100755 index 0000000..386c7e6 Binary files /dev/null and b/d04/poly differ