diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..6601adb --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,25 @@ +CXX = c++ +CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g +SRCDIR = src +OBJDIR = obj +NAME = ex02 + +SRCS = $(wildcard $(SRCDIR)/*.cpp) +OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + mkdir -p obj + $(CXX) $(CPPFLAGS) -c $< -o $@ + +$(NAME): $(OBJS) + $(CXX) $(CPPFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR)/*.o + +fclean: clean + rm -fr $(NAME) + +re: fclean all diff --git a/ex02/src/AAnimal.cpp b/ex02/src/AAnimal.cpp new file mode 100644 index 0000000..8297455 --- /dev/null +++ b/ex02/src/AAnimal.cpp @@ -0,0 +1,34 @@ +#include "AAnimal.hpp" +#include +#include + +AAnimal::AAnimal() +{ + std::cout << "AAnimal()" << std::endl; +} + +AAnimal::AAnimal(const AAnimal& src) +{ + *this = src; +} + +AAnimal& AAnimal::operator=(const AAnimal &src) +{ + this->type = src.type; + return *this; +} + +AAnimal::~AAnimal() +{ + std::cout << "~AAnimal()" << std::endl; +} + +void AAnimal::makeSound() const +{ + std::cout << "OuGAABounga" << std::endl; +} + +std::string AAnimal::getType() const +{ + return this->type; +} diff --git a/ex02/src/AAnimal.hpp b/ex02/src/AAnimal.hpp new file mode 100644 index 0000000..d7fcd07 --- /dev/null +++ b/ex02/src/AAnimal.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +class AAnimal +{ + public: + AAnimal(); + AAnimal(const AAnimal& src); + AAnimal &operator=(const AAnimal& src); + virtual ~AAnimal(); + + std::string getType() const; + + virtual void makeSound() const; + + protected: + std::string type; +}; diff --git a/ex02/src/Brain.cpp b/ex02/src/Brain.cpp new file mode 100644 index 0000000..44e8356 --- /dev/null +++ b/ex02/src/Brain.cpp @@ -0,0 +1,24 @@ +#include "Brain.hpp" +#include + +Brain& Brain::operator=(const Brain &src) +{ + for (size_t i = 0; i < 100; i++) + this->ideas[i] = src.ideas[i]; + return *this; +} + +Brain::~Brain() +{ + std::cout << "~Brain()" << std::endl; +} + +Brain::Brain() +{ + std::cout << "Brain()" << std::endl; +} + +Brain::Brain(const Brain& src) +{ + *this = src; +} diff --git a/ex02/src/Brain.hpp b/ex02/src/Brain.hpp new file mode 100644 index 0000000..99b16b4 --- /dev/null +++ b/ex02/src/Brain.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +class Brain +{ + public: + Brain(); + Brain(const Brain& src); + ~Brain(); + Brain& operator=(const Brain& src); + private: + std::string ideas[100]; +}; diff --git a/ex02/src/Cat.cpp b/ex02/src/Cat.cpp new file mode 100644 index 0000000..6382242 --- /dev/null +++ b/ex02/src/Cat.cpp @@ -0,0 +1,31 @@ +#include "Cat.hpp" +#include + +Cat::Cat() +{ + this->brain = new Brain(); + this->type = "Cat"; + std::cout << "Cat()" << std::endl; +} + +Cat& Cat::operator=(const Cat& src) +{ + this->type = src.type; + return *this; +} + +Cat::~Cat() +{ + delete this->brain; + std::cout << "~Cat()" << std::endl; +} + +Cat::Cat(const Cat& src): AAnimal() +{ + *this = src; +} + +void Cat::makeSound() const +{ + std::cout << "Meow Meow !" << std::endl; +} diff --git a/ex02/src/Cat.hpp b/ex02/src/Cat.hpp new file mode 100644 index 0000000..273ce8c --- /dev/null +++ b/ex02/src/Cat.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include "./AAnimal.hpp" +#include "./Brain.hpp" + +class Cat : public AAnimal +{ + public: + Cat(const Cat& src); + Cat &operator=(const Cat& src); + Cat(); + ~Cat(); + + void makeSound() const; + + private: + Brain* brain; +}; diff --git a/ex02/src/Dog.cpp b/ex02/src/Dog.cpp new file mode 100644 index 0000000..6892b2d --- /dev/null +++ b/ex02/src/Dog.cpp @@ -0,0 +1,32 @@ +#include "Dog.hpp" + +#include + +Dog::Dog() +{ + this->type = "Dog"; + this->brain = new Brain(); + std::cout << "Dog()" << std::endl; +} + +Dog& Dog::operator=(const Dog& src) +{ + this->type = src.type; + return *this; +} + +Dog::~Dog() +{ + delete this->brain; + std::cout << "~Dog()" << std::endl; +} + +Dog::Dog(const Dog& src): AAnimal() +{ + *this = src; +} + +void Dog::makeSound() const +{ + std::cout << "Ouaf ouaf !" << std::endl; +} diff --git a/ex02/src/Dog.hpp b/ex02/src/Dog.hpp new file mode 100644 index 0000000..183e0b9 --- /dev/null +++ b/ex02/src/Dog.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include "./AAnimal.hpp" +#include "./Brain.hpp" + +class Dog : public AAnimal +{ + public: + Dog(const Dog& src); + Dog &operator=(const Dog& src); + Dog(); + ~Dog(); + + void makeSound() const; + + private: + Brain* brain; +}; diff --git a/ex02/src/WrongAnimal.cpp b/ex02/src/WrongAnimal.cpp new file mode 100644 index 0000000..fd22d4f --- /dev/null +++ b/ex02/src/WrongAnimal.cpp @@ -0,0 +1,34 @@ +#include "WrongAnimal.hpp" +#include +#include + +WrongAnimal::WrongAnimal() +{ + std::cout << "WrongAnimal()" << std::endl; +} + +WrongAnimal::WrongAnimal(const WrongAnimal& src) +{ + *this = src; +} + +WrongAnimal& WrongAnimal::operator=(const WrongAnimal &src) +{ + this->type = src.type; + return *this; +} + +WrongAnimal::~WrongAnimal() +{ + std::cout << "~WrongAnimal()" << std::endl; +} + +void WrongAnimal::makeSound() const +{ + std::cout << "OuGABounga" << std::endl; +} + +std::string WrongAnimal::getType() const +{ + return this->type; +} diff --git a/ex02/src/WrongAnimal.hpp b/ex02/src/WrongAnimal.hpp new file mode 100644 index 0000000..850f4f0 --- /dev/null +++ b/ex02/src/WrongAnimal.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +class WrongAnimal +{ + public: + WrongAnimal(); + WrongAnimal(const WrongAnimal& src); + WrongAnimal &operator=(const WrongAnimal& src); + virtual ~WrongAnimal(); + + std::string getType() const; + + void makeSound() const; + + protected: + std::string type; +}; diff --git a/ex02/src/WrongCat.cpp b/ex02/src/WrongCat.cpp new file mode 100644 index 0000000..5dd473c --- /dev/null +++ b/ex02/src/WrongCat.cpp @@ -0,0 +1,24 @@ +#include "WrongCat.hpp" +#include + +WrongCat::WrongCat() +{ + this->type = "WrongCat"; + std::cout << "WrongCat()" << std::endl; +} + +WrongCat& WrongCat::operator=(const WrongCat& src) +{ + this->type = src.type; + return *this; +} + +WrongCat::~WrongCat() +{ + std::cout << "~WrongCat()" << std::endl; +} + +WrongCat::WrongCat(const WrongCat& src): WrongAnimal() +{ + *this = src; +} diff --git a/ex02/src/WrongCat.hpp b/ex02/src/WrongCat.hpp new file mode 100644 index 0000000..e2b6f10 --- /dev/null +++ b/ex02/src/WrongCat.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +#include "./WrongAnimal.hpp" + +class WrongCat : public WrongAnimal +{ + public: + WrongCat(const WrongCat& src); + WrongCat &operator=(const WrongCat& src); + WrongCat(); + ~WrongCat(); +}; diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp new file mode 100644 index 0000000..c0e9995 --- /dev/null +++ b/ex02/src/main.cpp @@ -0,0 +1,16 @@ +#include + +#include "AAnimal.hpp" +#include "Cat.hpp" +#include "Dog.hpp" +#include "WrongCat.hpp" + +int main() +{ + const AAnimal* j = new Dog(); + delete j; + const AAnimal* i = new Cat(); + delete i; + // const AAnimal* meta = new AAnimal(); + return 0; +}