ex02 finished

This commit is contained in:
2025-11-10 20:50:53 +01:00
parent 2438853723
commit fa95f2b5b6
14 changed files with 567 additions and 0 deletions

47
ex02/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
ex02/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 = 0;
std::string getType() const;
};
#endif

62
ex02/Brain/Brain.cpp Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 11:22:21 by aortigos #+# #+# */
/* Updated: 2025/11/10 20:43:06 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Brain.hpp"
Brain::Brain()
{
std::cout << "Brain has been created" << std::endl;
int i = 0;
while (i < 100)
{
ideas[i++] = "Empty idea";
}
}
Brain::~Brain()
{
std::cout << "Brain has been destroyed" << std::endl;
}
Brain::Brain(const Brain &other)
{
std::cout << "Brain copied" << std::endl;
*this = other;
}
Brain& Brain::operator=(const Brain &other)
{
std::cout << "Brain copy assignment called" << std::endl;
if (this != &other)
{
int i = 0;
while (i < 100)
{
this->ideas[i] = other.ideas[i];
i++;
}
}
return (*this);
}
void Brain::setIdea(int i, const std::string &idea)
{
if (i >= 0 && i < 100)
ideas[i] = idea;
}
std::string Brain::getIdea(int i) const
{
if (i >= 0 && i < 100)
return (this->ideas[i]);
return ("Invalid idea");
}

34
ex02/Brain/Brain.hpp Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 11:22:17 by aortigos #+# #+# */
/* Updated: 2025/11/10 20:42:43 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BRAIN_HPP
# define BRAIN_HPP
#include <iostream>
class Brain
{
private:
std::string ideas[100];
public:
Brain();
~Brain();
Brain(const Brain &other);
Brain& operator=(const Brain &other);
void setIdea(int i, const std::string &idea);
std::string getIdea(int i) const;
};
#endif

51
ex02/Cat/Cat.cpp Normal file
View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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) : Animal(other)
{
std::cout << "Cat copied" << std::endl;
this->brain = NULL;
*this = other;
}
Cat& Cat::operator=(const Cat &other)
{
std::cout << "Cat copy assignment called" << std::endl;
if (this != &other)
{
Animal::operator=(other);
if (this->brain)
delete (this->brain);
this->brain = new Brain(*other.brain);
}
return (*this);
}
void Cat::makeSound() const
{
std::cout << "Meow!" << std::endl;
}

37
ex02/Cat/Cat.hpp Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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;
Brain* getBrain() const { return brain; }
};
#endif

51
ex02/Dog/Dog.cpp Normal file
View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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) : Animal(other)
{
std::cout << "Dog copied" << std::endl;
this->brain = NULL;
*this = other;
}
Dog& Dog::operator=(const Dog &other)
{
std::cout << "Dog copy assignment called" << std::endl;
if (this != &other)
{
Animal::operator=(other);
if (this->brain)
delete (this->brain);
this->brain = new Brain(*other.brain);
}
return (*this);
}
void Dog::makeSound() const
{
std::cout << "Woof!" << std::endl;
}

38
ex02/Dog/Dog.hpp Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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;
Brain* getBrain() const { return brain; }
};
#endif

28
ex02/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) : WrongAnimal(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

26
ex02/main.cpp Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:43:10 by hadi #+# #+# */
/* Updated: 2025/11/10 20:50:41 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Animal/Animal.hpp"
#include "Dog/Dog.hpp"
#include "Cat/Cat.hpp"
int main()
{
//const Animal* j = new Animal();
const Animal* i = new Cat();
//j->makeSound();
i->makeSound();
return (0);
}