feat: finishing the ex00 of the module 04
This commit is contained in:
parent
1c1e515ceb
commit
cbb34cf2e0
13 changed files with 181 additions and 36 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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>
|
||||
|
||||
Animal::Animal() {
|
||||
std::cout << "[Animal] Creating the class" << std::endl;
|
||||
std::cout << "[Animal]\tCreating the class" << std::endl;
|
||||
type = "";
|
||||
}
|
||||
|
||||
Animal::~Animal() {
|
||||
std::cout << "[Animal] Deleting the class" << std::endl;
|
||||
std::cout << "[Animal]\tDeleting the class" << std::endl;
|
||||
}
|
||||
|
||||
void Animal::makeSound() {
|
||||
if (type.compare("Cat"))
|
||||
std::cout << "🐱 | Meow Meow" << std::endl;
|
||||
else if (type.compare("Dog"))
|
||||
std::cout << "🐶 | Wouf Wouf" << std::endl;
|
||||
else
|
||||
std::string Animal::getType() const {
|
||||
return (type);
|
||||
}
|
||||
|
||||
void Animal::makeSound() const {
|
||||
std::cout << "🤔 | thinking how to make a sound" << std::endl;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 {
|
||||
public:
|
||||
Animal();
|
||||
~Animal();
|
||||
void makeSound();
|
||||
std::string getType();
|
||||
virtual ~Animal();
|
||||
virtual void makeSound() const;
|
||||
std::string getType() const;
|
||||
protected:
|
||||
std::string type;
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -6,21 +6,21 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
Cat::Cat() {
|
||||
std::cout << "[Cat] Creating the class" << std::endl;
|
||||
std::cout << "[Cat]\t\tCreating the class" << std::endl;
|
||||
type = "Cat";
|
||||
}
|
||||
|
||||
Cat::~Cat() {
|
||||
std::cout << "[Cat] Deleting the class" << std::endl;
|
||||
std::cout << "[Cat]\t\tDeleting the class" << std::endl;
|
||||
}
|
||||
|
||||
std::string Cat::getType() {
|
||||
return (type);
|
||||
void Cat::makeSound() const {
|
||||
std::cout << "🐱 | Meow Meow" << std::endl;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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:
|
||||
Cat();
|
||||
~Cat();
|
||||
virtual void makeSound() const;
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -6,21 +6,21 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
Dog::Dog() {
|
||||
std::cout << "[Dog] Creating the class" << std::endl;
|
||||
std::cout << "[Dog]\t\tCreating the class" << std::endl;
|
||||
type = "Dog";
|
||||
}
|
||||
|
||||
Dog::~Dog() {
|
||||
std::cout << "[Dog] Deleting the class" << std::endl;
|
||||
std::cout << "[Dog]\t\tDeleting the class" << std::endl;
|
||||
}
|
||||
|
||||
std::string Dog::getType() {
|
||||
return (type);
|
||||
void Dog::makeSound() const {
|
||||
std::cout << "🐶 | Wouf Wouf" << std::endl;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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:
|
||||
Dog();
|
||||
~Dog();
|
||||
std::string getType();
|
||||
virtual void makeSound() const;
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -12,20 +12,25 @@
|
|||
# Variables
|
||||
|
||||
# Name
|
||||
NAME = polyformism
|
||||
NAME = polymorphism
|
||||
|
||||
# Commands
|
||||
CXX = c++
|
||||
RM = rm -rf
|
||||
|
||||
# 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)
|
||||
CXXFLAGS += -MMD -g3
|
||||
# Flag to debug (TO REMOVE)
|
||||
# CXXFLAGS += -D DEBUG=1
|
||||
|
||||
# Sources
|
||||
SRC = main.cpp
|
||||
SRC = Animal.cpp \
|
||||
Cat.cpp \
|
||||
Dog.cpp \
|
||||
WrongAnimal.cpp \
|
||||
WrongCat.cpp \
|
||||
main.cpp
|
||||
|
||||
# Objects
|
||||
OBJDIRNAME = ./build
|
||||
|
|
|
|||
31
cpp04/ex00/WrongAnimal.cpp
Normal file
31
cpp04/ex00/WrongAnimal.cpp
Normal 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;
|
||||
}
|
||||
30
cpp04/ex00/WrongAnimal.hpp
Normal file
30
cpp04/ex00/WrongAnimal.hpp
Normal 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
26
cpp04/ex00/WrongCat.cpp
Normal 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
30
cpp04/ex00/WrongCat.hpp
Normal 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
|
||||
|
|
@ -6,22 +6,45 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "Dog.hpp"
|
||||
#include "Cat.hpp"
|
||||
#include "WrongAnimal.hpp"
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
int main() {
|
||||
std::cout << "\033[0;33m[ Building classes ]\033[0m" << std::endl;
|
||||
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();
|
||||
const WrongAnimal* k = new WrongAnimal();
|
||||
const WrongAnimal* l = new WrongCat();
|
||||
|
||||
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();
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
BIN
cpp04/ex00/polymorphism
Executable file
BIN
cpp04/ex00/polymorphism
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue