From fa95f2b5b688a1a980cae21d16446dcbbae7aa64 Mon Sep 17 00:00:00 2001 From: aortigos Date: Mon, 10 Nov 2025 20:50:53 +0100 Subject: [PATCH] ex02 finished --- ex02/Animal/Animal.cpp | 47 ++++++++++++++++++++++++ ex02/Animal/Animal.hpp | 33 +++++++++++++++++ ex02/Brain/Brain.cpp | 62 ++++++++++++++++++++++++++++++++ ex02/Brain/Brain.hpp | 34 ++++++++++++++++++ ex02/Cat/Cat.cpp | 51 ++++++++++++++++++++++++++ ex02/Cat/Cat.hpp | 37 +++++++++++++++++++ ex02/Dog/Dog.cpp | 51 ++++++++++++++++++++++++++ ex02/Dog/Dog.hpp | 38 ++++++++++++++++++++ ex02/Makefile | 28 +++++++++++++++ ex02/WrongAnimal/WrongAnimal.cpp | 47 ++++++++++++++++++++++++ ex02/WrongAnimal/WrongAnimal.hpp | 33 +++++++++++++++++ ex02/WrongCat/WrongCat.cpp | 48 +++++++++++++++++++++++++ ex02/WrongCat/WrongCat.hpp | 32 +++++++++++++++++ ex02/main.cpp | 26 ++++++++++++++ 14 files changed, 567 insertions(+) create mode 100644 ex02/Animal/Animal.cpp create mode 100644 ex02/Animal/Animal.hpp create mode 100644 ex02/Brain/Brain.cpp create mode 100644 ex02/Brain/Brain.hpp create mode 100644 ex02/Cat/Cat.cpp create mode 100644 ex02/Cat/Cat.hpp create mode 100644 ex02/Dog/Dog.cpp create mode 100644 ex02/Dog/Dog.hpp create mode 100644 ex02/Makefile create mode 100644 ex02/WrongAnimal/WrongAnimal.cpp create mode 100644 ex02/WrongAnimal/WrongAnimal.hpp create mode 100644 ex02/WrongCat/WrongCat.cpp create mode 100644 ex02/WrongCat/WrongCat.hpp create mode 100644 ex02/main.cpp diff --git a/ex02/Animal/Animal.cpp b/ex02/Animal/Animal.cpp new file mode 100644 index 0000000..63dd2bf --- /dev/null +++ b/ex02/Animal/Animal.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos type = other.type; + return (*this); +} + +void Animal::makeSound() const +{ + std::cout << "Animal weird sounds..." << std::endl; +} + +std::string Animal::getType() const +{ + return (this->type); +} \ No newline at end of file diff --git a/ex02/Animal/Animal.hpp b/ex02/Animal/Animal.hpp new file mode 100644 index 0000000..736f4d0 --- /dev/null +++ b/ex02/Animal/Animal.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos + +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 diff --git a/ex02/Brain/Brain.cpp b/ex02/Brain/Brain.cpp new file mode 100644 index 0000000..e18ed94 --- /dev/null +++ b/ex02/Brain/Brain.cpp @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 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"); +} \ No newline at end of file diff --git a/ex02/Brain/Brain.hpp b/ex02/Brain/Brain.hpp new file mode 100644 index 0000000..c95fe84 --- /dev/null +++ b/ex02/Brain/Brain.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos + +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 diff --git a/ex02/Cat/Cat.cpp b/ex02/Cat/Cat.cpp new file mode 100644 index 0000000..a2a51c4 --- /dev/null +++ b/ex02/Cat/Cat.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 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; +} diff --git a/ex02/Cat/Cat.hpp b/ex02/Cat/Cat.hpp new file mode 100644 index 0000000..fe4460f --- /dev/null +++ b/ex02/Cat/Cat.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# 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 diff --git a/ex02/Dog/Dog.cpp b/ex02/Dog/Dog.cpp new file mode 100644 index 0000000..abf6aad --- /dev/null +++ b/ex02/Dog/Dog.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 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; +} diff --git a/ex02/Dog/Dog.hpp b/ex02/Dog/Dog.hpp new file mode 100644 index 0000000..dd62dce --- /dev/null +++ b/ex02/Dog/Dog.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# 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 diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..9859137 --- /dev/null +++ b/ex02/Makefile @@ -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 diff --git a/ex02/WrongAnimal/WrongAnimal.cpp b/ex02/WrongAnimal/WrongAnimal.cpp new file mode 100644 index 0000000..5573a0b --- /dev/null +++ b/ex02/WrongAnimal/WrongAnimal.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos type = other.type; + return (*this); +} + +void WrongAnimal::makeSound() const +{ + std::cout << "WrongAnimal weird sounds..." << std::endl; +} + +std::string WrongAnimal::getType() const +{ + return (this->type); +} \ No newline at end of file diff --git a/ex02/WrongAnimal/WrongAnimal.hpp b/ex02/WrongAnimal/WrongAnimal.hpp new file mode 100644 index 0000000..1f54504 --- /dev/null +++ b/ex02/WrongAnimal/WrongAnimal.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos + +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 diff --git a/ex02/WrongCat/WrongCat.cpp b/ex02/WrongCat/WrongCat.cpp new file mode 100644 index 0000000..26045bc --- /dev/null +++ b/ex02/WrongCat/WrongCat.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 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); +} \ No newline at end of file diff --git a/ex02/WrongCat/WrongCat.hpp b/ex02/WrongCat/WrongCat.hpp new file mode 100644 index 0000000..a383995 --- /dev/null +++ b/ex02/WrongCat/WrongCat.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# 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 diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..e0fd5c7 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos makeSound(); + i->makeSound(); + + return (0); +}