From d893207a3c59a1b2fb39829125dc715d347a1452 Mon Sep 17 00:00:00 2001 From: aortigos Date: Sun, 9 Nov 2025 21:56:00 +0100 Subject: [PATCH] Animal, Dog and Cat classes created and finished --- ex00/Animal/Animal.cpp | 5 +++++ ex00/Animal/Animal.hpp | 5 +++-- ex00/Cat/Cat.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ ex00/Cat/Cat.hpp | 31 ++++++++++++++++++++++++++++++ ex00/Dog/Dog.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ ex00/Dog/Dog.hpp | 32 +++++++++++++++++++++++++++++++ ex00/Makefile | 4 ++-- ex00/main.cpp | 14 ++++++++++++-- 8 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 ex00/Cat/Cat.cpp create mode 100644 ex00/Cat/Cat.hpp create mode 100644 ex00/Dog/Dog.cpp create mode 100644 ex00/Dog/Dog.hpp diff --git a/ex00/Animal/Animal.cpp b/ex00/Animal/Animal.cpp index 82f859e..63dd2bf 100644 --- a/ex00/Animal/Animal.cpp +++ b/ex00/Animal/Animal.cpp @@ -40,3 +40,8 @@ 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/ex00/Animal/Animal.hpp b/ex00/Animal/Animal.hpp index 0432ea2..88017c9 100644 --- a/ex00/Animal/Animal.hpp +++ b/ex00/Animal/Animal.hpp @@ -18,7 +18,7 @@ class Animal { - private: + protected: std::string type; public: Animal(); @@ -26,7 +26,8 @@ class Animal Animal(const Animal &other); Animal& operator=(const Animal &other); - virtual void makeSound() const; + virtual void makeSound() const; + std::string getType() const; }; #endif diff --git a/ex00/Cat/Cat.cpp b/ex00/Cat/Cat.cpp new file mode 100644 index 0000000..cd35e65 --- /dev/null +++ b/ex00/Cat/Cat.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos type = "Cat"; +} + +Cat::~Cat() +{ + std::cout << "Cat has been destroyed" << std::endl; +} + +Cat::Cat(const Cat &other) +{ + std::cout << "Cat copied" << std::endl; + *this = other; +} + +Cat& Cat::operator=(const Cat &other) +{ + std::cout << "Cat copy assignment called" << std::endl; + if (this != &other) + this->type = other.type; + return (*this); +} + +void Cat::makeSound() const +{ + std::cout << "Meow!" << std::endl; +} diff --git a/ex00/Cat/Cat.hpp b/ex00/Cat/Cat.hpp new file mode 100644 index 0000000..3af8a88 --- /dev/null +++ b/ex00/Cat/Cat.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include "../Animal/Animal.hpp" + +class Cat : public Animal +{ + public: + Cat(); + ~Cat(); + Cat(const Cat &other); + Cat& operator=(const Cat &other); + + void makeSound() const; +}; + +#endif diff --git a/ex00/Dog/Dog.cpp b/ex00/Dog/Dog.cpp new file mode 100644 index 0000000..fda5fb1 --- /dev/null +++ b/ex00/Dog/Dog.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos type = "Dog"; +} + +Dog::~Dog() +{ + std::cout << "Dog has been destroyed" << std::endl; +} + +Dog::Dog(const Dog &other) +{ + std::cout << "Dog copied" << std::endl; + *this = other; +} + +Dog& Dog::operator=(const Dog &other) +{ + std::cout << "Dog copy assignment called" << std::endl; + if (this != &other) + this->type = other.type; + return (*this); +} + +void Dog::makeSound() const +{ + std::cout << "Woof!" << std::endl; +} diff --git a/ex00/Dog/Dog.hpp b/ex00/Dog/Dog.hpp new file mode 100644 index 0000000..c81fb04 --- /dev/null +++ b/ex00/Dog/Dog.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include "../Animal/Animal.hpp" + +class Dog : public Animal +{ + public: + Dog(); + ~Dog(); + Dog(const Dog &other); + Dog& operator=(const Dog &other); + + void makeSound() const; + +}; + +#endif diff --git a/ex00/Makefile b/ex00/Makefile index 98b535c..f1ca206 100644 --- a/ex00/Makefile +++ b/ex00/Makefile @@ -1,9 +1,9 @@ NAME = animals -SRCS = main.cpp +SRCS = main.cpp Animal/Animal.cpp Dog/Dog.cpp Cat/Cat.cpp OBJS = $(SRCS:.cpp=.o) -CC = cc +CC = c++ CFLAGS = -Wall -Wextra -Werror -std=c++98 all: $(NAME) diff --git a/ex00/main.cpp b/ex00/main.cpp index 92d3655..570be1a 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -3,27 +3,37 @@ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hadi +#+ +:+ +#+ */ +/* By: aortigos getType() << " " << std::endl; std::cout << i->getType() << " " << std::endl; + std::cout << std::endl << "----- Making sounds ------" << std::endl; i->makeSound(); j->makeSound(); meta->makeSound(); + std::cout << std::endl << "----- Free all! ------" << std::endl; + delete (meta); + delete (j); + delete (i); + return (0); }