push
This commit is contained in:
commit
098a0cea33
|
|
@ -0,0 +1,51 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"iostream": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "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",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"numbers": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"ranges": "cpp",
|
||||||
|
"span": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"typeinfo": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
# include "AAnimal.hpp"
|
||||||
|
|
||||||
|
AAnimal::AAnimal (std::string type) : _type(type)
|
||||||
|
{
|
||||||
|
std::cout << "AAnimal constructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
AAnimal::AAnimal() : _type("Default")
|
||||||
|
{
|
||||||
|
std::cout << "AAnimal def constructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
AAnimal::AAnimal(const AAnimal &other) : _type(other._type)
|
||||||
|
{
|
||||||
|
std::cout << "AAnimal copy constructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
AAnimal &AAnimal::operator=(const AAnimal &other)
|
||||||
|
{
|
||||||
|
_type = other._type;
|
||||||
|
std::cout << "AAnimal copy assignment constructor called\n";
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
AAnimal::~AAnimal()
|
||||||
|
{
|
||||||
|
std::cout << "AAnimal deconstructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void AAnimal::makeSound() const
|
||||||
|
{
|
||||||
|
std::cout << "AAnimal can make different sounds.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string AAnimal::getType() const
|
||||||
|
{
|
||||||
|
return (_type);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
#ifndef AANIMAL_HPP
|
||||||
|
# define AANIMAL_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include "Brain.hpp"
|
||||||
|
class AAnimal
|
||||||
|
{
|
||||||
|
protected :
|
||||||
|
std::string _type;
|
||||||
|
|
||||||
|
public :
|
||||||
|
AAnimal();
|
||||||
|
AAnimal(std::string type);
|
||||||
|
AAnimal(const AAnimal &other);
|
||||||
|
virtual ~AAnimal();
|
||||||
|
AAnimal & operator=(const AAnimal &other);
|
||||||
|
|
||||||
|
virtual void makeSound() const = 0;
|
||||||
|
std::string getType() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
# include "Cat.hpp"
|
||||||
|
|
||||||
|
Cat::Cat () : AAnimal()
|
||||||
|
{
|
||||||
|
_type = "Cat";
|
||||||
|
_brain = new Brain();
|
||||||
|
std::cout << "Cat constructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
Cat::Cat (std::string type) : AAnimal(type)
|
||||||
|
{
|
||||||
|
_type = type;
|
||||||
|
_brain = new Brain();
|
||||||
|
std::cout << "Cat constructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
Cat::Cat(const Cat &other) : AAnimal()
|
||||||
|
{
|
||||||
|
_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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef CAT_HPP
|
||||||
|
# define CAT_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include "AAnimal.hpp"
|
||||||
|
# include "Brain.hpp"
|
||||||
|
|
||||||
|
class Cat : public AAnimal
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
# include "Dog.hpp"
|
||||||
|
|
||||||
|
Dog::Dog() : AAnimal()
|
||||||
|
{
|
||||||
|
_type = "Dog";
|
||||||
|
_brain = new Brain();
|
||||||
|
std::cout << "Dog default constructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
Dog::Dog (std::string type) : AAnimal(type)
|
||||||
|
{
|
||||||
|
_type = type;
|
||||||
|
_brain = new Brain();
|
||||||
|
std::cout << "Dog constructor called\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
Dog::Dog(const Dog &other) : AAnimal(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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef DOG_HPP
|
||||||
|
# define DOG_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include "AAnimal.hpp"
|
||||||
|
# include "Brain.hpp"
|
||||||
|
|
||||||
|
class Dog : public AAnimal
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
# include "AAnimal.hpp"
|
||||||
|
# include "WrongAnimal.hpp"
|
||||||
|
# include "Dog.hpp"
|
||||||
|
# include "Cat.hpp"
|
||||||
|
# include "WrongCat.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << "========== TEST 1: Basic AAnimal ==========" << std::endl;
|
||||||
|
{
|
||||||
|
// decom for testing
|
||||||
|
|
||||||
|
// std::cout << "\n========== AAnimal ==========" << std::endl;
|
||||||
|
// const AAnimal* meta = new AAnimal();
|
||||||
|
// std::cout << meta->getType() << " " << std::endl;
|
||||||
|
// meta->makeSound();
|
||||||
|
// delete meta;
|
||||||
|
|
||||||
|
std::cout << "\n========== Dog ==========" << std::endl;
|
||||||
|
const AAnimal* j = new Dog();
|
||||||
|
std::cout << j->getType() << " " << std::endl;
|
||||||
|
j->makeSound();
|
||||||
|
delete j;
|
||||||
|
|
||||||
|
std::cout << "\n========== Cat ==========" << std::endl;
|
||||||
|
const AAnimal* i = new Cat();
|
||||||
|
std::cout << i->getType() << " " << std::endl;
|
||||||
|
i->makeSound();
|
||||||
|
delete i;
|
||||||
|
}
|
||||||
|
std::cout << "\n========== TEST 1: Array of 10 Animals ==========" << std::endl;
|
||||||
|
{
|
||||||
|
const int size = 10;
|
||||||
|
AAnimal* 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 << "AAnimal " << 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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
CXX = c++
|
||||||
|
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||||
|
OBJDIR = obj
|
||||||
|
SOURCES = Main.cpp Dog.cpp Cat.cpp AAnimal.cpp WrongAnimal.cpp WrongCat.cpp Brain.cpp
|
||||||
|
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||||
|
NAME = AAnimal
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue