Brain added

This commit is contained in:
Angel Ortigosa Perez
2025-11-10 11:47:40 +01:00
parent 5ab7e97c1e
commit a6f3e7ea31
14 changed files with 526 additions and 0 deletions

47
ex01/Animal/Animal.cpp Normal file
View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Animal.hpp"
Animal::Animal() : type("Animal")
{
std::cout << "Animal has been created" << std::endl;
}
Animal::~Animal()
{
std::cout << "Animal has been destroyed" << std::endl;
}
Animal::Animal(const Animal &other)
{
std::cout << "Animal copied" << std::endl;
*this = other;
}
Animal& Animal::operator=(const Animal &other)
{
std::cout << "Animal copy assignment called" << std::endl;
if (this != &other)
this->type = other.type;
return (*this);
}
void Animal::makeSound() const
{
std::cout << "Animal weird sounds..." << std::endl;
}
std::string Animal::getType() const
{
return (this->type);
}

33
ex01/Animal/Animal.hpp Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ANIMAL_HPP
# define ANIMAL_HPP
# include <iostream>
class Animal
{
protected:
std::string type;
public:
Animal();
virtual ~Animal();
Animal(const Animal &other);
Animal& operator=(const Animal &other);
virtual void makeSound() const;
std::string getType() const;
};
#endif

23
ex01/Brain/Brain.cpp Normal file
View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 11:22:21 by aortigos #+# #+# */
/* Updated: 2025/11/10 11:33:52 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Brain.hpp"
Brain::Brain()
{
std::cout << "Brain has been created" << std::endl;
}
Brain::~Brain()
{
std::cout << "Brain has been destroyed" << std::endl;
}

29
ex01/Brain/Brain.hpp Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 11:22:17 by aortigos #+# #+# */
/* Updated: 2025/11/10 11:32:41 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BRAIN_HPP
# define BRAIN_HPP
#include <iostream>
class Brain
{
private:
std::string ideas[100];
public:
Brain();
~Brain();
};
#endif

45
ex01/Cat/Cat.cpp Normal file
View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Cat.hpp"
Cat::Cat() : Animal()
{
std::cout << "Cat has been created" << std::endl;
this->type = "Cat";
this->brain = new Brain();
}
Cat::~Cat()
{
std::cout << "Cat has been destroyed" << std::endl;
delete (this->brain);
}
Cat::Cat(const Cat &other)
{
std::cout << "Cat copied" << std::endl;
*this = other;
}
Cat& Cat::operator=(const Cat &other)
{
std::cout << "Cat copy assignment called" << std::endl;
if (this != &other)
this->type = other.type;
return (*this);
}
void Cat::makeSound() const
{
std::cout << "Meow!" << std::endl;
}

35
ex01/Cat/Cat.hpp Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CAT_HPP
# define CAT_HPP
# include <iostream>
# include "../Animal/Animal.hpp"
# include "../Brain/Brain.hpp"
class Cat : public Animal
{
private:
Brain *brain;
public:
Cat();
~Cat();
Cat(const Cat &other);
Cat& operator=(const Cat &other);
void makeSound() const;
};
#endif

45
ex01/Dog/Dog.cpp Normal file
View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Dog.hpp"
Dog::Dog() : Animal()
{
std::cout << "Dog has been created" << std::endl;
this->type = "Dog";
this->brain = new Brain();
}
Dog::~Dog()
{
std::cout << "Dog has been destroyed" << std::endl;
delete (this->brain);
}
Dog::Dog(const Dog &other)
{
std::cout << "Dog copied" << std::endl;
*this = other;
}
Dog& Dog::operator=(const Dog &other)
{
std::cout << "Dog copy assignment called" << std::endl;
if (this != &other)
this->type = other.type;
return (*this);
}
void Dog::makeSound() const
{
std::cout << "Woof!" << std::endl;
}

36
ex01/Dog/Dog.hpp Normal file
View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DOG_HPP
# define DOG_HPP
# include <iostream>
# include "../Animal/Animal.hpp"
# include "../Brain/Brain.hpp"
class Dog : public Animal
{
private:
Brain *brain;
public:
Dog();
~Dog();
Dog(const Dog &other);
Dog& operator=(const Dog &other);
void makeSound() const;
};
#endif

28
ex01/Makefile Normal file
View File

@@ -0,0 +1,28 @@
NAME = animals
SRCS = main.cpp Animal/Animal.cpp Dog/Dog.cpp Cat/Cat.cpp \
WrongAnimal/WrongAnimal.cpp WrongCat/WrongCat.cpp \
Brain/Brain.cpp
OBJS = $(SRCS:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "WrongAnimal.hpp"
WrongAnimal::WrongAnimal() : type("WrongAnimal")
{
std::cout << "Animal has been created" << std::endl;
}
WrongAnimal::~WrongAnimal()
{
std::cout << "Animal has been destroyed" << std::endl;
}
WrongAnimal::WrongAnimal(const WrongAnimal &other)
{
std::cout << "WrongAnimal copied" << std::endl;
*this = other;
}
WrongAnimal& WrongAnimal::operator=(const WrongAnimal &other)
{
std::cout << "WrongAnimal copy assignment called" << std::endl;
if (this != &other)
this->type = other.type;
return (*this);
}
void WrongAnimal::makeSound() const
{
std::cout << "WrongAnimal weird sounds..." << std::endl;
}
std::string WrongAnimal::getType() const
{
return (this->type);
}

View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef WRONGANIMAL_HPP
# define WRONGANIMAL_HPP
# include <iostream>
class WrongAnimal
{
protected:
std::string type;
public:
WrongAnimal();
~WrongAnimal();
WrongAnimal(const WrongAnimal &other);
WrongAnimal& operator=(const WrongAnimal &other);
void makeSound() const;
std::string getType() const;
};
#endif

View File

@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "WrongCat.hpp"
WrongCat::WrongCat() : WrongAnimal()
{
std::cout << "Cat has been created" << std::endl;
this->type = "WrongCat";
}
WrongCat::~WrongCat()
{
std::cout << "Cat has been destroyed" << std::endl;
}
WrongCat::WrongCat(const WrongCat &other)
{
std::cout << "WrongCat copied" << std::endl;
*this = other;
}
WrongCat& WrongCat::operator=(const WrongCat &other)
{
std::cout << "WrongCat copy assignment called" << std::endl;
if (this != &other)
this->type = other.type;
return (*this);
}
void WrongCat::makeSound() const
{
std::cout << "WrongCat weird sounds..." << std::endl;
}
std::string WrongCat::getType() const
{
return (this->type);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef WRONGCAT_HPP
# define WRONGCAT_HPP
# include <iostream>
# include "../WrongAnimal/WrongAnimal.hpp"
class WrongCat : public WrongAnimal
{
public:
WrongCat();
~WrongCat();
WrongCat(const WrongCat &other);
WrongCat& operator=(const WrongCat &other);
void makeSound() const;
std::string getType() const;
};
#endif

45
ex01/main.cpp Normal file
View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:43:10 by hadi #+# #+# */
/* Updated: 2025/11/10 10:33:20 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Animal/Animal.hpp"
#include "Dog/Dog.hpp"
#include "Cat/Cat.hpp"
#include "WrongAnimal/WrongAnimal.hpp"
#include "WrongCat/WrongCat.hpp"
int main()
{
std::cout << std::endl << "----- Creating animals ------" << std::endl;
const Animal* meta = new Animal();
const Animal* j = new Dog();
const Animal* i = new Cat();
const WrongAnimal* z = new WrongCat();
std::cout << std::endl << "----- Animal sounds ------" << std::endl;
std::cout << j->getType() << " says: ";
j->makeSound();
std::cout << std::endl << i->getType() << " says ";
i->makeSound();
std::cout << std::endl << z->getType() << " says ";
z->makeSound();
std::cout << std::endl << "----- Free all! ------" << std::endl;
delete (meta);
delete (j);
delete (i);
delete (z);
return (0);
}