This commit is contained in:
root
2025-11-15 15:37:47 +00:00
commit 098a0cea33
41 changed files with 1475 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
# include "Animal.hpp"
Animal::Animal (std::string type) : _type(type)
{
std::cout << "Animal constructor called\n";
}
Animal::Animal() : _type("Default")
{
std::cout << "Animal def constructor called\n";
}
Animal::Animal(const Animal &other) : _type(other._type)
{
std::cout << "Animal copy constructor called\n";
}
Animal &Animal::operator=(const Animal &other)
{
_type = other._type;
std::cout << "Animal copy assignment constructor called\n";
return (*this);
}
Animal::~Animal()
{
std::cout << "Animal deconstructor called\n";
}
void Animal::makeSound() const
{
std::cout << "Animal can make different sounds.\n";
}
std::string Animal::getType() const
{
return (_type);
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef ANIMAL_HPP
# define ANIMAL_HPP
# include <iostream>
# include "Brain.hpp"
class Animal
{
protected :
std::string _type;
public :
Animal();
Animal(std::string type);
Animal(const Animal &other);
virtual ~Animal();
Animal & operator=(const Animal &other);
virtual void makeSound() const;
std::string getType() const;
};
#endif
+37
View File
@@ -0,0 +1,37 @@
#include "Brain.hpp"
Brain::Brain()
{
int i = 0;
while (i < 100)
{
std::ostringstream oss;
oss << "idea" << i;
ideas[i] = oss.str();
i++;
}
std::cout << "Brain def constructor called\n";
}
Brain::Brain(const Brain &other)
{
*this = other;
std::cout << "Brain copy constructor called\n";
}
Brain &Brain::operator=(const Brain &other)
{
int i = 0;
while (i < 100)
{
ideas[i] = other.ideas[i];
i++;
}
std::cout << "Brain copy assignment constructor called\n";
return (*this);
}
Brain::~Brain()
{
std::cout << "Brain deconstructor called\n";
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef BRAIN_HPP
# define BRAIN_HPP
# include <iostream>
# include <sstream>
class Brain
{
public:
Brain();
Brain(const Brain &other);
Brain &operator=(const Brain &other);
~Brain();
std::string ideas[100];
};
#endif
+62
View File
@@ -0,0 +1,62 @@
# include "Cat.hpp"
Cat::Cat () : Animal()
{
_type = "Cat";
_brain = new Brain();
std::cout << "Cat constructor called\n";
}
Cat::Cat (std::string type) : Animal(type)
{
_type = type;
_brain = new Brain();
std::cout << "Cat constructor called\n";
}
Cat::Cat(const Cat &other) : Animal()
{
_brain = new Brain(*(other._brain));
std::cout << "Cat copy constructor called\n";
}
Cat &Cat::operator=(const Cat &other)
{
_type = other._type;
delete _brain;
_brain = new Brain(*(other._brain));
std::cout << "Cat copy assignment constructor called\n";
return (*this);
}
Cat::~Cat()
{
delete _brain;
std::cout << "Cat deconstructor called\n";
}
void Cat::makeSound() const
{
std::cout << "miaou miaou miaou (je suis relou) miaou miaou miaou miaou miaou miaou miaou miaou miaou miaou miaou miaou miaou miaou miaou\n";
}
std::string Cat::getIdea(int n_idea) const
{
if (n_idea < 0)
return "Negative ideas number is not posible.\n";
else if (n_idea > 99)
return "A cat's brain can have up to 100 ideas.\n";
else
return _brain->ideas[n_idea];
}
void Cat::setIdea(int n_idea, std::string idea)
{
if (n_idea < 0)
std::cout << "Negative idea number is not posible.\n";
else if (n_idea > 99)
std::cout << "A cat's brain can have up to 100 ideas.\n";
else
_brain->ideas[n_idea] = idea;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef CAT_HPP
# define CAT_HPP
# include <iostream>
# include "Animal.hpp"
# include "Brain.hpp"
class Cat : public Animal
{
private:
Brain *_brain;
public :
Cat();
Cat(std::string type);
Cat(const Cat &other);
virtual ~Cat();
Cat & operator=(const Cat &other);
virtual void makeSound() const;
std::string getIdea(int n_idea) const;
void setIdea(int n_idea, std::string idea);
};
#endif
+62
View File
@@ -0,0 +1,62 @@
# include "Dog.hpp"
Dog::Dog() : Animal()
{
_type = "Dog";
_brain = new Brain();
std::cout << "Dog default constructor called\n";
}
Dog::Dog (std::string type) : Animal(type)
{
_type = type;
_brain = new Brain();
std::cout << "Dog constructor called\n";
}
Dog::Dog(const Dog &other) : Animal(other)
{
_brain = new Brain(*(other._brain));
std::cout << "Dog copy constructor called\n";
}
Dog &Dog::operator=(const Dog &other)
{
_type = other._type;
delete _brain;
_brain = new Brain(*(other._brain));
std::cout << "Dog copy assignment constructor called\n";
return (*this);
}
Dog::~Dog()
{
delete _brain;
std::cout << "Dog deconstructor called\n";
}
void Dog::makeSound() const
{
std::cout << "Waf Waf Waf\n";
}
std::string Dog::getIdea(int n_idea) const
{
if (n_idea < 0)
return "Negative ideas number is not posible.\n";
else if (n_idea > 99)
return "A dog's brain can have up to 100 ideas.\n";
else
return _brain->ideas[n_idea];
}
void Dog::setIdea(int n_idea, std::string idea)
{
if (n_idea < 0)
std::cout << "Negative idea number is not posible.\n";
else if (n_idea > 99)
std::cout << "A dog's brain can have up to 100 ideas.\n";
else
_brain->ideas[n_idea] = idea;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef DOG_HPP
# define DOG_HPP
# include <iostream>
# include "Animal.hpp"
# include "Brain.hpp"
class Dog : public Animal
{
private:
Brain *_brain;
public :
Dog();
Dog(std::string type);
Dog(const Dog &other);
virtual ~Dog();
Dog & operator=(const Dog &other);
virtual void makeSound() const;
std::string getIdea(int n_idea) const;
void setIdea(int n_idea, std::string idea);
};
#endif
+81
View File
@@ -0,0 +1,81 @@
# include "Animal.hpp"
# include "WrongAnimal.hpp"
# include "Dog.hpp"
# include "Cat.hpp"
# include "WrongCat.hpp"
int main()
{
std::cout << "========== TEST 1: Array of 10 Animals ==========" << std::endl;
{
const int size = 10;
Animal* animals[size];
int i = 0;
while (i < size)
{
if (i % 2 == 0)
animals[i] = new Dog();
else
animals[i] = new Cat();
i++;
}
std::cout << "\n--- Making sounds ---" << std::endl;
i = 0;
while (i < size)
{
std::cout << "Animal " << i << " (" << animals[i]->getType() << "): ";
animals[i]->makeSound();
i++;
}
std::cout << "\n--- Deleting animals ---" << std::endl;
i = 0;
while (i < size)
{
delete animals[i];
i++;
}
}
std::cout << "\n========== TEST 2: Get Ideas from Brain ==========" << std::endl;
{
Dog dog;
Cat cat;
std::cout << "\n--- Dog ideas ---" << std::endl;
std::cout << "Idea 0: " << dog.getIdea(0) << std::endl;
std::cout << "Idea 42: " << dog.getIdea(42) << std::endl;
std::cout << "Idea 99: " << dog.getIdea(99) << std::endl;
std::cout << "\n--- Cat ideas ---" << std::endl;
std::cout << "Idea 5: " << cat.getIdea(5) << std::endl;
std::cout << "Idea 50: " << cat.getIdea(50) << std::endl;
std::cout << "Idea 98: " << cat.getIdea(98) << std::endl;
std::cout << "\n========== Index Out of Range ==========\n" << std::endl;
std::cout << "Trying to access idea 100: " << dog.getIdea(100) << std::endl;
std::cout << "Trying to access idea 150: " << dog.getIdea(150) << std::endl;
std::cout << "Trying to access idea -1: " << dog.getIdea(-1) << std::endl;
}
std::cout << "\n========== TEST 4: Deep Copy Test ==========" << std::endl;
{
std::cout << "--- Creating original dog ---" << std::endl;
Dog* original = new Dog();
original->setIdea(10, "idee test");
std::cout << "\nOriginal dog idea 10: " << original->getIdea(10) << std::endl;
std::cout << "\n--- Creating copy ---" << std::endl;
Dog* copy = new Dog(*original);
std::cout << "\nCopy dog idea 10: " << copy->getIdea(10) << std::endl;
std::cout << "\n--- Modifying original's idea ---" << std::endl;
original->setIdea(10, "Modified idea in original");
std::cout << "\nOriginal dog idea 10 after modification: " << original->getIdea(10) << std::endl;
std::cout << "Copy dog idea 10 (should be unchanged): " << copy->getIdea(10) << std::endl;
std::cout << "\n--- Deleting original ---" << std::endl;
delete original;
std::cout << "\n--- Copy still works after original deleted ---" << std::endl;
std::cout << "Copy dog idea 10: " << copy->getIdea(10) << std::endl;
copy->makeSound();
std::cout << "\n--- Deleting copy ---" << std::endl;
delete copy;
}
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
CXX = c++
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
OBJDIR = obj
SOURCES = Main.cpp Dog.cpp Cat.cpp Animal.cpp WrongAnimal.cpp WrongCat.cpp Brain.cpp
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
NAME = Animal
all: $(NAME)
$(OBJDIR):
@echo "📁 Creating obj directory..."
@mkdir -p $(OBJDIR)
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
@echo "🧠 Compiling $< ..."
@$(CXX) $(CXXFLAGS) -c $< -o $@
@echo "$@ ready!"
$(NAME): $(OBJECTS)
@echo "🔗 Linking $(NAME) ..."
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
@echo "🎉 $(NAME) is ready!"
clean:
@echo "🧹 Cleaning object files..."
@rm -rf $(OBJDIR)
@echo "✨ Objects cleaned!"
fclean: clean
@echo "🗑️ Removing $(NAME)..."
@rm -f $(NAME)
@echo "💀 Full clean complete!"
re: fclean all
.PHONY: all clean fclean re
+38
View File
@@ -0,0 +1,38 @@
# include "WrongAnimal.hpp"
WrongAnimal::WrongAnimal (std::string type) : _type(type)
{
std::cout << "WrongAnimal constructor called\n";
}
WrongAnimal::WrongAnimal() : _type("Default")
{
std::cout << "WrongAnimal def constructor called\n";
}
WrongAnimal::WrongAnimal(const WrongAnimal &other) : _type(other._type)
{
std::cout << "WrongAnimal copy constructor called\n";
}
WrongAnimal &WrongAnimal::operator=(const WrongAnimal &other)
{
_type = other._type;
std::cout << "WrongAnimal copy assignment constructor called\n";
return (*this);
}
WrongAnimal::~WrongAnimal()
{
std::cout << "WrongAnimal deconstructor called\n";
}
void WrongAnimal::makeSound() const
{
std::cout << "WrongAnimal can make different sounds.\n";
}
std::string WrongAnimal::getType() const
{
return (_type);
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef WRONGANIMAL_HPP
# define WRONGANIMAL_HPP
# include <iostream>
class WrongAnimal
{
protected :
std::string _type;
public :
WrongAnimal();
WrongAnimal(std::string type);
WrongAnimal(const WrongAnimal &other);
virtual ~WrongAnimal();
WrongAnimal & operator=(const WrongAnimal &other);
void makeSound() const;
std::string getType() const;
};
#endif
+36
View File
@@ -0,0 +1,36 @@
# include "WrongCat.hpp"
WrongCat::WrongCat () : WrongAnimal()
{
_type = "WrongCat";
std::cout << "WrongCat constructor called\n";
}
WrongCat::WrongCat (std::string type) : WrongAnimal(type)
{
_type = type;
std::cout << "WrongCat constructor called\n";
}
WrongCat::WrongCat(const WrongCat &other) : WrongAnimal()
{
*this = other;
std::cout << "WrongCat copy constructor called\n";
}
WrongCat &WrongCat::operator=(const WrongCat &other)
{
_type = other._type;
std::cout << "WrongCat copy assignment constructor called\n";
return (*this);
}
WrongCat::~WrongCat()
{
std::cout << "WrongCat deconstructor called\n";
}
void WrongCat::makeSound() const
{
std::cout << "wrongmiaou wrongmiaou wrongmiaou\n";
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef WrongCat_HPP
# define WrongCat_HPP
# include <iostream>
# include "WrongAnimal.hpp"
class WrongCat : public WrongAnimal
{
public :
WrongCat();
WrongCat(std::string type);
WrongCat(const WrongCat &other);
virtual ~WrongCat();
WrongCat & operator=(const WrongCat &other);
virtual void makeSound() const;
};
#endif