mirror of
https://github.com/Ladebeze66/cpp-partie-1.git
synced 2026-11-09 13:17:15 +01:00
ccp
This commit is contained in:
18
cpp04/ex00/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp04/ex00/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "linux-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
24
cpp04/ex00/.vscode/launch.json
vendored
Normal file
24
cpp04/ex00/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"externalConsole": false,
|
||||
"cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex00",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex00/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
67
cpp04/ex00/.vscode/settings.json
vendored
Normal file
67
cpp04/ex00/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
{
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "",
|
||||
"C_Cpp_Runner.useMsvc": false,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false,
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"compare": "cpp",
|
||||
"functional": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp"
|
||||
}
|
||||
}
|
||||
48
cpp04/ex00/Animal.cpp
Normal file
48
cpp04/ex00/Animal.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Animal.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 19:25:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:06:06 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
|
||||
Animal::Animal() : type("Undefined")
|
||||
{
|
||||
std::cout << GREEN << "Animal constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
Animal::Animal(const Animal ©) : type(copy.type)
|
||||
{
|
||||
std::cout << GREEN << "Animal copy constructor called. Type is copied as " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Animal& Animal::operator=(const Animal &operator_aff)// Protection contre l'auto-affectation
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->type = operator_aff.type;
|
||||
}
|
||||
std::cout << "Animal assignment operator called. Type is set to " << type << "." << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Animal::~Animal()
|
||||
{
|
||||
std::cout << RED << "Animal destructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
std::string Animal::getType() const
|
||||
{
|
||||
return (this->type);
|
||||
}
|
||||
|
||||
void Animal::makeSound() const
|
||||
{
|
||||
std::cout << MAGENTA << "Some animal sound!" << RESET << std::endl;
|
||||
}
|
||||
44
cpp04/ex00/Animal.hpp
Normal file
44
cpp04/ex00/Animal.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Animal.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 19:09:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:31:25 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ANIMAL_HPP
|
||||
#define ANIMAL_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class Animal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
Animal();
|
||||
Animal(const Animal ©);
|
||||
virtual Animal& operator=(const Animal &operator_aff);
|
||||
virtual ~Animal();
|
||||
|
||||
std::string getType() const;
|
||||
virtual void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
41
cpp04/ex00/Cat.cpp
Normal file
41
cpp04/ex00/Cat.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:52:48 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:54:23 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Cat.hpp"
|
||||
|
||||
Cat::Cat()
|
||||
{
|
||||
type = "Cat";
|
||||
std::cout << GREEN << "Cat default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Cat::Cat(const Cat ©) : Animal(copy)
|
||||
{
|
||||
std::cout << CYAN << "Cat copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Cat& Cat::operator=(const Cat &operator_aff)
|
||||
{
|
||||
Animal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "Cat assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Cat::~Cat()
|
||||
{
|
||||
std::cout << RED << "Cat destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Cat::makeSound() const
|
||||
{
|
||||
std::cout << "Miaouhh! Miaouhh!" << std::endl;
|
||||
}
|
||||
29
cpp04/ex00/Cat.hpp
Normal file
29
cpp04/ex00/Cat.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:17:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:18:06 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CAT_HPP
|
||||
#define CAT_HPP
|
||||
|
||||
#include "Animal.hpp"
|
||||
|
||||
class Cat : public Animal
|
||||
{
|
||||
public:
|
||||
Cat();
|
||||
Cat(const Cat ©);
|
||||
virtual Cat& operator=(const Cat &operator_aff);
|
||||
virtual ~Cat();
|
||||
|
||||
void makeSound() const override;
|
||||
};
|
||||
|
||||
#endif
|
||||
41
cpp04/ex00/Dog.cpp
Normal file
41
cpp04/ex00/Dog.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Dog.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:50:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:54:33 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Dog.hpp"
|
||||
|
||||
Dog::Dog()
|
||||
{
|
||||
type = "Dog";
|
||||
std::cout << GREEN << "Dog default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Dog::Dog(const Dog ©) : Animal(copy)
|
||||
{
|
||||
std::cout << CYAN << "Dog copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Dog& Dog::operator=(const Dog &operator_aff)
|
||||
{
|
||||
Animal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "Dog assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Dog::~Dog()
|
||||
{
|
||||
std::cout << RED << "Dog destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Dog::makeSound() const
|
||||
{
|
||||
std::cout << "Woof! Woof!" << std::endl;
|
||||
}
|
||||
29
cpp04/ex00/Dog.hpp
Normal file
29
cpp04/ex00/Dog.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Dog.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:06:42 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:16:31 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef DOG_HPP
|
||||
#define DOG_HPP
|
||||
|
||||
#include "Animal.hpp"
|
||||
|
||||
class Dog : public Animal
|
||||
{
|
||||
public:
|
||||
Dog();
|
||||
Dog(const Dog ©);
|
||||
virtual Dog& operator=(const Dog &operator_aff);
|
||||
virtual ~Dog();
|
||||
|
||||
void makeSound() const override;
|
||||
};
|
||||
|
||||
#endif
|
||||
66
cpp04/ex00/Makefile
Normal file
66
cpp04/ex00/Makefile
Normal file
@@ -0,0 +1,66 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/31 13:03:33 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\
|
||||
echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\
|
||||
echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\
|
||||
echo "( ( ( ( ( ( ( ("
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = animal
|
||||
|
||||
SRC = Animal.cpp \
|
||||
Cat.cpp \
|
||||
Dog.cpp \
|
||||
WrongAnimal.cpp \
|
||||
WrongCat.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./animal" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
48
cpp04/ex00/WrongAnimal.cpp
Normal file
48
cpp04/ex00/WrongAnimal.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongAnimal.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:55:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:57:57 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
WrongAnimal::WrongAnimal() : type("WrongAnimal")
|
||||
{
|
||||
std::cout << GREEN << "WrongAnimal default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongAnimal::WrongAnimal(const WrongAnimal ©) : type(copy.type)
|
||||
{
|
||||
std::cout << CYAN << "WrongAnimal copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongAnimal& WrongAnimal::operator=(const WrongAnimal &operator_aff)
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->type = operator_aff.type;
|
||||
}
|
||||
std::cout << MAGENTA << "WrongAnimal assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
WrongAnimal::~WrongAnimal()
|
||||
{
|
||||
std::cout << RED << "WrongAnimal destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
std::string WrongAnimal::getType() const
|
||||
{
|
||||
return (this->type);
|
||||
}
|
||||
|
||||
void WrongAnimal::makeSound() const
|
||||
{
|
||||
std::cout << "WrongAnimal makes a wrong sound!" << std::endl;
|
||||
}
|
||||
44
cpp04/ex00/WrongAnimal.hpp
Normal file
44
cpp04/ex00/WrongAnimal.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongAnimal.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:19:34 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:55:37 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef WRONGANIMAL_HPP
|
||||
#define WRONGANIMAL_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class WrongAnimal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
WrongAnimal();
|
||||
WrongAnimal(const WrongAnimal ©);
|
||||
WrongAnimal& operator=(const WrongAnimal &operator_aff);
|
||||
~WrongAnimal();
|
||||
|
||||
std::string getType() const;
|
||||
void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
41
cpp04/ex00/WrongCat.cpp
Normal file
41
cpp04/ex00/WrongCat.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongCat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:58:15 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:59:48 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
WrongCat::WrongCat()
|
||||
{
|
||||
type = "WrongCat";
|
||||
std::cout << GREEN << "WrongCat default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongCat::WrongCat(const WrongCat ©) : WrongAnimal(copy)
|
||||
{
|
||||
std::cout << CYAN << "WrongCat copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongCat& WrongCat::operator=(const WrongCat &operator_aff)
|
||||
{
|
||||
WrongAnimal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "WrongCat assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
WrongCat::~WrongCat()
|
||||
{
|
||||
std::cout << RED << "WrongCat destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void WrongCat::makeSound() const
|
||||
{
|
||||
std::cout << "WrongCat makes a wrong sound!" << std::endl;
|
||||
}
|
||||
29
cpp04/ex00/WrongCat.hpp
Normal file
29
cpp04/ex00/WrongCat.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongCat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:22:35 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:31:44 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef WRONGCAT_HPP
|
||||
#define WRONGCAT_HPP
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
class WrongCat : public WrongAnimal
|
||||
{
|
||||
public:
|
||||
WrongCat();
|
||||
WrongCat(const WrongCat ©);
|
||||
WrongCat& operator=(const WrongCat &operator_aff);
|
||||
~WrongCat();
|
||||
|
||||
void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
62
cpp04/ex00/main.cpp
Normal file
62
cpp04/ex00/main.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 13:00:09 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 13:19:56 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "Dog.hpp"
|
||||
#include "Cat.hpp"
|
||||
#include "WrongAnimal.hpp"
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
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();
|
||||
j->makeSound();
|
||||
meta->makeSound();
|
||||
|
||||
delete meta;
|
||||
delete j;
|
||||
delete i;
|
||||
|
||||
std::cout << "\n--- Tests WrongAnimal & WrongCat ---\n" << std::endl;
|
||||
|
||||
const WrongAnimal* wrongMeta = new WrongAnimal();
|
||||
const WrongAnimal* wrongCat = new WrongCat();
|
||||
|
||||
std::cout << wrongCat->getType() << " " << std::endl;
|
||||
wrongCat->makeSound();
|
||||
wrongMeta->makeSound();
|
||||
|
||||
delete wrongMeta;
|
||||
delete wrongCat;
|
||||
|
||||
std::cout << "\n--- Tests supplémentaires pour Dog & Cat ---\n" << std::endl;
|
||||
|
||||
Dog dog1, dog2;
|
||||
Cat cat1, cat2;
|
||||
|
||||
dog1.makeSound();
|
||||
cat1.makeSound();
|
||||
|
||||
Dog dog3(dog1);
|
||||
dog3.makeSound();
|
||||
|
||||
cat2 = cat1;
|
||||
cat2.makeSound();
|
||||
|
||||
return (0);
|
||||
}
|
||||
18
cpp04/ex01/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp04/ex01/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "linux-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
24
cpp04/ex01/.vscode/launch.json
vendored
Normal file
24
cpp04/ex01/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"externalConsole": false,
|
||||
"cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex01",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex01/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
105
cpp04/ex01/.vscode/settings.json
vendored
Normal file
105
cpp04/ex01/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
{
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "",
|
||||
"C_Cpp_Runner.useMsvc": false,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false,
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"random": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ostream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
}
|
||||
48
cpp04/ex01/Animal.cpp
Normal file
48
cpp04/ex01/Animal.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Animal.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 19:25:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:06:06 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
|
||||
Animal::Animal() : type("Undefined")
|
||||
{
|
||||
std::cout << GREEN << "Animal constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
Animal::Animal(const Animal ©) : type(copy.type)
|
||||
{
|
||||
std::cout << GREEN << "Animal copy constructor called. Type is copied as " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Animal& Animal::operator=(const Animal &operator_aff)// Protection contre l'auto-affectation
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->type = operator_aff.type;
|
||||
}
|
||||
std::cout << "Animal assignment operator called. Type is set to " << type << "." << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Animal::~Animal()
|
||||
{
|
||||
std::cout << RED << "Animal destructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
std::string Animal::getType() const
|
||||
{
|
||||
return (this->type);
|
||||
}
|
||||
|
||||
void Animal::makeSound() const
|
||||
{
|
||||
std::cout << MAGENTA << "Some animal sound!" << RESET << std::endl;
|
||||
}
|
||||
44
cpp04/ex01/Animal.hpp
Normal file
44
cpp04/ex01/Animal.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Animal.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 19:09:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:32:05 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ANIMAL_HPP
|
||||
#define ANIMAL_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class Animal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
Animal();
|
||||
Animal(const Animal ©);
|
||||
virtual Animal& operator=(const Animal &operator_aff);
|
||||
virtual ~Animal();
|
||||
|
||||
std::string getType() const;
|
||||
virtual void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
59
cpp04/ex01/Brain.cpp
Normal file
59
cpp04/ex01/Brain.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Brain.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 14:06:58 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 15:09:21 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Brain.hpp"
|
||||
|
||||
Brain::Brain()
|
||||
{
|
||||
// Initialisation des idées
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
ideas[i] = "Idea " + std::to_string(i);
|
||||
//std::cout << GREEN << "Initialising Brain Idea #" << i << ": " << ideas[i] << RESET << std::endl;
|
||||
}
|
||||
std::cout << GREEN << "Brain constructed with 100 ideas." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Brain::Brain(const Brain ©)
|
||||
{
|
||||
*this = copy;
|
||||
std::cout << CYAN << "Brain copy constructor called." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Brain& Brain::operator=(const Brain &operator_aff)
|
||||
{
|
||||
std::cout << MAGENTA << "Brain assignment operator called." << RESET << std::endl;
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
this->ideas[i] = operator_aff.ideas[i];
|
||||
std::cout << "Copying Idea #" << i << ": " << this->ideas[i] << std::endl;
|
||||
}
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Brain::~Brain()
|
||||
{
|
||||
std::cout << RED << "Brain destructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Brain::setIdea(std::string idea, int i)
|
||||
{
|
||||
this->ideas[i]=idea;
|
||||
}
|
||||
|
||||
std::string Brain::getIdea(int i) const
|
||||
{
|
||||
return (this->ideas[i]);
|
||||
}
|
||||
43
cpp04/ex01/Brain.hpp
Normal file
43
cpp04/ex01/Brain.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Brain.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 14:01:08 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:32:17 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BRAIN_HPP
|
||||
#define BRAIN_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class Brain
|
||||
{
|
||||
private:
|
||||
std::string ideas[100];
|
||||
|
||||
public:
|
||||
Brain();
|
||||
Brain(const Brain ©);
|
||||
Brain& operator=(const Brain &operator_aff);
|
||||
~Brain();
|
||||
void setIdea(std::string idea, int i);
|
||||
std::string getIdea(int i) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
47
cpp04/ex01/Cat.cpp
Normal file
47
cpp04/ex01/Cat.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:52:48 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 15:26:47 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Cat.hpp"
|
||||
|
||||
Cat::Cat()
|
||||
{
|
||||
this->type = "Cat";
|
||||
this->brain = new Brain();
|
||||
std::cout << GREEN << "Cat default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Cat::Cat(const Cat ©) : Animal(copy), brain(new Brain(*copy.brain))
|
||||
{
|
||||
std::cout << CYAN << "Cat copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Cat& Cat::operator=(const Cat &operator_aff)
|
||||
{
|
||||
Animal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "Cat assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Cat::~Cat()
|
||||
{
|
||||
std::cout << RED << "Cat destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Cat::makeSound() const
|
||||
{
|
||||
std::cout << "Miaouhh! Miaouhh!" << std::endl;
|
||||
}
|
||||
|
||||
Brain* Cat::getBrain(void) const
|
||||
{
|
||||
return (this->brain);
|
||||
}
|
||||
34
cpp04/ex01/Cat.hpp
Normal file
34
cpp04/ex01/Cat.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:17:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 15:11:08 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CAT_HPP
|
||||
#define CAT_HPP
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "Brain.hpp"
|
||||
|
||||
class Cat : public Animal
|
||||
{
|
||||
private:
|
||||
Brain* brain;
|
||||
|
||||
public:
|
||||
Cat();
|
||||
Cat(const Cat ©);
|
||||
virtual Cat& operator=(const Cat &operator_aff);
|
||||
virtual ~Cat();
|
||||
|
||||
void makeSound() const override;
|
||||
Brain* getBrain(void) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
47
cpp04/ex01/Dog.cpp
Normal file
47
cpp04/ex01/Dog.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Dog.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:50:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 15:28:27 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Dog.hpp"
|
||||
|
||||
Dog::Dog()
|
||||
{
|
||||
this->type = "Dog";
|
||||
this->brain = new Brain();
|
||||
std::cout << GREEN << "Dog default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Dog::Dog(const Dog ©) : Animal(copy), brain(new Brain(*copy.brain))
|
||||
{
|
||||
std::cout << CYAN << "Dog copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Dog& Dog::operator=(const Dog &operator_aff)
|
||||
{
|
||||
Animal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "Dog assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Dog::~Dog()
|
||||
{
|
||||
std::cout << RED << "Dog destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Dog::makeSound() const
|
||||
{
|
||||
std::cout << "Woof! Woof!" << std::endl;
|
||||
}
|
||||
|
||||
Brain* Dog::getBrain(void) const
|
||||
{
|
||||
return (this->brain);
|
||||
}
|
||||
34
cpp04/ex01/Dog.hpp
Normal file
34
cpp04/ex01/Dog.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Dog.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:06:42 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 15:12:01 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef DOG_HPP
|
||||
#define DOG_HPP
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "Brain.hpp"
|
||||
|
||||
class Dog : public Animal
|
||||
{
|
||||
private:
|
||||
Brain* brain;
|
||||
|
||||
public:
|
||||
Dog();
|
||||
Dog(const Dog ©);
|
||||
virtual Dog& operator=(const Dog &operator_aff);
|
||||
virtual ~Dog();
|
||||
|
||||
void makeSound() const override;
|
||||
Brain* getBrain(void) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
73
cpp04/ex01/Makefile
Normal file
73
cpp04/ex01/Makefile
Normal file
@@ -0,0 +1,73 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/31 14:46:49 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " ██▓ ▄▄▄ ▓█████▄▓█████ ▄▄▄▄ ▓█████▒███████▓█████";\
|
||||
echo " ▓██▒ ▒████▄ ▒██▀ ██▓█ ▀▓█████▄▓█ ▀▒ ▒ ▒ ▄▀▓█ ▀";\
|
||||
echo " ▒██░ ▒██ ▀█▄ ░██ █▒███ ▒██▒ ▄█▒███ ░ ▒ ▄▀▒░▒███";\
|
||||
echo " ▒██░ ░██▄▄▄▄██░▓█▄ ▒▓█ ▄▒██░█▀ ▒▓█ ▄ ▄▀▒ ▒▓█ ▄";\
|
||||
echo " ░██████▓█ ▓██░▒████▓░▒████░▓█ ▀█░▒████▒███████░▒████▒";\
|
||||
echo " ░ ▒░▓ ▒▒ ▓▒█░▒▒▓ ▒░░ ▒░ ░▒▓███▀░░ ▒░ ░▒▒ ▓░▒░░░ ▒░ ░";\
|
||||
echo " ░ ░ ▒ ░▒ ▒▒ ░░ ▒ ▒ ░ ░ ▒░▒ ░ ░ ░ ░░▒ ▒ ░ ▒░ ░ ░";\
|
||||
echo " ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░";\
|
||||
echo " ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ ░";\
|
||||
echo " ░ ░ ░"
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = brain
|
||||
|
||||
SRC = Animal.cpp \
|
||||
Cat.cpp \
|
||||
Dog.cpp \
|
||||
WrongAnimal.cpp \
|
||||
WrongCat.cpp \
|
||||
Brain.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./brain" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
48
cpp04/ex01/WrongAnimal.cpp
Normal file
48
cpp04/ex01/WrongAnimal.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongAnimal.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:55:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:57:57 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
WrongAnimal::WrongAnimal() : type("WrongAnimal")
|
||||
{
|
||||
std::cout << GREEN << "WrongAnimal default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongAnimal::WrongAnimal(const WrongAnimal ©) : type(copy.type)
|
||||
{
|
||||
std::cout << CYAN << "WrongAnimal copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongAnimal& WrongAnimal::operator=(const WrongAnimal &operator_aff)
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->type = operator_aff.type;
|
||||
}
|
||||
std::cout << MAGENTA << "WrongAnimal assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
WrongAnimal::~WrongAnimal()
|
||||
{
|
||||
std::cout << RED << "WrongAnimal destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
std::string WrongAnimal::getType() const
|
||||
{
|
||||
return (this->type);
|
||||
}
|
||||
|
||||
void WrongAnimal::makeSound() const
|
||||
{
|
||||
std::cout << "WrongAnimal makes a wrong sound!" << std::endl;
|
||||
}
|
||||
44
cpp04/ex01/WrongAnimal.hpp
Normal file
44
cpp04/ex01/WrongAnimal.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongAnimal.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:19:34 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:55:37 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef WRONGANIMAL_HPP
|
||||
#define WRONGANIMAL_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class WrongAnimal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
WrongAnimal();
|
||||
WrongAnimal(const WrongAnimal ©);
|
||||
WrongAnimal& operator=(const WrongAnimal &operator_aff);
|
||||
~WrongAnimal();
|
||||
|
||||
std::string getType() const;
|
||||
void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
41
cpp04/ex01/WrongCat.cpp
Normal file
41
cpp04/ex01/WrongCat.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongCat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:58:15 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:59:48 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
WrongCat::WrongCat()
|
||||
{
|
||||
type = "WrongCat";
|
||||
std::cout << GREEN << "WrongCat default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongCat::WrongCat(const WrongCat ©) : WrongAnimal(copy)
|
||||
{
|
||||
std::cout << CYAN << "WrongCat copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongCat& WrongCat::operator=(const WrongCat &operator_aff)
|
||||
{
|
||||
WrongAnimal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "WrongCat assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
WrongCat::~WrongCat()
|
||||
{
|
||||
std::cout << RED << "WrongCat destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void WrongCat::makeSound() const
|
||||
{
|
||||
std::cout << "WrongCat makes a wrong sound!" << std::endl;
|
||||
}
|
||||
29
cpp04/ex01/WrongCat.hpp
Normal file
29
cpp04/ex01/WrongCat.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongCat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:22:35 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:32:31 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef WRONGCAT_HPP
|
||||
#define WRONGCAT_HPP
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
class WrongCat : public WrongAnimal
|
||||
{
|
||||
public:
|
||||
WrongCat();
|
||||
WrongCat(const WrongCat ©);
|
||||
WrongCat& operator=(const WrongCat &operator_aff);
|
||||
~WrongCat();
|
||||
|
||||
void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
74
cpp04/ex01/main.cpp
Normal file
74
cpp04/ex01/main.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 13:00:09 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 15:30:45 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "Dog.hpp"
|
||||
#include "Cat.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
const Animal* j = new Dog();
|
||||
const Animal* i = new Cat();
|
||||
|
||||
delete j;
|
||||
delete i;
|
||||
|
||||
Animal* animals[10];
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if (i < 5)
|
||||
{
|
||||
animals[i] = new Dog();
|
||||
}
|
||||
else
|
||||
{
|
||||
animals[i] = new Cat();
|
||||
}
|
||||
}
|
||||
|
||||
// Suppression des animaux
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
delete animals[i];
|
||||
}
|
||||
|
||||
// Test de copie profonde
|
||||
Dog originalDog;
|
||||
Dog copyDog(originalDog);
|
||||
|
||||
std::cout << "\n--- Test Deep Copy for Cat ---\n" << std::endl;
|
||||
|
||||
Cat originalCat;
|
||||
originalCat.getBrain()->setIdea("Jouer avec une balle", 0); // Définir une idée
|
||||
|
||||
// Copie profonde
|
||||
Cat copyCat(originalCat);
|
||||
|
||||
// Modifier l'original après la copie
|
||||
originalCat.getBrain()->setIdea("Dormir sur le canapé", 0);
|
||||
|
||||
// Afficher les idées pour vérifier la copie profonde
|
||||
std::cout << "Original Cat's Idea #0: " << originalCat.getBrain()->getIdea(0) << std::endl;
|
||||
std::cout << "Copy Cat's Idea #0: " << copyCat.getBrain()->getIdea(0) << std::endl;
|
||||
|
||||
if (originalCat.getBrain()->getIdea(0) != copyCat.getBrain()->getIdea(0))
|
||||
{
|
||||
std::cout << "Deep Copy Test Passed: Modifications to the original do not affect the copy." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Deep Copy Test Failed: Modifications to the original affect the copy." << std::endl;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
18
cpp04/ex02/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp04/ex02/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "linux-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
24
cpp04/ex02/.vscode/launch.json
vendored
Normal file
24
cpp04/ex02/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"externalConsole": false,
|
||||
"cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex02",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex02/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
59
cpp04/ex02/.vscode/settings.json
vendored
Normal file
59
cpp04/ex02/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "",
|
||||
"C_Cpp_Runner.useMsvc": false,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false
|
||||
}
|
||||
48
cpp04/ex02/AAnimal.cpp
Normal file
48
cpp04/ex02/AAnimal.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* AAnimal.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 19:25:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:18:10 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "AAnimal.hpp"
|
||||
|
||||
AAnimal::AAnimal() : type("Undefined")
|
||||
{
|
||||
std::cout << GREEN << "AAnimal constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
AAnimal::AAnimal(const AAnimal ©) : type(copy.type)
|
||||
{
|
||||
std::cout << GREEN << "AAnimal copy constructor called. Type is copied as " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
AAnimal& AAnimal::operator=(const AAnimal &operator_aff)// Protection contre l'auto-affectation
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->type = operator_aff.type;
|
||||
}
|
||||
std::cout << "AAnimal assignment operator called. Type is set to " << type << "." << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
AAnimal::~AAnimal()
|
||||
{
|
||||
std::cout << RED << "AAnimal destructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
std::string AAnimal::getType() const
|
||||
{
|
||||
return (this->type);
|
||||
}
|
||||
|
||||
void AAnimal::makeSound() const
|
||||
{
|
||||
std::cout << MAGENTA << "Some animal sound!" << RESET << std::endl;
|
||||
}
|
||||
44
cpp04/ex02/AAnimal.hpp
Normal file
44
cpp04/ex02/AAnimal.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* AAnimal.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 19:09:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:19:56 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef AANIMAL_HPP
|
||||
#define AANIMAL_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class AAnimal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
AAnimal();
|
||||
AAnimal(const AAnimal ©);
|
||||
virtual AAnimal& operator=(const AAnimal &operator_aff);
|
||||
virtual ~AAnimal();
|
||||
|
||||
std::string getType() const;
|
||||
virtual void makeSound() const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
59
cpp04/ex02/Brain.cpp
Normal file
59
cpp04/ex02/Brain.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Brain.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 14:06:58 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:30:23 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Brain.hpp"
|
||||
|
||||
Brain::Brain()
|
||||
{
|
||||
// Initialisation des idées
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
ideas[i] = "Idea " + std::to_string(i);
|
||||
//std::cout << GREEN << "Initialising Brain Idea #" << i << ": " << ideas[i] << RESET << std::endl;
|
||||
}
|
||||
std::cout << GREEN << "Brain constructed with 100 ideas." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Brain::Brain(const Brain ©)
|
||||
{
|
||||
*this = copy;
|
||||
std::cout << CYAN << "Brain copy constructor called." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Brain& Brain::operator=(const Brain &operator_aff)
|
||||
{
|
||||
std::cout << MAGENTA << "Brain assignment operator called." << RESET << std::endl;
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
this->ideas[i] = operator_aff.ideas[i];
|
||||
//std::cout << "Copying Idea #" << i << ": " << this->ideas[i] << std::endl;
|
||||
}
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Brain::~Brain()
|
||||
{
|
||||
std::cout << RED << "Brain destructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Brain::setIdea(std::string idea, int i)
|
||||
{
|
||||
this->ideas[i]=idea;
|
||||
}
|
||||
|
||||
std::string Brain::getIdea(int i) const
|
||||
{
|
||||
return (this->ideas[i]);
|
||||
}
|
||||
43
cpp04/ex02/Brain.hpp
Normal file
43
cpp04/ex02/Brain.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Brain.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 14:01:08 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:18:37 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BRAIN_HPP
|
||||
#define BRAIN_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class Brain
|
||||
{
|
||||
private:
|
||||
std::string ideas[100];
|
||||
|
||||
public:
|
||||
Brain();
|
||||
Brain(const Brain ©);
|
||||
Brain& operator=(const Brain &operator_aff);
|
||||
~Brain();
|
||||
void setIdea(std::string idea, int i);
|
||||
std::string getIdea(int i) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
47
cpp04/ex02/Cat.cpp
Normal file
47
cpp04/ex02/Cat.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:52:48 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:19:07 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Cat.hpp"
|
||||
|
||||
Cat::Cat()
|
||||
{
|
||||
this->type = "Cat";
|
||||
this->brain = new Brain();
|
||||
std::cout << GREEN << "Cat default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Cat::Cat(const Cat ©) : AAnimal(copy), brain(new Brain(*copy.brain))
|
||||
{
|
||||
std::cout << CYAN << "Cat copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Cat& Cat::operator=(const Cat &operator_aff)
|
||||
{
|
||||
AAnimal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "Cat assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Cat::~Cat()
|
||||
{
|
||||
std::cout << RED << "Cat destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Cat::makeSound() const
|
||||
{
|
||||
std::cout << "Miaouhh! Miaouhh!" << std::endl;
|
||||
}
|
||||
|
||||
Brain* Cat::getBrain(void) const
|
||||
{
|
||||
return (this->brain);
|
||||
}
|
||||
34
cpp04/ex02/Cat.hpp
Normal file
34
cpp04/ex02/Cat.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:17:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:20:30 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CAT_HPP
|
||||
#define CAT_HPP
|
||||
|
||||
#include "AAnimal.hpp"
|
||||
#include "Brain.hpp"
|
||||
|
||||
class Cat : public AAnimal
|
||||
{
|
||||
private:
|
||||
Brain* brain;
|
||||
|
||||
public:
|
||||
Cat();
|
||||
Cat(const Cat ©);
|
||||
virtual Cat& operator=(const Cat &operator_aff);
|
||||
virtual ~Cat();
|
||||
|
||||
void makeSound() const override;
|
||||
Brain* getBrain(void) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
47
cpp04/ex02/Dog.cpp
Normal file
47
cpp04/ex02/Dog.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Dog.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:50:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:19:31 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Dog.hpp"
|
||||
|
||||
Dog::Dog()
|
||||
{
|
||||
this->type = "Dog";
|
||||
this->brain = new Brain();
|
||||
std::cout << GREEN << "Dog default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Dog::Dog(const Dog ©) : AAnimal(copy), brain(new Brain(*copy.brain))
|
||||
{
|
||||
std::cout << CYAN << "Dog copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Dog& Dog::operator=(const Dog &operator_aff)
|
||||
{
|
||||
AAnimal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "Dog assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Dog::~Dog()
|
||||
{
|
||||
std::cout << RED << "Dog destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Dog::makeSound() const
|
||||
{
|
||||
std::cout << "Woof! Woof!" << std::endl;
|
||||
}
|
||||
|
||||
Brain* Dog::getBrain(void) const
|
||||
{
|
||||
return (this->brain);
|
||||
}
|
||||
34
cpp04/ex02/Dog.hpp
Normal file
34
cpp04/ex02/Dog.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Dog.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:06:42 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:20:45 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef DOG_HPP
|
||||
#define DOG_HPP
|
||||
|
||||
#include "AAnimal.hpp"
|
||||
#include "Brain.hpp"
|
||||
|
||||
class Dog : public AAnimal
|
||||
{
|
||||
private:
|
||||
Brain* brain;
|
||||
|
||||
public:
|
||||
Dog();
|
||||
Dog(const Dog ©);
|
||||
virtual Dog& operator=(const Dog &operator_aff);
|
||||
virtual ~Dog();
|
||||
|
||||
void makeSound() const override;
|
||||
Brain* getBrain(void) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
73
cpp04/ex02/Makefile
Normal file
73
cpp04/ex02/Makefile
Normal file
@@ -0,0 +1,73 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/31 16:25:51 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " ██▓ ▄▄▄ ▓█████▄▓█████ ▄▄▄▄ ▓█████▒███████▓█████";\
|
||||
echo " ▓██▒ ▒████▄ ▒██▀ ██▓█ ▀▓█████▄▓█ ▀▒ ▒ ▒ ▄▀▓█ ▀";\
|
||||
echo " ▒██░ ▒██ ▀█▄ ░██ █▒███ ▒██▒ ▄█▒███ ░ ▒ ▄▀▒░▒███";\
|
||||
echo " ▒██░ ░██▄▄▄▄██░▓█▄ ▒▓█ ▄▒██░█▀ ▒▓█ ▄ ▄▀▒ ▒▓█ ▄";\
|
||||
echo " ░██████▓█ ▓██░▒████▓░▒████░▓█ ▀█░▒████▒███████░▒████▒";\
|
||||
echo " ░ ▒░▓ ▒▒ ▓▒█░▒▒▓ ▒░░ ▒░ ░▒▓███▀░░ ▒░ ░▒▒ ▓░▒░░░ ▒░ ░";\
|
||||
echo " ░ ░ ▒ ░▒ ▒▒ ░░ ▒ ▒ ░ ░ ▒░▒ ░ ░ ░ ░░▒ ▒ ░ ▒░ ░ ░";\
|
||||
echo " ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░";\
|
||||
echo " ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ ░";\
|
||||
echo " ░ ░ ░"
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = abstract
|
||||
|
||||
SRC = AAnimal.cpp \
|
||||
Cat.cpp \
|
||||
Dog.cpp \
|
||||
WrongAnimal.cpp \
|
||||
WrongCat.cpp \
|
||||
Brain.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./abstract" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
48
cpp04/ex02/WrongAnimal.cpp
Normal file
48
cpp04/ex02/WrongAnimal.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongAnimal.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:55:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:57:57 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
WrongAnimal::WrongAnimal() : type("WrongAnimal")
|
||||
{
|
||||
std::cout << GREEN << "WrongAnimal default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongAnimal::WrongAnimal(const WrongAnimal ©) : type(copy.type)
|
||||
{
|
||||
std::cout << CYAN << "WrongAnimal copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongAnimal& WrongAnimal::operator=(const WrongAnimal &operator_aff)
|
||||
{
|
||||
if (this != &operator_aff)
|
||||
{
|
||||
this->type = operator_aff.type;
|
||||
}
|
||||
std::cout << MAGENTA << "WrongAnimal assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
WrongAnimal::~WrongAnimal()
|
||||
{
|
||||
std::cout << RED << "WrongAnimal destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
std::string WrongAnimal::getType() const
|
||||
{
|
||||
return (this->type);
|
||||
}
|
||||
|
||||
void WrongAnimal::makeSound() const
|
||||
{
|
||||
std::cout << "WrongAnimal makes a wrong sound!" << std::endl;
|
||||
}
|
||||
44
cpp04/ex02/WrongAnimal.hpp
Normal file
44
cpp04/ex02/WrongAnimal.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongAnimal.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:19:34 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:55:37 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef WRONGANIMAL_HPP
|
||||
#define WRONGANIMAL_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class WrongAnimal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
WrongAnimal();
|
||||
WrongAnimal(const WrongAnimal ©);
|
||||
WrongAnimal& operator=(const WrongAnimal &operator_aff);
|
||||
~WrongAnimal();
|
||||
|
||||
std::string getType() const;
|
||||
void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
41
cpp04/ex02/WrongCat.cpp
Normal file
41
cpp04/ex02/WrongCat.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongCat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:58:15 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 12:59:48 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
WrongCat::WrongCat()
|
||||
{
|
||||
type = "WrongCat";
|
||||
std::cout << GREEN << "WrongCat default constructor called. Type is set to " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongCat::WrongCat(const WrongCat ©) : WrongAnimal(copy)
|
||||
{
|
||||
std::cout << CYAN << "WrongCat copy constructor called. Type is " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
WrongCat& WrongCat::operator=(const WrongCat &operator_aff)
|
||||
{
|
||||
WrongAnimal::operator=(operator_aff);
|
||||
std::cout << MAGENTA << "WrongCat assignment operator called. Type is now " << type << "." << RESET << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
WrongCat::~WrongCat()
|
||||
{
|
||||
std::cout << RED << "WrongCat destructor called. Type was " << type << "." << RESET << std::endl;
|
||||
}
|
||||
|
||||
void WrongCat::makeSound() const
|
||||
{
|
||||
std::cout << "WrongCat makes a wrong sound!" << std::endl;
|
||||
}
|
||||
29
cpp04/ex02/WrongCat.hpp
Normal file
29
cpp04/ex02/WrongCat.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* WrongCat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 12:22:35 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:21:11 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef WRONGCAT_HPP
|
||||
#define WRONGCAT_HPP
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
class WrongCat : public WrongAnimal
|
||||
{
|
||||
public:
|
||||
WrongCat();
|
||||
WrongCat(const WrongCat ©);
|
||||
WrongCat& operator=(const WrongCat &operator_aff);
|
||||
~WrongCat();
|
||||
|
||||
void makeSound() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
91
cpp04/ex02/main.cpp
Normal file
91
cpp04/ex02/main.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 13:00:09 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:28:40 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "AAnimal.hpp"
|
||||
#include "Dog.hpp"
|
||||
#include "Cat.hpp"
|
||||
// Ajoutez d'autres inclusions si nécessaire
|
||||
|
||||
int main()
|
||||
{
|
||||
// Tentative d'instanciation d'un Animal (doit échouer)
|
||||
// AAnimal animal; // Décommentez pour vérifier que cela génère une erreur de compilation.
|
||||
|
||||
// Création d'un tableau d'animaux
|
||||
const int numAnimals = 4;
|
||||
AAnimal* animals[numAnimals];
|
||||
|
||||
// Remplir le tableau avec des Dogs et Cats
|
||||
for (int i = 0; i < numAnimals; ++i)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
animals[i] = new Dog(); // Utilise Dog pour les indices pairs
|
||||
}
|
||||
else
|
||||
{
|
||||
animals[i] = new Cat(); // Utilise Cat pour les indices impairs
|
||||
}
|
||||
}
|
||||
|
||||
// Faire du bruit avec chaque animal
|
||||
for (int i = 0; i < numAnimals; ++i)
|
||||
{
|
||||
std::cout << "Animal #" << i + 1 << " sound: ";
|
||||
animals[i]->makeSound();
|
||||
}
|
||||
|
||||
// Libérer la mémoire allouée pour les animaux
|
||||
for (int i = 0; i < numAnimals; ++i)
|
||||
{
|
||||
delete animals[i];
|
||||
}
|
||||
|
||||
// Tests supplémentaires pour la copie profonde
|
||||
std::cout << "\n--- Test Deep Copy ---\n";
|
||||
Dog originalDog;
|
||||
Dog copyDog(originalDog); // Utiliser le constructeur de copie
|
||||
originalDog.makeSound();
|
||||
copyDog.makeSound();
|
||||
std::cout << "\n--- Test Deep Copy for Cat ---\n" << std::endl;
|
||||
|
||||
Cat originalCat;
|
||||
originalCat.getBrain()->setIdea("Jouer avec une balle", 0); // Définir une idée
|
||||
|
||||
// Copie profonde
|
||||
Cat copyCat(originalCat);
|
||||
|
||||
// Modifier l'original après la copie
|
||||
originalCat.getBrain()->setIdea("Dormir sur le canapé", 0);
|
||||
|
||||
// Afficher les idées pour vérifier la copie profonde
|
||||
std::cout << "Original Cat's Idea #0: " << originalCat.getBrain()->getIdea(0) << std::endl;
|
||||
std::cout << "Copy Cat's Idea #0: " << copyCat.getBrain()->getIdea(0) << std::endl;
|
||||
|
||||
if (originalCat.getBrain()->getIdea(0) != copyCat.getBrain()->getIdea(0))
|
||||
{
|
||||
std::cout << "Deep Copy Test Passed: Modifications to the original do not affect the copy." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Deep Copy Test Failed: Modifications to the original affect the copy." << std::endl;
|
||||
}
|
||||
|
||||
// Test de l'assignation
|
||||
Dog anotherDog;
|
||||
anotherDog = originalDog; // Utiliser l'opérateur d'assignation
|
||||
anotherDog.makeSound();
|
||||
|
||||
|
||||
|
||||
return (0);
|
||||
}
|
||||
18
cpp04/ex03/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp04/ex03/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "linux-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
24
cpp04/ex03/.vscode/launch.json
vendored
Normal file
24
cpp04/ex03/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"externalConsole": false,
|
||||
"cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex03",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex03/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
59
cpp04/ex03/.vscode/settings.json
vendored
Normal file
59
cpp04/ex03/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "",
|
||||
"C_Cpp_Runner.useMsvc": false,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false
|
||||
}
|
||||
27
cpp04/ex03/AMateria.cpp
Normal file
27
cpp04/ex03/AMateria.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* AMateria.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 16:47:27 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:48:56 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "AMateria.hpp"
|
||||
|
||||
AMateria::AMateria(std::string const & type) : type(type) {}
|
||||
|
||||
AMateria::~AMateria() {}
|
||||
|
||||
std::string const & AMateria::getType() const
|
||||
{
|
||||
return (this->type);
|
||||
}
|
||||
|
||||
void AMateria::use(ICharacter& target)
|
||||
{
|
||||
std::cout << "* uses some unknown materia on " << target.getName() << " *" << std::endl;
|
||||
}
|
||||
34
cpp04/ex03/AMateria.hpp
Normal file
34
cpp04/ex03/AMateria.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* AMateria.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 16:44:47 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:09:18 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef AMATERIA_HPP
|
||||
#define AMATERIA_HPP
|
||||
|
||||
#include "ICharacter.hpp"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class AMateria
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
AMateria(std::string const & type);
|
||||
virtual ~AMateria();
|
||||
|
||||
std::string const & getType() const; // Renvoie le type de la materia
|
||||
virtual AMateria* clone() const = 0; // Doit être implémenté dans les sous-classes
|
||||
virtual void use(ICharacter& target); // Peut être redéfini dans les sous-classes
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
cpp04/ex03/AMateria.o
Normal file
BIN
cpp04/ex03/AMateria.o
Normal file
Binary file not shown.
82
cpp04/ex03/Character.cpp
Normal file
82
cpp04/ex03/Character.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Character.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 16:58:59 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:28:36 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Character.hpp"
|
||||
#include "AMateria.hpp"
|
||||
|
||||
Character::Character(std::string name) : name(name)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
inventory[i] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Character::Character(const Character& other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
Character& Character::operator=(const Character& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
name = other.name;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
delete inventory[i]; // Assurez-vous de libérer la mémoire de l'inventaire actuel
|
||||
inventory[i] = other.inventory[i] ? other.inventory[i]->clone() : nullptr;
|
||||
}
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Character::~Character()
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
delete inventory[i];
|
||||
}
|
||||
}
|
||||
|
||||
std::string const & Character::getName() const
|
||||
{
|
||||
return (name);
|
||||
}
|
||||
|
||||
void Character::equip(AMateria* m)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
if (!inventory[i])
|
||||
{
|
||||
inventory[i] = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Character::unequip(int idx)
|
||||
{
|
||||
if (idx >= 0 && idx < 4)
|
||||
{
|
||||
inventory[idx] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Character::use(int idx, ICharacter& target)
|
||||
{
|
||||
if (idx >= 0 && idx < 4 && inventory[idx])
|
||||
{
|
||||
inventory[idx]->use(target);
|
||||
}
|
||||
}
|
||||
36
cpp04/ex03/Character.hpp
Normal file
36
cpp04/ex03/Character.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Character.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 16:58:01 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:58:40 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CHARACTER_HPP
|
||||
#define CHARACTER_HPP
|
||||
|
||||
#include "ICharacter.hpp"
|
||||
|
||||
class Character : public ICharacter
|
||||
{
|
||||
private:
|
||||
std::string name;
|
||||
AMateria* inventory[4]; // Inventaire avec 4 emplacements pour les Materias
|
||||
|
||||
public:
|
||||
Character(std::string name);
|
||||
Character(const Character& other);
|
||||
Character& operator=(const Character& other);
|
||||
virtual ~Character();
|
||||
|
||||
virtual std::string const & getName() const override;
|
||||
virtual void equip(AMateria* m) override;
|
||||
virtual void unequip(int idx) override;
|
||||
virtual void use(int idx, ICharacter& target) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
cpp04/ex03/Character.o
Normal file
BIN
cpp04/ex03/Character.o
Normal file
Binary file not shown.
35
cpp04/ex03/Cure.cpp
Normal file
35
cpp04/ex03/Cure.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cure.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 17:03:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:04:37 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Cure.hpp"
|
||||
|
||||
Cure::Cure() : AMateria("cure") {}
|
||||
|
||||
Cure::Cure(const Cure& other) : AMateria(other) {}
|
||||
|
||||
Cure& Cure::operator=(const Cure& other)
|
||||
{
|
||||
AMateria::operator=(other);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Cure::~Cure() {}
|
||||
|
||||
AMateria* Cure::clone() const
|
||||
{
|
||||
return new Cure(*this);
|
||||
}
|
||||
|
||||
void Cure::use(ICharacter& target)
|
||||
{
|
||||
std::cout << "* heals " << target.getName() << "’s wounds *" << std::endl;
|
||||
}
|
||||
30
cpp04/ex03/Cure.hpp
Normal file
30
cpp04/ex03/Cure.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Cure.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 17:03:19 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:03:43 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CURE_HPP
|
||||
#define CURE_HPP
|
||||
|
||||
#include "AMateria.hpp"
|
||||
|
||||
class Cure : public AMateria
|
||||
{
|
||||
public:
|
||||
Cure();
|
||||
Cure(const Cure& other);
|
||||
Cure& operator=(const Cure& other);
|
||||
virtual ~Cure();
|
||||
|
||||
virtual AMateria* clone() const override;
|
||||
virtual void use(ICharacter& target) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
cpp04/ex03/Cure.o
Normal file
BIN
cpp04/ex03/Cure.o
Normal file
Binary file not shown.
31
cpp04/ex03/ICharacter.hpp
Normal file
31
cpp04/ex03/ICharacter.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ICharacter.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 16:52:43 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:25:11 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ICHARACTER_HPP
|
||||
#define ICHARACTER_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
class AMateria;
|
||||
|
||||
class ICharacter
|
||||
{
|
||||
public:
|
||||
virtual ~ICharacter() {}
|
||||
|
||||
virtual std::string const & getName() const = 0; // Doit retourner le nom du personnage
|
||||
virtual void equip(AMateria* m) = 0; // Équipe une AMateria
|
||||
virtual void unequip(int idx) = 0; // Déséquipe une AMateria sans la détruire
|
||||
virtual void use(int idx, ICharacter& target) = 0; // Utilise une AMateria sur un ICharacter
|
||||
};
|
||||
|
||||
#endif
|
||||
26
cpp04/ex03/IMateriaSource.hpp
Normal file
26
cpp04/ex03/IMateriaSource.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* IMateriaSource.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 17:06:19 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:07:06 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef IMATERIASOURCE_HPP
|
||||
#define IMATERIASOURCE_HPP
|
||||
|
||||
#include "AMateria.hpp"
|
||||
|
||||
class IMateriaSource
|
||||
{
|
||||
public:
|
||||
virtual ~IMateriaSource() {}
|
||||
virtual void learnMateria(AMateria*) = 0;
|
||||
virtual AMateria* createMateria(std::string const & type) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
35
cpp04/ex03/Ice.cpp
Normal file
35
cpp04/ex03/Ice.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Ice.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 16:49:57 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:50:52 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Ice.hpp"
|
||||
|
||||
Ice::Ice() : AMateria("ice") {}
|
||||
|
||||
Ice::Ice(const Ice& other) : AMateria(other) {}
|
||||
|
||||
Ice& Ice::operator=(const Ice& other)
|
||||
{
|
||||
AMateria::operator=(other);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Ice::~Ice() {}
|
||||
|
||||
AMateria* Ice::clone() const
|
||||
{
|
||||
return new Ice(*this);
|
||||
}
|
||||
|
||||
void Ice::use(ICharacter& target)
|
||||
{
|
||||
std::cout << "* shoots an ice bolt at " << target.getName() << " *" << std::endl;
|
||||
}
|
||||
30
cpp04/ex03/Ice.hpp
Normal file
30
cpp04/ex03/Ice.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Ice.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 16:49:13 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 16:49:44 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ICE_HPP
|
||||
#define ICE_HPP
|
||||
|
||||
#include "AMateria.hpp"
|
||||
|
||||
class Ice : public AMateria
|
||||
{
|
||||
public:
|
||||
Ice();
|
||||
Ice(const Ice& other);
|
||||
Ice& operator=(const Ice& other);
|
||||
virtual ~Ice();
|
||||
|
||||
virtual AMateria* clone() const override;
|
||||
virtual void use(ICharacter& target) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
cpp04/ex03/Ice.o
Normal file
BIN
cpp04/ex03/Ice.o
Normal file
Binary file not shown.
72
cpp04/ex03/Makefile
Normal file
72
cpp04/ex03/Makefile
Normal file
@@ -0,0 +1,72 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/31 17:14:41 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " ██▓ ▄▄▄ ▓█████▄▓█████ ▄▄▄▄ ▓█████▒███████▓█████";\
|
||||
echo " ▓██▒ ▒████▄ ▒██▀ ██▓█ ▀▓█████▄▓█ ▀▒ ▒ ▒ ▄▀▓█ ▀";\
|
||||
echo " ▒██░ ▒██ ▀█▄ ░██ █▒███ ▒██▒ ▄█▒███ ░ ▒ ▄▀▒░▒███";\
|
||||
echo " ▒██░ ░██▄▄▄▄██░▓█▄ ▒▓█ ▄▒██░█▀ ▒▓█ ▄ ▄▀▒ ▒▓█ ▄";\
|
||||
echo " ░██████▓█ ▓██░▒████▓░▒████░▓█ ▀█░▒████▒███████░▒████▒";\
|
||||
echo " ░ ▒░▓ ▒▒ ▓▒█░▒▒▓ ▒░░ ▒░ ░▒▓███▀░░ ▒░ ░▒▒ ▓░▒░░░ ▒░ ░";\
|
||||
echo " ░ ░ ▒ ░▒ ▒▒ ░░ ▒ ▒ ░ ░ ▒░▒ ░ ░ ░ ░░▒ ▒ ░ ▒░ ░ ░";\
|
||||
echo " ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░";\
|
||||
echo " ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ ░";\
|
||||
echo " ░ ░ ░"
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = materia
|
||||
|
||||
SRC = AMateria.cpp \
|
||||
Character.cpp \
|
||||
Cure.cpp \
|
||||
Ice.cpp \
|
||||
MateriaSource.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./materia" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
68
cpp04/ex03/MateriaSource.cpp
Normal file
68
cpp04/ex03/MateriaSource.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* MateriaSource.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 17:08:37 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:11:59 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "MateriaSource.hpp"
|
||||
|
||||
MateriaSource::MateriaSource()
|
||||
{
|
||||
materias.fill(nullptr);
|
||||
}
|
||||
|
||||
MateriaSource::MateriaSource(const MateriaSource& other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
MateriaSource& MateriaSource::operator=(const MateriaSource& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
delete materias[i];
|
||||
materias[i] = other.materias[i] ? other.materias[i]->clone() : nullptr;
|
||||
}
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
MateriaSource::~MateriaSource()
|
||||
{
|
||||
for (auto& materia : materias)
|
||||
{
|
||||
delete materia;
|
||||
}
|
||||
}
|
||||
|
||||
void MateriaSource::learnMateria(AMateria* m)
|
||||
{
|
||||
for (auto& materia : materias)
|
||||
{
|
||||
if (!materia)
|
||||
{
|
||||
materia = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AMateria* MateriaSource::createMateria(std::string const & type)
|
||||
{
|
||||
for (auto& materia : materias)
|
||||
{
|
||||
if (materia && materia->getType() == type)
|
||||
{
|
||||
return materia->clone();
|
||||
}
|
||||
}
|
||||
return (nullptr);
|
||||
}
|
||||
34
cpp04/ex03/MateriaSource.hpp
Normal file
34
cpp04/ex03/MateriaSource.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* MateriaSource.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 17:07:33 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:08:05 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MATERIASOURCE_HPP
|
||||
#define MATERIASOURCE_HPP
|
||||
|
||||
#include "IMateriaSource.hpp"
|
||||
#include <array>
|
||||
|
||||
class MateriaSource : public IMateriaSource
|
||||
{
|
||||
private:
|
||||
std::array<AMateria*, 4> materias; // Peut contenir jusqu'à 4 types de Materias
|
||||
|
||||
public:
|
||||
MateriaSource();
|
||||
MateriaSource(const MateriaSource& other);
|
||||
MateriaSource& operator=(const MateriaSource& other);
|
||||
virtual ~MateriaSource();
|
||||
|
||||
virtual void learnMateria(AMateria* m) override;
|
||||
virtual AMateria* createMateria(std::string const & type) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
cpp04/ex03/MateriaSource.o
Normal file
BIN
cpp04/ex03/MateriaSource.o
Normal file
Binary file not shown.
70
cpp04/ex03/main.cpp
Normal file
70
cpp04/ex03/main.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/31 17:14:54 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/31 17:36:22 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "MateriaSource.hpp"
|
||||
#include "Character.hpp"
|
||||
#include "Ice.hpp"
|
||||
#include "Cure.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
IMateriaSource* src = new MateriaSource();
|
||||
src->learnMateria(new Ice());
|
||||
src->learnMateria(new Cure());
|
||||
ICharacter* me = new Character("me");
|
||||
ICharacter* bob = new Character("bob");
|
||||
|
||||
std::string input;
|
||||
while (true)
|
||||
{
|
||||
std::cout << "Enter command (equip, use, or quit): ";
|
||||
std::cin >> input;
|
||||
|
||||
if (input == "equip")
|
||||
{
|
||||
std::string materiaType;
|
||||
std::cout << "Enter materia type (ice or cure): ";
|
||||
std::cin >> materiaType;
|
||||
AMateria* tmp = src->createMateria(materiaType);
|
||||
if (tmp)
|
||||
{
|
||||
me->equip(tmp);
|
||||
std::cout << materiaType << " equipped." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Unknown materia type." << std::endl;
|
||||
}
|
||||
}
|
||||
else if (input == "use")
|
||||
{
|
||||
int idx;
|
||||
std::cout << "Enter inventory index (0-3): ";
|
||||
std::cin >> idx;
|
||||
me->use(idx, *bob);
|
||||
}
|
||||
else if (input == "quit")
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Unknown command." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
delete bob;
|
||||
delete me;
|
||||
delete src;
|
||||
|
||||
return (0);
|
||||
}
|
||||
BIN
cpp04/ex03/main.o
Normal file
BIN
cpp04/ex03/main.o
Normal file
Binary file not shown.
BIN
cpp04/ex03/materia
Executable file
BIN
cpp04/ex03/materia
Executable file
Binary file not shown.
Reference in New Issue
Block a user