From 6d4b5788d70a3995ba9375f12d1a82b4cc227189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 30 May 2025 08:14:26 +0200 Subject: [PATCH] ex00: it works! --- .gitignore | 4 ++++ ex00/.gitignore | 1 + ex00/Animal.cpp | 29 +++++++++++++++++++++++ ex00/Animal.hpp | 20 ++++++++++++++++ ex00/Cat.cpp | 20 ++++++++++++++++ ex00/Cat.hpp | 14 +++++++++++ ex00/Dog.cpp | 20 ++++++++++++++++ ex00/Dog.hpp | 14 +++++++++++ ex00/Makefile | 49 +++++++++++++++++++++++++++++++++++++++ ex00/WrongAnimal.cpp | 29 +++++++++++++++++++++++ ex00/WrongAnimal.hpp | 20 ++++++++++++++++ ex00/WrongCat.cpp | 8 +++++++ ex00/WrongCat.hpp | 14 +++++++++++ ex00/WrongDog.cpp | 7 ++++++ ex00/WrongDog.hpp | 14 +++++++++++ ex00/main.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++ 16 files changed, 318 insertions(+) create mode 100644 .gitignore create mode 100644 ex00/.gitignore create mode 100644 ex00/Animal.cpp create mode 100644 ex00/Animal.hpp create mode 100644 ex00/Cat.cpp create mode 100644 ex00/Cat.hpp create mode 100644 ex00/Dog.cpp create mode 100644 ex00/Dog.hpp create mode 100644 ex00/Makefile create mode 100644 ex00/WrongAnimal.cpp create mode 100644 ex00/WrongAnimal.hpp create mode 100644 ex00/WrongCat.cpp create mode 100644 ex00/WrongCat.hpp create mode 100644 ex00/WrongDog.cpp create mode 100644 ex00/WrongDog.hpp create mode 100644 ex00/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a4cfdeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.d +.cache +compile_commands.json diff --git a/ex00/.gitignore b/ex00/.gitignore new file mode 100644 index 0000000..5d61c98 --- /dev/null +++ b/ex00/.gitignore @@ -0,0 +1 @@ +ex00 diff --git a/ex00/Animal.cpp b/ex00/Animal.cpp new file mode 100644 index 0000000..8279207 --- /dev/null +++ b/ex00/Animal.cpp @@ -0,0 +1,29 @@ +#include "Animal.hpp" +#include + +Animal::Animal() { std::cout << __PRETTY_FUNCTION__ << std::endl; } + +Animal::Animal(const Animal &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + type = other.type; +} + +Animal::~Animal() { std::cout << __PRETTY_FUNCTION__ << std::endl; } + +Animal &Animal::operator=(const Animal &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + this->type = other.type; + return *this; +} + +std::string Animal::getType() const { return type; } + +void Animal::makeSound() const { + if (type == "Cat") { + std::cout << "[Meow]" << std::endl; + } else if (type == "Dog") { + std::cout << "[Bark]" << std::endl; + } else { + std::cout << "[Generic Animal Sound]" << std::endl; + } +} diff --git a/ex00/Animal.hpp b/ex00/Animal.hpp new file mode 100644 index 0000000..44461a3 --- /dev/null +++ b/ex00/Animal.hpp @@ -0,0 +1,20 @@ +#ifndef ANIMAL_HPP +#define ANIMAL_HPP + +#include + +class Animal { +public: + Animal(); + Animal(const Animal &other); + ~Animal(); + Animal &operator=(const Animal &other); + + std::string getType() const; + void makeSound() const; + +protected: + std::string type; +}; + +#endif diff --git a/ex00/Cat.cpp b/ex00/Cat.cpp new file mode 100644 index 0000000..9c64030 --- /dev/null +++ b/ex00/Cat.cpp @@ -0,0 +1,20 @@ +#include "Cat.hpp" +#include + +Cat::Cat() { + std::cout << __PRETTY_FUNCTION__ << std::endl; + type = "Cat"; +} + +Cat::Cat(const Cat &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + this->type = other.type; +} + +Cat::~Cat() { std::cout << __PRETTY_FUNCTION__ << std::endl; } + +Cat &Cat::operator=(const Cat &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + this->type = other.type; + return *this; +} diff --git a/ex00/Cat.hpp b/ex00/Cat.hpp new file mode 100644 index 0000000..159b515 --- /dev/null +++ b/ex00/Cat.hpp @@ -0,0 +1,14 @@ +#ifndef CAT_HPP +#define CAT_HPP + +#include "Animal.hpp" + +class Cat : public Animal { +public: + Cat(); + Cat(const Cat &other); + ~Cat(); + Cat &operator=(const Cat &other); +}; + +#endif diff --git a/ex00/Dog.cpp b/ex00/Dog.cpp new file mode 100644 index 0000000..94e252e --- /dev/null +++ b/ex00/Dog.cpp @@ -0,0 +1,20 @@ +#include "Dog.hpp" +#include + +Dog::Dog() { + std::cout << __PRETTY_FUNCTION__ << std::endl; + type = "Dog"; +} + +Dog::Dog(const Dog &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + this->type = other.type; +} + +Dog::~Dog() { std::cout << __PRETTY_FUNCTION__ << std::endl; } + +Dog &Dog::operator=(const Dog &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + this->type = other.type; + return *this; +} diff --git a/ex00/Dog.hpp b/ex00/Dog.hpp new file mode 100644 index 0000000..695dd96 --- /dev/null +++ b/ex00/Dog.hpp @@ -0,0 +1,14 @@ +#ifndef DOG_HPP +#define DOG_HPP + +#include "Animal.hpp" + +class Dog : public Animal { +public: + Dog(); + Dog(const Dog &other); + ~Dog(); + Dog &operator=(const Dog &other); +}; + +#endif diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..e8401d6 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,49 @@ +NAME = ex00 +ifeq ($(CPPFLAGS),) + CPPFLAGS = -Wall -Wextra -Werror -std=c++98 -ggdb -fsanitize=address,undefined +endif +ifeq ($(CXX),) + CXX = c++ +endif +# g++ is the default on 42 computers +ifeq ($(CXX),g++) + CXX = c++ +endif +srcs = \ + Animal.cpp \ + Cat.cpp \ + Dog.cpp \ + WrongAnimal.cpp \ + WrongDog.cpp \ + WrongCat.cpp \ + +main_objs = main.o $(srcs:.cpp=.o) +all_objs = $(main_objs) +deps = $(all_objs:.o=.d) + +.PHONY: all clean fclean re run + +all: $(NAME) + +-include $(deps) + +$(NAME): $(main_objs) + $(CXX) $(CPPFLAGS) -o $@ $^ + +%.o: %.cpp + $(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp + $(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d + +clean: + find . -name '*.o' -print -delete + find . -name '*.d' -print -delete + +fclean: clean + rm -f $(NAME) + +re: + +make fclean + +make all + +run: $(NAME) + ./$(NAME) diff --git a/ex00/WrongAnimal.cpp b/ex00/WrongAnimal.cpp new file mode 100644 index 0000000..cddac7e --- /dev/null +++ b/ex00/WrongAnimal.cpp @@ -0,0 +1,29 @@ +#include "WrongAnimal.hpp" +#include + +WrongAnimal::WrongAnimal() { std::cout << __PRETTY_FUNCTION__ << std::endl; } + +WrongAnimal::WrongAnimal(const WrongAnimal &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + type = other.type; +} + +WrongAnimal::~WrongAnimal() { std::cout << __PRETTY_FUNCTION__ << std::endl; } + +WrongAnimal &WrongAnimal::operator=(const WrongAnimal &other) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + this->type = other.type; + return *this; +} + +std::string WrongAnimal::getType() const { return type; } + +void WrongAnimal::makeSound() const { + if (type == "WrongCat") { + std::cout << "[Eldritch Meow]" << std::endl; + } else if (type == "WrongDog") { + std::cout << "[Eldritch Bark]" << std::endl; + } else { + std::cout << "[Eldritch WrongAnimal Sound]" << std::endl; + } +} diff --git a/ex00/WrongAnimal.hpp b/ex00/WrongAnimal.hpp new file mode 100644 index 0000000..2f2a64f --- /dev/null +++ b/ex00/WrongAnimal.hpp @@ -0,0 +1,20 @@ +#ifndef WRONG_ANIMAL_HPP +#define WRONG_ANIMAL_HPP + +#include + +class WrongAnimal { +public: + WrongAnimal(); + WrongAnimal(const WrongAnimal &other); + ~WrongAnimal(); + WrongAnimal &operator=(const WrongAnimal &other); + + std::string getType() const; + void makeSound() const; + +protected: + std::string type; +}; + +#endif diff --git a/ex00/WrongCat.cpp b/ex00/WrongCat.cpp new file mode 100644 index 0000000..1f2ddf2 --- /dev/null +++ b/ex00/WrongCat.cpp @@ -0,0 +1,8 @@ +#include "WrongCat.hpp" + +#include + +WrongCat::WrongCat() { + std::cout << __PRETTY_FUNCTION__ << std::endl; + type = "WrongCat"; +} diff --git a/ex00/WrongCat.hpp b/ex00/WrongCat.hpp new file mode 100644 index 0000000..b440a81 --- /dev/null +++ b/ex00/WrongCat.hpp @@ -0,0 +1,14 @@ +#ifndef WRONG_CAT_HPP +#define WRONG_CAT_HPP + +#include "WrongAnimal.hpp" + +class WrongCat : public WrongAnimal { +public: + WrongCat(); + WrongCat(const WrongCat &other); + ~WrongCat(); + WrongCat &operator=(const WrongCat &other); +}; + +#endif diff --git a/ex00/WrongDog.cpp b/ex00/WrongDog.cpp new file mode 100644 index 0000000..0838cac --- /dev/null +++ b/ex00/WrongDog.cpp @@ -0,0 +1,7 @@ +#include "WrongDog.hpp" +#include + +WrongDog::WrongDog() { + std::cout << __PRETTY_FUNCTION__ << std::endl; + type = "WrongDog"; +} diff --git a/ex00/WrongDog.hpp b/ex00/WrongDog.hpp new file mode 100644 index 0000000..ea89185 --- /dev/null +++ b/ex00/WrongDog.hpp @@ -0,0 +1,14 @@ +#ifndef WRONG_DOG_HPP +#define WRONG_DOG_HPP + +#include "WrongAnimal.hpp" + +class WrongDog : public WrongAnimal { +public: + WrongDog(); + WrongDog(const WrongDog &other); + ~WrongDog(); + WrongDog &operator=(const WrongDog &other); +}; + +#endif diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..c65ebbc --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,55 @@ +#include "Animal.hpp" +#include "Cat.hpp" +#include "Dog.hpp" +#include "WrongAnimal.hpp" +#include "WrongCat.hpp" +#include "WrongDog.hpp" +#include + +int main() { + { + Animal animal1 = Animal(); + Animal animal2 = animal1; + Animal animal3; + animal3 = animal1; + } + std::cout << std::endl; + { + const Animal *meta = new Animal(); + const Animal *j = new Dog(); + const Animal *i = new Cat(); + + std::cout << j->getType() << " " << std::endl; + std::cout << i->getType() << " " << std::endl; + i->makeSound(); + j->makeSound(); + meta->makeSound(); + + delete meta; + delete j; + delete i; + } + std::cout << std::endl; + { + const WrongAnimal *meta = new WrongAnimal(); + const WrongAnimal *j = new WrongDog(); + const WrongAnimal *i = new WrongCat(); + + std::cout << j->getType() << " " << std::endl; + std::cout << i->getType() << " " << std::endl; + i->makeSound(); + j->makeSound(); + meta->makeSound(); + + delete meta; + delete j; + delete i; + } + std::cout << std::endl; + { + const Dog *dog = new Dog(); + dog->makeSound(); + delete dog; + } + return 0; +}