This commit is contained in:
2024-02-20 15:35:19 +01:00
parent a5816720e4
commit 93c619a3c7
267 changed files with 10113 additions and 0 deletions

View 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
View 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
View 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
View 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 &copy) : 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
View 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 &copy);
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
View 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 &copy)
{
*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
View 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 &copy);
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
View 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 &copy) : 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
View 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 &copy);
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
View 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 &copy) : 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
View 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 &copy);
virtual Dog& operator=(const Dog &operator_aff);
virtual ~Dog();
void makeSound() const override;
Brain* getBrain(void) const;
};
#endif

73
cpp04/ex02/Makefile Normal file
View 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

View 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 &copy) : 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;
}

View 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 &copy);
WrongAnimal& operator=(const WrongAnimal &operator_aff);
~WrongAnimal();
std::string getType() const;
void makeSound() const;
};
#endif

41
cpp04/ex02/WrongCat.cpp Normal file
View 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 &copy) : 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
View 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 &copy);
WrongCat& operator=(const WrongCat &operator_aff);
~WrongCat();
void makeSound() const;
};
#endif

91
cpp04/ex02/main.cpp Normal file
View 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);
}