feat: adding the ex02

This commit is contained in:
Raphael 2025-02-15 19:22:12 +01:00
parent 921b3951d9
commit e78b8fbe33
14 changed files with 539 additions and 0 deletions

31
cpp04/ex02/Animal.cpp Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/30 13:21:01 by rparodi #+# #+# */
/* Updated: 2025/01/31 19:53:47 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Animal.hpp"
#include <iostream>
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;
}

30
cpp04/ex02/Animal.hpp Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <iostream>
class Animal {
public:
Animal();
virtual ~Animal();
virtual void makeSound() const;
std::string getType() const;
protected:
std::string type;
private:
};
#endif

31
cpp04/ex02/Brain.cpp Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 15:17:41 by rparodi #+# #+# */
/* Updated: 2025/02/07 17:51:50 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Brain.hpp"
#include <sstream>
#include <string>
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);
}

31
cpp04/ex02/Brain.hpp Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
class Brain {
public:
Brain();
~Brain();
std::string idea[100];
Brain& operator=(Brain &value);
protected:
private:
};
#endif

28
cpp04/ex02/Cat.cpp Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

31
cpp04/ex02/Cat.hpp Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
class Cat : public Animal {
public:
Cat();
~Cat();
virtual void makeSound() const;
protected:
private:
Brain *brain;
};
#endif

29
cpp04/ex02/Dog.cpp Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

32
cpp04/ex02/Dog.hpp Normal file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
class Dog : public Animal {
public:
Dog();
~Dog();
virtual void makeSound() const;
protected:
private:
Brain *brain;
};
#endif

128
cpp04/ex02/Makefile Normal file
View file

@ -0,0 +1,128 @@
# **************************************************************************** #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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}

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongAnimal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/30 13:21:01 by rparodi #+# #+# */
/* Updated: 2025/01/31 19:54:23 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "WrongAnimal.hpp"
#include <iostream>
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;
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongAnimal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <iostream>
class WrongAnimal {
public:
WrongAnimal();
virtual ~WrongAnimal();
virtual void makeSound() const;
std::string getType() const;
protected:
std::string type;
private:
};
#endif

26
cpp04/ex02/WrongCat.cpp Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongCat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

30
cpp04/ex02/WrongCat.hpp Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongCat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
class WrongCat : public WrongAnimal {
public:
WrongCat();
~WrongCat();
virtual void makeSound() const;
protected:
private:
};
#endif

51
cpp04/ex02/main.cpp Normal file
View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
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;
}