1
0

」 feat(Ex00): added a very cool feature !

This commit is contained in:
2024-12-01 20:24:38 +01:00
parent 467c3be3c3
commit c22dad6f83
15 changed files with 333 additions and 0 deletions

56
ex00/Makefile Normal file
View File

@ -0,0 +1,56 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

BIN
ex00/Polymorphism Executable file

Binary file not shown.

46
ex00/animal.cpp Normal file
View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* animal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

33
ex00/animal.hpp Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* animal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 16:33:08 by adjoly #+# #+# */
/* Updated: 2024/12/01 20:22:41 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
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);

38
ex00/cat.cpp Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");
}

25
ex00/cat.hpp Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
};

38
ex00/dog.cpp Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dog.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");
}

25
ex00/dog.hpp Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dog.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
};

22
ex00/log.cpp Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* log.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 16:30:53 by adjoly #+# #+# */
/* Updated: 2024/11/30 16:32:37 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <string>
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;
}

37
ex00/main.cpp Normal file
View File

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

2
ex00/obj/animal.d Normal file
View File

@ -0,0 +1,2 @@
obj/./animal.o: animal.cpp animal.hpp
animal.hpp:

3
ex00/obj/cat.d Normal file
View File

@ -0,0 +1,3 @@
obj/./cat.o: cat.cpp cat.hpp animal.hpp
cat.hpp:
animal.hpp:

3
ex00/obj/dog.d Normal file
View File

@ -0,0 +1,3 @@
obj/./dog.o: dog.cpp dog.hpp animal.hpp
dog.hpp:
animal.hpp:

1
ex00/obj/log.d Normal file
View File

@ -0,0 +1 @@
obj/./log.o: log.cpp

4
ex00/obj/main.d Normal file
View File

@ -0,0 +1,4 @@
obj/./main.o: main.cpp cat.hpp animal.hpp dog.hpp
cat.hpp:
animal.hpp:
dog.hpp: