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);
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef ANIMAL_HPP
# define ANIMAL_HPP
# include <iostream>
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
+36
View File
@@ -0,0 +1,36 @@
# include "Cat.hpp"
Cat::Cat () : Animal()
{
_type = "Cat";
std::cout << "Cat constructor called\n";
}
Cat::Cat (std::string type) : Animal(type)
{
_type = type;
std::cout << "Cat constructor called\n";
}
Cat::Cat(const Cat &other) : Animal()
{
*this = other;
std::cout << "Cat copy constructor called\n";
}
Cat &Cat::operator=(const Cat &other)
{
_type = other._type;
std::cout << "Cat copy assignment constructor called\n";
return (*this);
}
Cat::~Cat()
{
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";
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef CAT_HPP
# define CAT_HPP
# include <iostream>
# include "Animal.hpp"
class Cat : public Animal
{
public :
Cat();
Cat(std::string type);
Cat(const Cat &other);
virtual ~Cat();
Cat & operator=(const Cat &other);
virtual void makeSound() const;
};
#endif
+36
View File
@@ -0,0 +1,36 @@
# include "Dog.hpp"
Dog::Dog() : Animal()
{
_type = "Dog";
std::cout << "Dog default constructor called\n";
}
Dog::Dog (std::string type) : Animal(type)
{
_type = type;
std::cout << "Dog constructor called\n";
}
Dog::Dog(const Dog &other) : Animal(other)
{
*this = other;
std::cout << "Dog copy constructor called\n";
}
Dog &Dog::operator=(const Dog &other)
{
_type = other._type;
std::cout << "Dog copy assignment constructor called\n";
return (*this);
}
Dog::~Dog()
{
std::cout << "Dog deconstructor called\n";
}
void Dog::makeSound() const
{
std::cout << "Waf Waf Waf\n";
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef DOG_HPP
# define DOG_HPP
# include <iostream>
# include "Animal.hpp"
class Dog : public Animal
{
public :
Dog();
Dog(std::string type);
Dog(const Dog &other);
virtual ~Dog();
Dog & operator=(const Dog &other);
virtual void makeSound() const;
};
#endif
+45
View File
@@ -0,0 +1,45 @@
# include "Animal.hpp"
# include "WrongAnimal.hpp"
# include "Dog.hpp"
# include "Cat.hpp"
# include "WrongCat.hpp"
int main()
{
std::cout << "========== TEST 1: Basic Animal ==========" << std::endl;
{
std::cout << "\n========== Animal ==========" << std::endl;
const Animal* meta = new Animal();
std::cout << meta->getType() << " " << std::endl;
meta->makeSound();
delete meta;
std::cout << "\n========== Dog ==========" << std::endl;
const Animal* j = new Dog();
std::cout << j->getType() << " " << std::endl;
j->makeSound();
delete j;
std::cout << "\n========== Cat ==========" << std::endl;
const Animal* i = new Cat();
std::cout << i->getType() << " " << std::endl;
j->makeSound();
delete i;
}
std::cout << "\n========== TEST 2: Wrong Animal (No Virtual) ==========" << std::endl;
{
std::cout << "\n========== WrongAnimal ==========" << std::endl;
const WrongAnimal* k = new WrongAnimal();
std::cout << "WrongAnimal type: " << k->getType() << std::endl;
k->makeSound(); // WrongAnimal sound
delete k;
std::cout << "\n========== WrongCat ==========" << std::endl;
const WrongAnimal* l = new WrongCat();
std::cout << "WrongCat type: " << l->getType() << std::endl;
l->makeSound(); // WrongAnimal sound aussi (pas de polymorphisme!)
delete l;
}
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
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";
}
+18
View File
@@ -0,0 +1,18 @@
#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