diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..a19c5bc --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,56 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2024/11/30 16:22:30 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +SHELL = bash + +NAME = Polymorphism + +CC = c++ + +OBJSDIR = obj/ + +SRCS = $(shell find . -name '*.cpp') + +OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) + +FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP + +RED = \033[0;31m +GREEN = \033[0;32m +YELLOW = \033[1;33m +PURPLE = \e[0;35m +NC = \033[0m +DELETE = \x1B[2K\r + +all: $(NAME) + +$(NAME): $(OBJS) + @$(CC) $(FLAGS) $(OBJS) -o $(NAME) + @printf "$(YELLOW)「✨」($(NAME)) Program compiled\n" + +$(OBJSDIR)%.o: %.cpp + @mkdir -p $(@D) + @$(CC) $(FLAGS) -c $< -o $@ + @printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n" + +clean: + @rm -f $(OBJS) + @printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n" + +fclean: clean + @rm -f $(NAME) + @rm -Rf $(OBJSDIR) + @printf "$(RED)「🗑️」($(NAME)) Program deleted\n" + +re: fclean all + +.PHONY: clean fclean all re diff --git a/ex00/Polymorphism b/ex00/Polymorphism new file mode 100755 index 0000000..fc647e8 Binary files /dev/null and b/ex00/Polymorphism differ diff --git a/ex00/animal.cpp b/ex00/animal.cpp new file mode 100644 index 0000000..dada99c --- /dev/null +++ b/ex00/animal.cpp @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* animal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:14:27 by adjoly #+# #+# */ +/* Updated: 2024/12/01 20:22:32 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "animal.hpp" + +Animal::Animal(void) { + log("➕", "Animal", "", "construtor called"); +} + +Animal::~Animal() { + log("➖", "Animal", _type, "destructor called"); +} + +Animal::Animal(std::string type) : _type(type) { + log("➕", "Animal", type, "construtor called"); +} + +Animal::Animal(const Animal &cpy) { + log("➕", "Animal", _type, "copy construtor called"); + *this = cpy; +} + +Animal &Animal::operator=(const Animal &cpy) { + log("🟰", "Animal", _type, "copy assignment construtor called"); + if (this != &cpy) { + _type = cpy._type; + } + return (*this); +} + +void Animal::makeSound(void) const { + log("🔊", "Animal", "", "making a sound MEOWMEOW"); +} + +std::string Animal::getType(void) const { + return (_type); +} diff --git a/ex00/animal.hpp b/ex00/animal.hpp new file mode 100644 index 0000000..7fb07d5 --- /dev/null +++ b/ex00/animal.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* animal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/30 16:33:08 by adjoly #+# #+# */ +/* Updated: 2024/12/01 20:22:41 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class Animal { + protected: + std::string _type; + + public: + Animal(void); + ~Animal(void); + Animal(std::string); + Animal(const Animal &); + Animal &operator=(const Animal &); + + std::string getType(void) const; + + virtual void makeSound(void) const; +}; + +void log(std::string, std::string, std::string, std::string); diff --git a/ex00/cat.cpp b/ex00/cat.cpp new file mode 100644 index 0000000..e23227b --- /dev/null +++ b/ex00/cat.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:49:11 by adjoly #+# #+# */ +/* Updated: 2024/12/01 20:18:55 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cat.hpp" + +Cat::Cat(void) : Animal("Cat"){ + log("➕", "Cat", "", "construtor called"); +} + +Cat::~Cat(void) { + log("➖", "Cat", "", "destructor called"); +} + +Cat::Cat(const Cat &cpy) : Animal("Cat"){ + log("➕", "Cat", _type, "copy construtor called"); + *this = cpy; +} + +Cat &Cat::operator=(const Cat &cpy) { + log("🟰", "Cat", "", "copy assignment construtor called"); + if (this != &cpy) { + this->_type = cpy._type; + } + return (*this); +} + +void Cat::makeSound(void) const { + log("🔊", "Cat", "", "Mmmmmmeeeeeeeeoooooooowwwwww"); +} diff --git a/ex00/cat.hpp b/ex00/cat.hpp new file mode 100644 index 0000000..399a22f --- /dev/null +++ b/ex00/cat.hpp @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:06:08 by adjoly #+# #+# */ +/* Updated: 2024/12/01 20:18:42 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "animal.hpp" + +class Cat : public Animal { + public: + Cat(void); + ~Cat(void); + Cat(const Cat &); + Cat &operator=(const Cat &); + + void makeSound(void) const; +}; diff --git a/ex00/dog.cpp b/ex00/dog.cpp new file mode 100644 index 0000000..ac57842 --- /dev/null +++ b/ex00/dog.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 20:05:21 by adjoly #+# #+# */ +/* Updated: 2024/12/01 20:18:27 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "dog.hpp" + +Dog::Dog(void) : Animal("Dog"){ + log("➕", "Dog", "", "construtor called"); +} + +Dog::~Dog(void) { + log("➖", "Dog", "", "destructor called"); +} + +Dog::Dog(const Dog &cpy) : Animal("Dog"){ + log("➕", "Dog", _type, "copy construtor called"); + *this = cpy; +} + +Dog &Dog::operator=(const Dog &cpy) { + log("🟰", "Dog", "", "copy assignment construtor called"); + if (this != &cpy) { + this->_type = cpy._type; + } + return (*this); +} + +void Dog::makeSound(void) const { + log("🔊", "Dog", "", "woof"); +} diff --git a/ex00/dog.hpp b/ex00/dog.hpp new file mode 100644 index 0000000..a32cc7c --- /dev/null +++ b/ex00/dog.hpp @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 20:06:20 by adjoly #+# #+# */ +/* Updated: 2024/12/01 20:18:35 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "animal.hpp" + +class Dog : public Animal { + public: + Dog(void); + ~Dog(void); + Dog(const Dog &); + Dog &operator=(const Dog &); + + void makeSound(void) const; +}; diff --git a/ex00/log.cpp b/ex00/log.cpp new file mode 100644 index 0000000..9dbf721 --- /dev/null +++ b/ex00/log.cpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* log.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/30 16:30:53 by adjoly #+# #+# */ +/* Updated: 2024/11/30 16:32:37 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void log(std::string emoji, std::string what, std::string who, std::string str) { + if (who.empty()) + std::cout << "「" << emoji << "」" << what << ": " << str << std::endl; + else + std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str << std::endl; +} + diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..2dddb6c --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/30 16:21:28 by adjoly #+# #+# */ +/* Updated: 2024/12/01 20:23:26 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cat.hpp" +#include "dog.hpp" +#include + +//int main(void) { +// cat cutie; +// dog dogo; +// +// cutie.makeSound(); +// dogo.makeSound(); +//} + +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(); + + return 0; +} diff --git a/ex00/obj/animal.d b/ex00/obj/animal.d new file mode 100644 index 0000000..b663f5a --- /dev/null +++ b/ex00/obj/animal.d @@ -0,0 +1,2 @@ +obj/./animal.o: animal.cpp animal.hpp +animal.hpp: diff --git a/ex00/obj/cat.d b/ex00/obj/cat.d new file mode 100644 index 0000000..8de22f8 --- /dev/null +++ b/ex00/obj/cat.d @@ -0,0 +1,3 @@ +obj/./cat.o: cat.cpp cat.hpp animal.hpp +cat.hpp: +animal.hpp: diff --git a/ex00/obj/dog.d b/ex00/obj/dog.d new file mode 100644 index 0000000..62240f3 --- /dev/null +++ b/ex00/obj/dog.d @@ -0,0 +1,3 @@ +obj/./dog.o: dog.cpp dog.hpp animal.hpp +dog.hpp: +animal.hpp: diff --git a/ex00/obj/log.d b/ex00/obj/log.d new file mode 100644 index 0000000..da9c7eb --- /dev/null +++ b/ex00/obj/log.d @@ -0,0 +1 @@ +obj/./log.o: log.cpp diff --git a/ex00/obj/main.d b/ex00/obj/main.d new file mode 100644 index 0000000..ece6bf2 --- /dev/null +++ b/ex00/obj/main.d @@ -0,0 +1,4 @@ +obj/./main.o: main.cpp cat.hpp animal.hpp dog.hpp +cat.hpp: +animal.hpp: +dog.hpp: