From 40895a8da7a1b2536eb30c756b2c93504fac9cf1 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 24 Oct 2024 18:54:58 +0200 Subject: [PATCH 1/1] Implement the solution to ex00 --- ex00/Animal.cpp | 39 ++++++++++++++++++++++++++++++ ex00/Animal.h | 22 +++++++++++++++++ ex00/Cat.cpp | 32 +++++++++++++++++++++++++ ex00/Cat.h | 20 ++++++++++++++++ ex00/Dog.cpp | 32 +++++++++++++++++++++++++ ex00/Dog.h | 20 ++++++++++++++++ ex00/Makefile | 57 ++++++++++++++++++++++++++++++++++++++++++++ ex00/WrongAnimal.cpp | 39 ++++++++++++++++++++++++++++++ ex00/WrongAnimal.h | 22 +++++++++++++++++ ex00/WrongCat.cpp | 32 +++++++++++++++++++++++++ ex00/WrongCat.h | 20 ++++++++++++++++ ex00/main.cpp | 26 ++++++++++++++++++++ 12 files changed, 361 insertions(+) create mode 100644 ex00/Animal.cpp create mode 100644 ex00/Animal.h create mode 100644 ex00/Cat.cpp create mode 100644 ex00/Cat.h create mode 100644 ex00/Dog.cpp create mode 100644 ex00/Dog.h create mode 100644 ex00/Makefile create mode 100644 ex00/WrongAnimal.cpp create mode 100644 ex00/WrongAnimal.h create mode 100644 ex00/WrongCat.cpp create mode 100644 ex00/WrongCat.h create mode 100644 ex00/main.cpp diff --git a/ex00/Animal.cpp b/ex00/Animal.cpp new file mode 100644 index 0000000..fff5e5e --- /dev/null +++ b/ex00/Animal.cpp @@ -0,0 +1,39 @@ +#include "Animal.h" +#include +#include + +Animal::Animal(std::string type) + : m_type(type) +{ + std::cout << "Animal default cstr\n"; +} + +Animal::Animal(const Animal &other) +{ + std::cout << "Animal copy cstr\n"; + *this = other; +} + +Animal::~Animal() +{ + std::cout << "Animal destr\n"; +} + +Animal &Animal::operator=(const Animal &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +// This would better be purely virtual function but the subject mandates that +// even the base must implement this function. +void Animal::makeSound() const +{ + std::cout << "BASE ANIMAL SOUNDS\n"; +} + +std::string Animal::getType() const +{ + return (m_type); +} diff --git a/ex00/Animal.h b/ex00/Animal.h new file mode 100644 index 0000000..2115075 --- /dev/null +++ b/ex00/Animal.h @@ -0,0 +1,22 @@ +#ifndef ANIMAL_H +# define ANIMAL_H + +# include + +class Animal +{ + protected: + std::string m_type; + + public: + Animal(std::string type = "BASE"); + Animal(const Animal &other); + ~Animal(); + + Animal &operator=(const Animal &other); + + virtual void makeSound() const; + std::string getType() const; +}; + +#endif // ANIMAL_H diff --git a/ex00/Cat.cpp b/ex00/Cat.cpp new file mode 100644 index 0000000..def9eb8 --- /dev/null +++ b/ex00/Cat.cpp @@ -0,0 +1,32 @@ +#include "Cat.h" +#include + +Cat::Cat() + : Animal("Cat") +{ + std::cout << "Cat default cstr\n"; +} + +Cat::Cat(const Cat &other) + : Animal("Cat") +{ + std::cout << "Cat copy cstr\n"; + *this = other; +} + +Cat::~Cat() +{ + std::cout << "Cat destr\n"; +} + +Cat &Cat::operator=(const Cat &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +void Cat::makeSound() const +{ + std::cout << "Meow :3\n"; +} diff --git a/ex00/Cat.h b/ex00/Cat.h new file mode 100644 index 0000000..77a3c09 --- /dev/null +++ b/ex00/Cat.h @@ -0,0 +1,20 @@ +#ifndef CAT_H +# define CAT_H + +# include "Animal.h" + +class Cat : public Animal +{ + private: + + public: + Cat(); + Cat(const Cat &other); + ~Cat(); + + Cat &operator=(const Cat &other); + + void makeSound() const; +}; + +#endif // CAT_H diff --git a/ex00/Dog.cpp b/ex00/Dog.cpp new file mode 100644 index 0000000..c81191d --- /dev/null +++ b/ex00/Dog.cpp @@ -0,0 +1,32 @@ +#include "Dog.h" +#include + +Dog::Dog() + : Animal("Dog") +{ + std::cout << "Dog default cstr\n"; +} + +Dog::Dog(const Dog &other) + : Animal("Dog") +{ + std::cout << "Dog copy cstr\n"; + *this = other; +} + +Dog::~Dog() +{ + std::cout << "Dog destr\n"; +} + +Dog &Dog::operator=(const Dog &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +void Dog::makeSound() const +{ + std::cout << "Bark!\n"; +} diff --git a/ex00/Dog.h b/ex00/Dog.h new file mode 100644 index 0000000..db3631b --- /dev/null +++ b/ex00/Dog.h @@ -0,0 +1,20 @@ +#ifndef DOG_H +# define DOG_H + +# include "Animal.h" + +class Dog : public Animal +{ + private: + + public: + Dog(); + Dog(const Dog &other); + ~Dog(); + + Dog &operator=(const Dog &other); + + void makeSound() const; +}; + +#endif // DOG_H diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..8e04f72 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,57 @@ +CC := c++ +CFLAGS = -std=c++98 -Wall -Wextra -Werror -Wpedantic + +ifneq ("$(wildcard .debug)","") + CFLAGS += -g +endif + +RM := rm -f + +INCDIR := inc +INCDIR += $(addsuffix /inc, $(SUBPROJECTS)); +ifneq ($(INCLUDE),) + INCLUDE := $(addprefix -I, $(INCDIR)) +endif + +SRCDIR := . + +SOURCES := main.cpp \ + Animal.cpp \ + Cat.cpp \ + Dog.cpp \ + WrongAnimal.cpp \ + WrongCat.cpp \ + +SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) + +OBJECTS := $(SOURCES:.cpp=.o) + +NAME := zoo + +all : $(NAME) + +debug : .debug + $(MAKE) all + +nodebug : + $(RM) .debug + $(MAKE) re + +.% : + $(MAKE) fclean + touch $@ + +$(NAME) : $(OBJECTS) + $(CC) $(CFLAGS) -o $@ $^ $(LINKS) + +%.o : %.cpp + $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) + +clean : + $(RM) $(OBJECTS) + +fclean : clean + $(RM) $(NAME) + +re : fclean + $(MAKE) all diff --git a/ex00/WrongAnimal.cpp b/ex00/WrongAnimal.cpp new file mode 100644 index 0000000..097a8a9 --- /dev/null +++ b/ex00/WrongAnimal.cpp @@ -0,0 +1,39 @@ +#include "WrongAnimal.h" +#include +#include + +WrongAnimal::WrongAnimal(std::string type) + : m_type(type) +{ + std::cout << "WrongAnimal default cstr\n"; +} + +WrongAnimal::WrongAnimal(const WrongAnimal &other) +{ + std::cout << "WrongAnimal copy cstr\n"; + *this = other; +} + +WrongAnimal::~WrongAnimal() +{ + std::cout << "WrongAnimal destr\n"; +} + +WrongAnimal &WrongAnimal::operator=(const WrongAnimal &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +// This would better be purely virtual function but the subject mandates that +// even the base must implement this function. +void WrongAnimal::makeSound() const +{ + std::cout << "BASE ANIMAL SOUNDS\n"; +} + +std::string WrongAnimal::getType() const +{ + return (m_type); +} diff --git a/ex00/WrongAnimal.h b/ex00/WrongAnimal.h new file mode 100644 index 0000000..39a59a3 --- /dev/null +++ b/ex00/WrongAnimal.h @@ -0,0 +1,22 @@ +#ifndef WRONGANIMAL_H +# define WRONGANIMAL_H + +# include + +class WrongAnimal +{ + protected: + std::string m_type; + + public: + WrongAnimal(std::string type = "BASE"); + WrongAnimal(const WrongAnimal &other); + ~WrongAnimal(); + + WrongAnimal &operator=(const WrongAnimal &other); + + void makeSound() const; + std::string getType() const; +}; + +#endif // WRONGANIMAL_H diff --git a/ex00/WrongCat.cpp b/ex00/WrongCat.cpp new file mode 100644 index 0000000..50d7745 --- /dev/null +++ b/ex00/WrongCat.cpp @@ -0,0 +1,32 @@ +#include "WrongCat.h" +#include + +WrongCat::WrongCat() + : WrongAnimal("WrongCat") +{ + std::cout << "WrongCat default cstr\n"; +} + +WrongCat::WrongCat(const WrongCat &other) + : WrongAnimal("WrongCat") +{ + std::cout << "WrongCat copy cstr\n"; + *this = other; +} + +WrongCat::~WrongCat() +{ + std::cout << "WrongCat destr\n"; +} + +WrongCat &WrongCat::operator=(const WrongCat &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +void WrongCat::makeSound() const +{ + std::cout << "Meow :3\n"; +} diff --git a/ex00/WrongCat.h b/ex00/WrongCat.h new file mode 100644 index 0000000..c2d81e4 --- /dev/null +++ b/ex00/WrongCat.h @@ -0,0 +1,20 @@ +#ifndef WRONGCAT_H +# define WRONGCAT_H + +# include "WrongAnimal.h" + +class WrongCat : public WrongAnimal +{ + private: + + public: + WrongCat(); + WrongCat(const WrongCat &other); + ~WrongCat(); + + WrongCat &operator=(const WrongCat &other); + + void makeSound() const; +}; + +#endif // WRONGCAT_H diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..856c8a1 --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,26 @@ +#include "Animal.h" +#include "Cat.h" +#include "Dog.h" +#include "WrongAnimal.h" +#include "WrongCat.h" +#include + +int main() +{ + const Animal* meta = new Animal(); + const Animal* j = new Dog(); + const Animal* i = new Cat(); + + const WrongAnimal* meta2 = new WrongAnimal(); + const WrongAnimal* i2 = new WrongCat(); + + std::cout << j->getType() << " " << std::endl; + std::cout << i->getType() << " " << std::endl; + i->makeSound(); + j->makeSound(); + meta->makeSound(); + std::cout << i2->getType() << " " << std::endl; + i2->makeSound(); + meta2->makeSound(); + return (0); +} -- 2.30.2