feat: finishing the ex00 of the module 04

This commit is contained in:
Raphael 2025-01-31 19:58:32 +01:00
parent 1c1e515ceb
commit cbb34cf2e0
13 changed files with 181 additions and 36 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/30 13:21:01 by rparodi #+# #+# */ /* Created: 2025/01/30 13:21:01 by rparodi #+# #+# */
/* Updated: 2025/01/30 13:37:51 by rparodi ### ########.fr */ /* Updated: 2025/01/31 19:53:47 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,19 +14,18 @@
#include <iostream> #include <iostream>
Animal::Animal() { Animal::Animal() {
std::cout << "[Animal] Creating the class" << std::endl; std::cout << "[Animal]\tCreating the class" << std::endl;
type = ""; type = "";
} }
Animal::~Animal() { Animal::~Animal() {
std::cout << "[Animal] Deleting the class" << std::endl; std::cout << "[Animal]\tDeleting the class" << std::endl;
} }
void Animal::makeSound() { std::string Animal::getType() const {
if (type.compare("Cat")) return (type);
std::cout << "🐱 | Meow Meow" << std::endl; }
else if (type.compare("Dog"))
std::cout << "🐶 | Wouf Wouf" << std::endl; void Animal::makeSound() const {
else
std::cout << "🤔 | thinking how to make a sound" << std::endl; std::cout << "🤔 | thinking how to make a sound" << std::endl;
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 17:44:54 by rparodi #+# #+# */ /* Created: 2025/01/28 17:44:54 by rparodi #+# #+# */
/* Updated: 2025/01/30 13:42:23 by rparodi ### ########.fr */ /* Updated: 2025/01/31 19:23:05 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,9 +19,9 @@
class Animal { class Animal {
public: public:
Animal(); Animal();
~Animal(); virtual ~Animal();
void makeSound(); virtual void makeSound() const;
std::string getType(); std::string getType() const;
protected: protected:
std::string type; std::string type;
private: private:

View file

@ -6,21 +6,21 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/30 13:32:43 by rparodi #+# #+# */ /* Created: 2025/01/30 13:32:43 by rparodi #+# #+# */
/* Updated: 2025/01/30 13:38:46 by rparodi ### ########.fr */ /* Updated: 2025/01/31 19:54:03 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Cat.hpp" #include "Cat.hpp"
Cat::Cat() { Cat::Cat() {
std::cout << "[Cat] Creating the class" << std::endl; std::cout << "[Cat]\t\tCreating the class" << std::endl;
type = "Cat"; type = "Cat";
} }
Cat::~Cat() { Cat::~Cat() {
std::cout << "[Cat] Deleting the class" << std::endl; std::cout << "[Cat]\t\tDeleting the class" << std::endl;
} }
std::string Cat::getType() { void Cat::makeSound() const {
return (type); std::cout << "🐱 | Meow Meow" << std::endl;
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 17:44:48 by rparodi #+# #+# */ /* Created: 2025/01/28 17:44:48 by rparodi #+# #+# */
/* Updated: 2025/01/30 13:41:27 by rparodi ### ########.fr */ /* Updated: 2025/01/31 18:24:18 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,7 @@ class Cat : public Animal {
public: public:
Cat(); Cat();
~Cat(); ~Cat();
virtual void makeSound() const;
protected: protected:
private: private:

View file

@ -6,21 +6,21 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/30 13:40:30 by rparodi #+# #+# */ /* Created: 2025/01/30 13:40:30 by rparodi #+# #+# */
/* Updated: 2025/01/30 13:40:51 by rparodi ### ########.fr */ /* Updated: 2025/01/31 19:53:02 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Dog.hpp" #include "Dog.hpp"
Dog::Dog() { Dog::Dog() {
std::cout << "[Dog] Creating the class" << std::endl; std::cout << "[Dog]\t\tCreating the class" << std::endl;
type = "Dog"; type = "Dog";
} }
Dog::~Dog() { Dog::~Dog() {
std::cout << "[Dog] Deleting the class" << std::endl; std::cout << "[Dog]\t\tDeleting the class" << std::endl;
} }
std::string Dog::getType() { void Dog::makeSound() const {
return (type); std::cout << "🐶 | Wouf Wouf" << std::endl;
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 17:44:51 by rparodi #+# #+# */ /* Created: 2025/01/28 17:44:51 by rparodi #+# #+# */
/* Updated: 2025/01/30 13:32:28 by rparodi ### ########.fr */ /* Updated: 2025/01/31 18:24:07 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@ class Dog : public Animal {
public: public:
Dog(); Dog();
~Dog(); ~Dog();
std::string getType(); virtual void makeSound() const;
protected: protected:
private: private:

View file

@ -12,20 +12,25 @@
# Variables # Variables
# Name # Name
NAME = polyformism NAME = polymorphism
# Commands # Commands
CXX = c++ CXX = c++
RM = rm -rf RM = rm -rf
# Flags # Flags
# Mandatory flags for 42 CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ # 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) # Flags to debug and have the dependences (can be removed for correction)
CXXFLAGS += -MMD -g3 CXXFLAGS += -MMD -g3
# Flag to debug (TO REMOVE)
# CXXFLAGS += -D DEBUG=1
# Sources # Sources
SRC = main.cpp SRC = Animal.cpp \
Cat.cpp \
Dog.cpp \
WrongAnimal.cpp \
WrongCat.cpp \
main.cpp
# Objects # Objects
OBJDIRNAME = ./build OBJDIRNAME = ./build

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/ex00/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/ex00/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

View file

@ -6,22 +6,45 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 17:08:09 by rparodi #+# #+# */ /* Created: 2025/01/28 17:08:09 by rparodi #+# #+# */
/* Updated: 2025/01/30 13:12:32 by rparodi ### ########.fr */ /* Updated: 2025/01/31 19:52:09 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Animal.hpp" #include "Animal.hpp"
#include "Dog.hpp" #include "Dog.hpp"
#include "Cat.hpp" #include "Cat.hpp"
#include "WrongAnimal.hpp"
#include "WrongCat.hpp"
int main() { int main() {
std::cout << "\033[0;33m[ Building classes ]\033[0m" << std::endl;
const Animal* meta = new Animal(); const Animal* meta = new Animal();
const Animal* j = new Dog(); const Animal* j = new Dog();
const Animal* i = new Cat(); const Animal* i = new Cat();
std::cout << j->getType() << " " << std::endl; const WrongAnimal* k = new WrongAnimal();
std::cout << i->getType() << " " << std::endl; const WrongAnimal* l = new WrongCat();
i->makeSound(); //will output the cat sound!
j->makeSound(); std::cout << std::endl << "\033[0;33m[ Testing the getType method ]\033[0m" << std::endl;
std::cout << "Type of j:\t" << j->getType() << std::endl;
std::cout << "Type of i:\t" << i->getType() << std::endl;
std::cout << "Type of l:\t" << l->getType() << std::endl;
std::cout << std::endl << "\033[0;33m[ Testing the makeSound method ]\033[0m" << std::endl;
std::cout << "Sound of meta:\t";
meta->makeSound(); meta->makeSound();
std::cout << std::endl << "Sound of i:\t";
i->makeSound();
std::cout << std::endl << "Sound of j:\t";
j->makeSound();
std::cout << std::endl << "Sound of k:\t";
k->makeSound();
std::cout << std::endl << "Sound of l:\t";
l->makeSound();
std::cout << std::endl << "\033[0;33m[ Deleting classes ]\033[0m" << std::endl;
delete meta;
delete j;
delete i;
delete k;
delete l;
return 0; return 0;
} }

BIN
cpp04/ex00/polymorphism Executable file

Binary file not shown.