From e78b8fbe338e1f1aed83083f11e82d96c2e7902a Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 15 Feb 2025 19:22:12 +0100 Subject: [PATCH] feat: adding the ex02 --- cpp04/ex02/Animal.cpp | 31 +++++++++ cpp04/ex02/Animal.hpp | 30 +++++++++ cpp04/ex02/Brain.cpp | 31 +++++++++ cpp04/ex02/Brain.hpp | 31 +++++++++ cpp04/ex02/Cat.cpp | 28 ++++++++ cpp04/ex02/Cat.hpp | 31 +++++++++ cpp04/ex02/Dog.cpp | 29 +++++++++ cpp04/ex02/Dog.hpp | 32 ++++++++++ cpp04/ex02/Makefile | 128 +++++++++++++++++++++++++++++++++++++ cpp04/ex02/WrongAnimal.cpp | 31 +++++++++ cpp04/ex02/WrongAnimal.hpp | 30 +++++++++ cpp04/ex02/WrongCat.cpp | 26 ++++++++ cpp04/ex02/WrongCat.hpp | 30 +++++++++ cpp04/ex02/main.cpp | 51 +++++++++++++++ 14 files changed, 539 insertions(+) create mode 100644 cpp04/ex02/Animal.cpp create mode 100644 cpp04/ex02/Animal.hpp create mode 100644 cpp04/ex02/Brain.cpp create mode 100644 cpp04/ex02/Brain.hpp create mode 100644 cpp04/ex02/Cat.cpp create mode 100644 cpp04/ex02/Cat.hpp create mode 100644 cpp04/ex02/Dog.cpp create mode 100644 cpp04/ex02/Dog.hpp create mode 100644 cpp04/ex02/Makefile create mode 100644 cpp04/ex02/WrongAnimal.cpp create mode 100644 cpp04/ex02/WrongAnimal.hpp create mode 100644 cpp04/ex02/WrongCat.cpp create mode 100644 cpp04/ex02/WrongCat.hpp create mode 100644 cpp04/ex02/main.cpp diff --git a/cpp04/ex02/Animal.cpp b/cpp04/ex02/Animal.cpp new file mode 100644 index 0000000..b80a082 --- /dev/null +++ b/cpp04/ex02/Animal.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/30 13:21:01 by rparodi #+# #+# */ +/* Updated: 2025/01/31 19:53:47 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include + +Animal::Animal() { + std::cout << "[Animal]\tCreating the class" << std::endl; + type = ""; +} + +Animal::~Animal() { + std::cout << "[Animal]\tDeleting the class" << std::endl; +} + +std::string Animal::getType() const { + return (type); +} + +void Animal::makeSound() const { + std::cout << "🤔 | thinking how to make a sound" << std::endl; +} diff --git a/cpp04/ex02/Animal.hpp b/cpp04/ex02/Animal.hpp new file mode 100644 index 0000000..bfb8474 --- /dev/null +++ b/cpp04/ex02/Animal.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/28 17:44:54 by rparodi #+# #+# */ +/* Updated: 2025/01/31 19:23:05 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ANIMAL_HPP +#define ANIMAL_HPP + +#include +#include + +class Animal { + public: + Animal(); + virtual ~Animal(); + virtual void makeSound() const; + std::string getType() const; + protected: + std::string type; + private: +}; + +#endif diff --git a/cpp04/ex02/Brain.cpp b/cpp04/ex02/Brain.cpp new file mode 100644 index 0000000..3ed559c --- /dev/null +++ b/cpp04/ex02/Brain.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/06 15:17:41 by rparodi #+# #+# */ +/* Updated: 2025/02/07 17:51:50 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Brain.hpp" +#include +#include + +Brain::Brain() { + for (int i = 0; i < 100; i++) { + std::ostringstream oss; + oss << "I'm thinking about the number " << (i + 1); + idea[i] = oss.str(); + } +} + +Brain::~Brain() { + +} + +Brain& Brain::operator=(Brain &value) { + return (value); +} diff --git a/cpp04/ex02/Brain.hpp b/cpp04/ex02/Brain.hpp new file mode 100644 index 0000000..3e7e09a --- /dev/null +++ b/cpp04/ex02/Brain.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/06 15:17:49 by rparodi #+# #+# */ +/* Updated: 2025/02/07 17:51:43 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BRAIN_HPP +#define BRAIN_HPP + +#include + +class Brain { + public: + Brain(); + ~Brain(); + std::string idea[100]; + Brain& operator=(Brain &value); + + protected: + + private: + +}; + +#endif diff --git a/cpp04/ex02/Cat.cpp b/cpp04/ex02/Cat.cpp new file mode 100644 index 0000000..c045f91 --- /dev/null +++ b/cpp04/ex02/Cat.cpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/30 13:32:43 by rparodi #+# #+# */ +/* Updated: 2025/02/10 12:22:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Cat.hpp" + +Cat::Cat() { + std::cout << "[Cat]\t\tCreating the class" << std::endl; + type = "Cat"; + brain = new Brain(); +} + +Cat::~Cat() { + delete brain; + std::cout << "[Cat]\t\tDeleting the class" << std::endl; +} + +void Cat::makeSound() const { + std::cout << "🐱 | Meow Meow" << std::endl; +} diff --git a/cpp04/ex02/Cat.hpp b/cpp04/ex02/Cat.hpp new file mode 100644 index 0000000..78e9530 --- /dev/null +++ b/cpp04/ex02/Cat.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/28 17:44:48 by rparodi #+# #+# */ +/* Updated: 2025/02/10 12:20:07 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CAT_HPP +#define CAT_HPP + +#include "Animal.hpp" +#include "Brain.hpp" +#include + +class Cat : public Animal { + public: + Cat(); + ~Cat(); + virtual void makeSound() const; + protected: + + private: + Brain *brain; +}; + +#endif diff --git a/cpp04/ex02/Dog.cpp b/cpp04/ex02/Dog.cpp new file mode 100644 index 0000000..1eb69f7 --- /dev/null +++ b/cpp04/ex02/Dog.cpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/30 13:40:30 by rparodi #+# #+# */ +/* Updated: 2025/02/10 12:19:22 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Dog.hpp" +#include "Brain.hpp" + +Dog::Dog() { + std::cout << "[Dog]\t\tCreating the class" << std::endl; + type = "Dog"; + brain = new Brain(); +} + +Dog::~Dog() { + delete brain; + std::cout << "[Dog]\t\tDeleting the class" << std::endl; +} + +void Dog::makeSound() const { + std::cout << "🐶 | Wouf Wouf" << std::endl; +} diff --git a/cpp04/ex02/Dog.hpp b/cpp04/ex02/Dog.hpp new file mode 100644 index 0000000..ba98c5f --- /dev/null +++ b/cpp04/ex02/Dog.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/28 17:44:51 by rparodi #+# #+# */ +/* Updated: 2025/02/07 17:51:39 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DOG_HPP +#define DOG_HPP + +#include "Animal.hpp" +#include "Brain.hpp" +#include + +class Dog : public Animal { + public: + Dog(); + ~Dog(); + virtual void makeSound() const; + protected: + + private: + Brain *brain; + +}; + +#endif diff --git a/cpp04/ex02/Makefile b/cpp04/ex02/Makefile new file mode 100644 index 0000000..12e6eaa --- /dev/null +++ b/cpp04/ex02/Makefile @@ -0,0 +1,128 @@ +# **************************************************************************** # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2024/12/21 19:50:58 by rparodi ### ########.fr # +# # +# **************************************************************************** # + +# Variables + +# Name +NAME = polyformism + +# Commands +CXX = c++ +RM = rm -rf + +# Flags +# Mandatory flags for 42 +CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ +# Flags to debug and have the dependences (can be removed for correction) +CXXFLAGS += -MMD -g3 +# Flag to debug (TO REMOVE) +# CXXFLAGS += -D DEBUG=1 +# Sources +SRC = Animal.cpp \ + Brain.cpp \ + Cat.cpp \ + Dog.cpp \ + WrongAnimal.cpp \ + WrongCat.cpp \ + main.cpp \ + +# Objects +OBJDIRNAME = ./build +OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o)) + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +# Rules + +# All (make all) +all: header $(NAME) footer + +# Bonus (make bonus) +bonus: header $(OBJ) footer + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Clean (make clean) +clean: + @printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n' + @printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n' + @$(RM) $(OBJDIRNAME) + +# Clean (make fclean) +fclean: clean + @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header fclean all + +# Dependences for all +$(NAME): $(OBJ) + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Creating the objects +$(OBJDIRNAME)/%.o: %.cpp + @mkdir -p $(dir $@) + @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' + @$(CXX) $(CXXFLAGS) -o $@ -c $< + +# Header +header: + @clear + @printf '\n\n' + @printf '$(GOLD) ******* ****** ******* $(END)\n' + @printf '$(GOLD) ****** *** ******* $(END)\n' + @printf '$(GOLD) ******* * ******* $(END)\n' + @printf '$(GOLD) ****** ******* $(END)\n' + @printf '$(GOLD) ******* ******* $(END)\n' + @printf '$(GOLD) ******************* ******* * $(END)\n' + @printf '$(GOLD) ******************* ******* *** $(END)\n' + @printf '$(GOLD) ****** ******* ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GREY) Made by rparodi$(END)\n\n' + +# Footer +footer: + @printf "\n" + @printf "$(GOLD) ,_ _,$(END)\n" + @printf "$(GOLD) | \\___//|$(END)\n" + @printf "$(GOLD) |=6 6=|$(END)\n" + @printf "$(GOLD) \\=._Y_.=/$(END)\n" + @printf "$(GOLD) ) \` ( ,$(END)\n" + @printf "$(GOLD) / \\ (('$(END)\n" + @printf "$(GOLD) | | ))$(END)\n" + @printf "$(GOLD) /| | | |\\_//$(END)\n" + @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" + @printf "$(GOLD) '\"' '\"'$(END)\n" + @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + +clangd: + @echo \ + "CompileFlags:\n" \ + " Add:\n" \ + " - \"-std=c++98 -Wall -Wextra -Werror\"\n" \ + " - \"-I"$(shell pwd)"/includes\"\n" \ + " - \"-xc++\"\n" \ + > .clangd + +# Phony +.PHONY: all bonus clean fclean re clangd +-include ${OBJ:.o=.d} diff --git a/cpp04/ex02/WrongAnimal.cpp b/cpp04/ex02/WrongAnimal.cpp new file mode 100644 index 0000000..0afff32 --- /dev/null +++ b/cpp04/ex02/WrongAnimal.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/30 13:21:01 by rparodi #+# #+# */ +/* Updated: 2025/01/31 19:54:23 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongAnimal.hpp" +#include + +WrongAnimal::WrongAnimal() { + std::cout << "[WrongAnimal]\tCreating the class" << std::endl; + type = ""; +} + +WrongAnimal::~WrongAnimal() { + std::cout << "[WrongAnimal]\tDeleting the class" << std::endl; +} + +std::string WrongAnimal::getType() const { + return (type); +} + +void WrongAnimal::makeSound() const { + std::cout << "🦊 | What does the fox say ?" << std::endl; +} diff --git a/cpp04/ex02/WrongAnimal.hpp b/cpp04/ex02/WrongAnimal.hpp new file mode 100644 index 0000000..3e13252 --- /dev/null +++ b/cpp04/ex02/WrongAnimal.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/28 17:44:54 by rparodi #+# #+# */ +/* Updated: 2025/01/31 19:34:06 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGANIMAL_HPP +#define WRONGANIMAL_HPP + +#include +#include + +class WrongAnimal { + public: + WrongAnimal(); + virtual ~WrongAnimal(); + virtual void makeSound() const; + std::string getType() const; + protected: + std::string type; + private: +}; + +#endif diff --git a/cpp04/ex02/WrongCat.cpp b/cpp04/ex02/WrongCat.cpp new file mode 100644 index 0000000..e7abd44 --- /dev/null +++ b/cpp04/ex02/WrongCat.cpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/30 13:32:43 by rparodi #+# #+# */ +/* Updated: 2025/01/31 19:54:35 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongCat.hpp" + +WrongCat::WrongCat() { + std::cout << "[WrongCat]\tCreating the class" << std::endl; + type = "WrongCat"; +} + +WrongCat::~WrongCat() { + std::cout << "[WrongCat]\tDeleting the class" << std::endl; +} + +void WrongCat::makeSound() const { + std::cout << "🙀 | Help me I transform my self in cat" << std::endl; +} diff --git a/cpp04/ex02/WrongCat.hpp b/cpp04/ex02/WrongCat.hpp new file mode 100644 index 0000000..d9e55b6 --- /dev/null +++ b/cpp04/ex02/WrongCat.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/28 17:44:48 by rparodi #+# #+# */ +/* Updated: 2025/01/31 19:37:00 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGCAT_HPP +#define WRONGCAT_HPP + +#include "WrongAnimal.hpp" +#include + +class WrongCat : public WrongAnimal { + public: + WrongCat(); + ~WrongCat(); + virtual void makeSound() const; + protected: + + private: + +}; + +#endif diff --git a/cpp04/ex02/main.cpp b/cpp04/ex02/main.cpp new file mode 100644 index 0000000..93f3c9d --- /dev/null +++ b/cpp04/ex02/main.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/28 17:08:09 by rparodi #+# #+# */ +/* Updated: 2025/02/10 12:40:53 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" +#include "WrongAnimal.hpp" +#include "WrongCat.hpp" +#include + +int main() { + const unsigned int n = 42; + if (n % 2 != 0) { + std::cerr << "n must be an even number, if not i can't create the same number of Cat and Animal" << std::endl; + return (1); + } + + std::cout << "\033[0;33m[ Building classes ]\033[0m" << std::endl; + Animal *animals[n]; + for (unsigned int i = 0; i < n; i++) { + std::cout << "(" << i + 1 << ") "; + if (i % 2 == 0) { + animals[i] = new Dog(); + } else { + animals[i] = new Cat(); + } + } + + std::cout << "\033[0;33m[ Making sounds ]\033[0m" << std::endl; + for (unsigned int i = 0; i < n; i++) { + std::cout << "(" << i + 1 << ") "; + animals[i]->makeSound(); + } + + std::cout << "\033[0;33m[ Deleting classes ]\033[0m" << std::endl; + for (unsigned int i = 0; i < n; i++) { + std::cout << "(" << i + 1 << ") "; + delete animals[i]; + } + + return 0; +}