diff --git a/ex02/AAnimal.cpp b/ex02/AAnimal.cpp new file mode 100644 index 0000000..63edb81 --- /dev/null +++ b/ex02/AAnimal.cpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AAnimal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:14:27 by adjoly #+# #+# */ +/* Updated: 2024/12/06 16:23:53 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "AAnimal.hpp" + +AAnimal::AAnimal(void) { + log("➕", "AAnimal", "", "construtor called"); +} + +AAnimal::~AAnimal() { + log("➖", "AAnimal", _type, "destructor called"); +} + +AAnimal::AAnimal(std::string type) : _type(type) { + log("➕", "AAnimal", type, "construtor called"); +} + +AAnimal::AAnimal(const AAnimal &cpy) { + log("➕", "AAnimal", _type, "copy construtor called"); + *this = cpy; +} + +AAnimal &AAnimal::operator=(const AAnimal &cpy) { + log("🟰", "AAnimal", _type, "copy assignment construtor called"); + if (this != &cpy) { + _type = cpy._type; + } + return (*this); +} + +std::string AAnimal::getType(void) const { + return (_type); +} diff --git a/ex02/AAnimal.hpp b/ex02/AAnimal.hpp new file mode 100644 index 0000000..47b3598 --- /dev/null +++ b/ex02/AAnimal.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AAnimal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/30 16:33:08 by adjoly #+# #+# */ +/* Updated: 2024/12/06 16:24:03 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include "Log.hpp" + +class AAnimal { + protected: + std::string _type; + + public: + AAnimal(void); + AAnimal(std::string); + virtual ~AAnimal(void); + AAnimal(const AAnimal &); + virtual AAnimal &operator=(const AAnimal &); + + virtual std::string getType(void) const; + + virtual void makeSound(void) const = 0; +}; diff --git a/ex02/Abstract b/ex02/Abstract new file mode 100755 index 0000000..1f911eb Binary files /dev/null and b/ex02/Abstract differ diff --git a/ex02/Brain.cpp b/ex02/Brain.cpp new file mode 100644 index 0000000..24a3af4 --- /dev/null +++ b/ex02/Brain.cpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/06 11:47:20 by adjoly #+# #+# */ +/* Updated: 2024/12/06 15:53:11 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Brain.hpp" + +Brain::Brain(void) { + log("➕", "Brain", "", "construtor called"); +} + +Brain::~Brain(void) { + log("➖", "Brain", "", "destructor called"); +} + +Brain::Brain(const Brain &cpy) { + log("➕", "Brain", "", "copy construtor called"); + *this = cpy; +} + +Brain &Brain::operator=(const Brain &cpy) { + log("🟰", "Brain", "", "copy assignment construtor called"); + if (this != &cpy) { + for (uint i = 0; i < 100; i++) { + this->_ideas[i] = cpy._ideas[i]; + } + } + return (*this); +} diff --git a/ex02/Brain.hpp b/ex02/Brain.hpp new file mode 100644 index 0000000..3454fa5 --- /dev/null +++ b/ex02/Brain.hpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/06 11:35:05 by adjoly #+# #+# */ +/* Updated: 2024/12/06 15:53:18 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include "Log.hpp" + +class Brain { + public: + Brain(void); + ~Brain(void); + Brain(const Brain &); + Brain &operator=(const Brain &); + + std::string _ideas[100]; +}; diff --git a/ex02/Cat.cpp b/ex02/Cat.cpp new file mode 100644 index 0000000..26c00d7 --- /dev/null +++ b/ex02/Cat.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:49:11 by adjoly #+# #+# */ +/* Updated: 2024/12/06 16:09:23 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Cat.hpp" + +Cat::Cat(void) : AAnimal("Cat"){ + _brain = new Brain(); + log("➕", "Cat", "", "construtor called"); +} + +Cat::~Cat(void) { + delete _brain; + log("➖", "Cat", "", "destructor called"); +} + +Cat::Cat(const Cat &cpy) : AAnimal("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"); +} + +void Cat::setIdea(std::string idea, uint i) { + _brain->_ideas[i] = idea; +} + +std::string Cat::getIdea(uint i) { + return (_brain->_ideas[i]); +} diff --git a/ex02/Cat.hpp b/ex02/Cat.hpp new file mode 100644 index 0000000..595323f --- /dev/null +++ b/ex02/Cat.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:06:08 by adjoly #+# #+# */ +/* Updated: 2024/12/06 16:08:59 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "AAnimal.hpp" +#include "Brain.hpp" + +class Cat : public AAnimal { + private: + Brain *_brain; + public: + Cat(void); + ~Cat(void); + Cat(const Cat &); + Cat &operator=(const Cat &); + + void makeSound(void) const; + void setIdea(std::string, uint); + std::string getIdea(uint); +}; diff --git a/ex02/Dog.cpp b/ex02/Dog.cpp new file mode 100644 index 0000000..f895a91 --- /dev/null +++ b/ex02/Dog.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 20:05:21 by adjoly #+# #+# */ +/* Updated: 2024/12/06 16:09:36 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Dog.hpp" + +Dog::Dog(void) : AAnimal("Dog"){ + _brain = new Brain(); + log("➕", "Dog", "", "construtor called"); +} + +Dog::~Dog(void) { + delete _brain; + log("➖", "Dog", "", "destructor called"); +} + +Dog::Dog(const Dog &cpy) : AAnimal("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"); +} + +void Dog::setIdea(std::string idea, uint i) { + _brain->_ideas[i] = idea; +} + +std::string Dog::getIdea(uint i) { + return (_brain->_ideas[i]); +} diff --git a/ex02/Dog.hpp b/ex02/Dog.hpp new file mode 100644 index 0000000..d38fcc3 --- /dev/null +++ b/ex02/Dog.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 20:06:20 by adjoly #+# #+# */ +/* Updated: 2024/12/06 16:08:53 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "AAnimal.hpp" +#include "Brain.hpp" + +class Dog : public AAnimal { + private: + Brain *_brain; + public: + Dog(void); + ~Dog(void); + Dog(const Dog &); + Dog &operator=(const Dog &); + + void makeSound(void) const; + void setIdea(std::string, uint); + std::string getIdea(uint); +}; diff --git a/ex02/Log.cpp b/ex02/Log.cpp new file mode 100644 index 0000000..9dbf721 --- /dev/null +++ b/ex02/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/ex02/Log.hpp b/ex02/Log.hpp new file mode 100644 index 0000000..5b8e7fe --- /dev/null +++ b/ex02/Log.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* log.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/06 12:01:21 by adjoly #+# #+# */ +/* Updated: 2024/12/06 13:12:13 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +typedef unsigned int uint; + +void log(std::string, std::string, std::string, std::string); diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..6d5d0a5 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,56 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2024/12/06 16:11:18 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +SHELL = bash + +NAME = Abstract + +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/ex02/WrongAnimal.cpp b/ex02/WrongAnimal.cpp new file mode 100644 index 0000000..418fa70 --- /dev/null +++ b/ex02/WrongAnimal.cpp @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:14:27 by adjoly #+# #+# */ +/* Updated: 2024/12/04 13:56:34 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongAnimal.hpp" + +WrongAnimal::WrongAnimal(void) { + log("➕", "WrongAnimal", "", "construtor called"); +} + +WrongAnimal::~WrongAnimal() { + log("➖", "WrongAnimal", _type, "destructor called"); +} + +WrongAnimal::WrongAnimal(std::string type) : _type(type) { + log("➕", "WrongAnimal", type, "construtor called"); +} + +WrongAnimal::WrongAnimal(const WrongAnimal &cpy) { + log("➕", "WrongAnimal", _type, "copy construtor called"); + *this = cpy; +} + +WrongAnimal &WrongAnimal::operator=(const WrongAnimal &cpy) { + log("🟰", "WrongAnimal", _type, "copy assignment construtor called"); + if (this != &cpy) { + _type = cpy._type; + } + return (*this); +} + +void WrongAnimal::makeSound(void) const { + log("🔊", "WrongAnimal", "", "making a wrong sound OOOOOOOOOOOOOOOoo"); +} + +std::string WrongAnimal::getType(void) const { + return (_type); +} diff --git a/ex02/WrongAnimal.hpp b/ex02/WrongAnimal.hpp new file mode 100644 index 0000000..14ba80e --- /dev/null +++ b/ex02/WrongAnimal.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/30 16:33:08 by adjoly #+# #+# */ +/* Updated: 2024/12/04 13:50:36 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class WrongAnimal { + protected: + std::string _type; + + public: + WrongAnimal(void); + ~WrongAnimal(void); + WrongAnimal(std::string); + WrongAnimal(const WrongAnimal &); + WrongAnimal &operator=(const WrongAnimal &); + + std::string getType(void) const; + + void makeSound(void) const; +}; + +void log(std::string, std::string, std::string, std::string); diff --git a/ex02/WrongCat.cpp b/ex02/WrongCat.cpp new file mode 100644 index 0000000..1121b3c --- /dev/null +++ b/ex02/WrongCat.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:49:11 by adjoly #+# #+# */ +/* Updated: 2024/12/06 15:55:37 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongCat.hpp" + +WrongCat::WrongCat(void) : WrongAnimal("WrongCat"){ + log("➕", "WrongCat", "", "construtor called"); +} + +WrongCat::~WrongCat(void) { + log("➖", "WrongCat", "", "destructor called"); +} + +WrongCat::WrongCat(const WrongCat &cpy) : WrongAnimal("WrongCat"){ + log("➕", "WrongCat", _type, "copy construtor called"); + *this = cpy; +} + +WrongCat &WrongCat::operator=(const WrongCat &cpy) { + log("🟰", "WrongCat", "", "copy assignment construtor called"); + if (this != &cpy) { + this->_type = cpy._type; + } + return (*this); +} + +void WrongCat::makeSound(void) const { + log("🔊", "WrongCat", "", "wwwwwwwwooooooooeeeeeeeeemmmmmmm"); +} diff --git a/ex02/WrongCat.hpp b/ex02/WrongCat.hpp new file mode 100644 index 0000000..6dd78a2 --- /dev/null +++ b/ex02/WrongCat.hpp @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 19:06:08 by adjoly #+# #+# */ +/* Updated: 2024/12/06 15:55:47 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "WrongAnimal.hpp" + +class WrongCat : public WrongAnimal { + public: + WrongCat(void); + ~WrongCat(void); + WrongCat(const WrongCat &); + WrongCat &operator=(const WrongCat &); + + void makeSound(void) const; +}; diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..231b667 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/06 11:20:59 by adjoly #+# #+# */ +/* Updated: 2024/12/06 16:21:49 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "AAnimal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" + +int main() { + const AAnimal* dog = new Dog(); + Cat* cat = new Cat(); + + cat->setIdea("I want to eat", 0); + cat->setIdea("I want to eat", 1); + cat->setIdea("I want to eat", 2); + cat->setIdea("I want to eat", 3); + cat->setIdea("I want to eat", 4); + cat->setIdea("I want to eat", 5); + cat->setIdea("I want to eat", 6); + dog->makeSound(); + cat->makeSound(); + log("🥩", "Idea", "Cat", cat->getIdea(0)); + log("🥩", "Idea", "Cat", cat->getIdea(1)); + log("🥩", "Idea", "Cat", cat->getIdea(2)); + log("🥩", "Idea", "Cat", cat->getIdea(3)); + log("🥩", "Idea", "Cat", cat->getIdea(4)); + log("🥩", "Idea", "Cat", cat->getIdea(5)); + log("🥩", "Idea", "Cat", cat->getIdea(6)); + + delete cat; + delete dog; + + return 0; +}