From f55c6faed3a82fcb6f4a9eae7dbb2d7637cb39b8 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 7 Apr 2025 17:47:36 +0200 Subject: [PATCH] somewhere in ex02 --- .gitignore | 1 + Makefile | 35 +++++++++++++++ common.mk | 35 +++++++++++++++ ex00/Makefile | 21 +++++++++ ex00/inc/Animal.hpp | 30 +++++++++++++ ex00/inc/Cat.hpp | 27 ++++++++++++ ex00/inc/Dog.hpp | 27 ++++++++++++ ex00/inc/WrongAnimal.hpp | 32 ++++++++++++++ ex00/inc/WrongCat.hpp | 28 ++++++++++++ ex00/src/Animal.cpp | 45 ++++++++++++++++++++ ex00/src/Cat.cpp | 44 +++++++++++++++++++ ex00/src/Dog.cpp | 43 +++++++++++++++++++ ex00/src/WrongAnimal.cpp | 43 +++++++++++++++++++ ex00/src/WrongCat.cpp | 41 ++++++++++++++++++ ex00/src/main.cpp | 92 ++++++++++++++++++++++++++++++++++++++++ ex01/Makefile | 20 +++++++++ ex01/inc/Animal.hpp | 30 +++++++++++++ ex01/inc/Brain.hpp | 37 ++++++++++++++++ ex01/inc/Cat.hpp | 32 ++++++++++++++ ex01/inc/Dog.hpp | 31 ++++++++++++++ ex01/src/Animal.cpp | 45 ++++++++++++++++++++ ex01/src/Brain.cpp | 81 +++++++++++++++++++++++++++++++++++ ex01/src/Cat.cpp | 50 ++++++++++++++++++++++ ex01/src/Dog.cpp | 43 +++++++++++++++++++ ex01/src/main.cpp | 60 ++++++++++++++++++++++++++ ex02/Makefile | 16 +++++++ ex03/Makefile | 16 +++++++ 27 files changed, 1005 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 common.mk create mode 100644 ex00/Makefile create mode 100644 ex00/inc/Animal.hpp create mode 100644 ex00/inc/Cat.hpp create mode 100644 ex00/inc/Dog.hpp create mode 100644 ex00/inc/WrongAnimal.hpp create mode 100644 ex00/inc/WrongCat.hpp create mode 100644 ex00/src/Animal.cpp create mode 100644 ex00/src/Cat.cpp create mode 100644 ex00/src/Dog.cpp create mode 100644 ex00/src/WrongAnimal.cpp create mode 100644 ex00/src/WrongCat.cpp create mode 100644 ex00/src/main.cpp create mode 100644 ex01/Makefile create mode 100644 ex01/inc/Animal.hpp create mode 100644 ex01/inc/Brain.hpp create mode 100644 ex01/inc/Cat.hpp create mode 100644 ex01/inc/Dog.hpp create mode 100644 ex01/src/Animal.cpp create mode 100644 ex01/src/Brain.cpp create mode 100644 ex01/src/Cat.cpp create mode 100644 ex01/src/Dog.cpp create mode 100644 ex01/src/main.cpp create mode 100644 ex02/Makefile create mode 100644 ex03/Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6f9a44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/settings.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b1953c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/21 14:50:00 by whaffman #+# #+# # +# Updated: 2025/03/21 14:58:40 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +EX = $(shell find . -maxdepth 1 -type d -name 'ex*' -exec basename {} \;) + +all: + for ex in $(EX); do \ + $(MAKE) -C $$ex; \ + done + +clean: + for ex in $(EX); do \ + $(MAKE) -C $$ex clean; \ + done + +fclean: + for ex in $(EX); do \ + $(MAKE) -C $$ex fclean; \ + done + +re: + for ex in $(EX); do \ + $(MAKE) -C $$ex re; \ + done + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/common.mk b/common.mk new file mode 100644 index 0000000..4bfab0b --- /dev/null +++ b/common.mk @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# :::::::: # +# common.mk :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/21 15:00:16 by whaffman #+# #+# # +# Updated: 2025/03/21 15:04:44 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +INC = -I./inc +VPATH = src +OBJ = $(SRC:.cpp=.o) +CC = c++ +CFLAGS = -Wall -Wextra -Werror + +all: $(NAME) + +$(NAME): $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $(NAME) + +%.o: %.cpp + $(CC) $(CFLAGS) $(INC) -c $< -o $@ + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..ff83e85 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,21 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/04/05 17:33:17 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= Polymorphism +SRC= main.cpp \ + Animal.cpp \ + Dog.cpp \ + Cat.cpp \ + WrongAnimal.cpp \ + WrongCat.cpp + +-include ../common.mk diff --git a/ex00/inc/Animal.hpp b/ex00/inc/Animal.hpp new file mode 100644 index 0000000..8f3f890 --- /dev/null +++ b/ex00/inc/Animal.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Animal.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 16:53:44 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class Animal +{ +protected: + std::string type; + +public: + Animal(); + Animal(const Animal &animal); + virtual ~Animal(); + Animal &operator=(const Animal &animal); + + virtual void makeSound(void) const; + std::string getType(void) const; +}; \ No newline at end of file diff --git a/ex00/inc/Cat.hpp b/ex00/inc/Cat.hpp new file mode 100644 index 0000000..5bd4ad3 --- /dev/null +++ b/ex00/inc/Cat.hpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Cat.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:02:57 by whaffman #+# #+# */ +/* Updated: 2025/04/05 17:30:47 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Animal.hpp" +#include + +class Cat : public Animal +{ +public: + Cat(); + Cat(const Cat &cat); + ~Cat() override; + Cat &operator=(const Cat &cat); + + void makeSound(void) const override; +}; \ No newline at end of file diff --git a/ex00/inc/Dog.hpp b/ex00/inc/Dog.hpp new file mode 100644 index 0000000..13b8b1f --- /dev/null +++ b/ex00/inc/Dog.hpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Dog.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:04:03 by whaffman #+# #+# */ +/* Updated: 2025/04/05 17:30:41 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Animal.hpp" +#include + +class Dog : public Animal +{ +public: + Dog(); + Dog(const Dog &Dog); + ~Dog() override; + Dog &operator=(const Dog &Dog); + + void makeSound(void) const override; +}; \ No newline at end of file diff --git a/ex00/inc/WrongAnimal.hpp b/ex00/inc/WrongAnimal.hpp new file mode 100644 index 0000000..a573a66 --- /dev/null +++ b/ex00/inc/WrongAnimal.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* WrongAnimal.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:24:04 by whaffman #+# #+# */ +/* Updated: 2025/04/05 17:26:34 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include + +class WrongAnimal +{ +protected: + std::string type; + + +public: + WrongAnimal(); + WrongAnimal(const WrongAnimal &animal); + virtual ~WrongAnimal(); + WrongAnimal &operator=(const WrongAnimal &animal); + + virtual void makeSound(void) const; + std::string getType(void) const; +}; \ No newline at end of file diff --git a/ex00/inc/WrongCat.hpp b/ex00/inc/WrongCat.hpp new file mode 100644 index 0000000..37b3634 --- /dev/null +++ b/ex00/inc/WrongCat.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* WrongCat.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:23:37 by whaffman #+# #+# */ +/* Updated: 2025/04/05 17:30:32 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "WrongAnimal.hpp" +#include +#include + +class WrongCat : public WrongAnimal +{ +public: + WrongCat(); + WrongCat(const WrongCat &cat); + ~WrongCat() override; + WrongCat &operator=(const WrongCat &cat); + + void makeSound(void) const override; +}; \ No newline at end of file diff --git a/ex00/src/Animal.cpp b/ex00/src/Animal.cpp new file mode 100644 index 0000000..3e8fe29 --- /dev/null +++ b/ex00/src/Animal.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Animal.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 16:59:26 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include +#include + +Animal::Animal() +{ + this->type = "Animal"; + std::cout << "Animal default constructor called" << std::endl; +} +Animal::Animal(const Animal &animal) : type(animal.type) +{ + std::cout << "Animal copy constructor called" << std::endl; +} +Animal::~Animal() +{ + std::cout << "Animal destructor called" << std::endl; +} +Animal &Animal::operator=(const Animal &animal) +{ + std::cout << "Animal assignment operator called" << std::endl; + if (this == &animal) + return *this; + + type = animal.type; + return *this; +} +void Animal::makeSound(void) const +{ +} +std::string Animal::getType(void) const +{ + return type; +} \ No newline at end of file diff --git a/ex00/src/Cat.cpp b/ex00/src/Cat.cpp new file mode 100644 index 0000000..098a3bb --- /dev/null +++ b/ex00/src/Cat.cpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Cat.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:05:34 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Cat.hpp" +#include +#include + +Cat::Cat() +{ + this->type = "Cat"; + std::cout << "Cat default constructor called" << std::endl; +} +Cat::Cat(const Cat &cat) : Animal(cat) +{ + this->type = cat.type; + std::cout << "Cat copy constructor called" << std::endl; +} +Cat::~Cat() +{ + std::cout << "Cat destructor called" << std::endl; +} +Cat &Cat::operator=(const Cat &cat) +{ + std::cout << "Cat assignment operator called" << std::endl; + if (this == &cat) + return *this; + + type = cat.type; + return *this; +} + +void Cat::makeSound(void) const +{ + std::cout << "Meow" << std::endl; +} diff --git a/ex00/src/Dog.cpp b/ex00/src/Dog.cpp new file mode 100644 index 0000000..885c3ac --- /dev/null +++ b/ex00/src/Dog.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Dog.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:06:12 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Dog.hpp" +#include +#include + +Dog::Dog() +{ + this->type = "Dog"; + std::cout << "Dog default constructor called" << std::endl; +} +Dog::Dog(const Dog &dog) : Animal(dog) +{ + this->type = dog.type; + std::cout << "Dog copy constructor called" << std::endl; +} +Dog::~Dog() +{ + std::cout << "Dog destructor called" << std::endl; +} +Dog &Dog::operator=(const Dog &dog) +{ + std::cout << "Dog assignment operator called" << std::endl; + if (this == &dog) + return *this; + + type = dog.type; + return *this; +} +void Dog::makeSound(void) const +{ + std::cout << "Woof" << std::endl; +} diff --git a/ex00/src/WrongAnimal.cpp b/ex00/src/WrongAnimal.cpp new file mode 100644 index 0000000..a6eac93 --- /dev/null +++ b/ex00/src/WrongAnimal.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* WrongAnimal.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:26:52 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "WrongAnimal.hpp" +#include +#include + +WrongAnimal::WrongAnimal() +{ + this->type = "WrongAnimal"; + std::cout << "WrongAnimal default constructor called" << std::endl; +} +WrongAnimal::WrongAnimal(const WrongAnimal &animal) : type(animal.type) +{ + std::cout << "WrongAnimal copy constructor called" << std::endl; +} +WrongAnimal::~WrongAnimal() +{ + std::cout << "WrongAnimal destructor called" << std::endl; +} +WrongAnimal &WrongAnimal::operator=(const WrongAnimal &animal) +{ + std::cout << "WrongAnimal assignment operator called" << std::endl; + if (this == &animal) + return *this; + + type = animal.type; + return *this; +} +void WrongAnimal::makeSound(void) const {} +std::string WrongAnimal::getType(void) const +{ + return type; +} \ No newline at end of file diff --git a/ex00/src/WrongCat.cpp b/ex00/src/WrongCat.cpp new file mode 100644 index 0000000..b1831b8 --- /dev/null +++ b/ex00/src/WrongCat.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* WrongCat.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:28:51 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "WrongCat.hpp" + +WrongCat::WrongCat() +{ + this->type = "WrongCat"; + std::cout << "WrongCat default constructor called" << std::endl; +} +WrongCat::WrongCat(const WrongCat &cat) : WrongAnimal(cat) +{ + this->type = cat.type; + std::cout << "WrongCat copy constructor called" << std::endl; +} +WrongCat::~WrongCat() +{ + std::cout << "WrongCat destructor called" << std::endl; +} +WrongCat &WrongCat::operator=(const WrongCat &cat) +{ + std::cout << "WrongCat assignment operator called" << std::endl; + if (this == &cat) + return *this; + + type = cat.type; + return *this; +} +void WrongCat::makeSound(void) const +{ + std::cout << "WrongMeow" << std::endl; +} \ No newline at end of file diff --git a/ex00/src/main.cpp b/ex00/src/main.cpp new file mode 100644 index 0000000..3e234fc --- /dev/null +++ b/ex00/src/main.cpp @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:06:50 by whaffman #+# #+# */ +/* Updated: 2025/04/05 18:01:03 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include "WrongAnimal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" +#include "WrongCat.hpp" +#include + +int main(void) +{ + std::cout << "Construct Animal, Cat and Dog" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "construct an animal:" << std::endl; + const Animal* animal = new Animal(); + std::cout << "construct a cat:" << std::endl; + const Animal* cat = new Cat(); + std::cout << "construct a dog:" << std::endl; + const Animal* dog = new Dog(); + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the getType function" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "animal is of type : " << animal->getType() << std::endl; + std::cout << "cat is of type : " << cat->getType() << std::endl; + std::cout << "dog is of type : " << dog->getType() << std::endl; + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the makeSound function" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "animal sound : "; + animal->makeSound(); + std::cout << std::endl; + std::cout << "cat sound : "; + cat->makeSound(); + std::cout << "dog sound : "; + dog->makeSound(); + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the destructor" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "delete animal:" << std::endl; + delete animal; + std::cout << "delete cat:" << std::endl; + delete cat; + std::cout << "delete dog:" << std::endl; + delete dog; + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "Construct WrongAnimal, WrongCat" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "construct a wrongAnimal:" << std::endl; + const WrongAnimal* wrongAnimal = new WrongAnimal(); + std::cout << "construct a wrongCat:" << std::endl; + const WrongAnimal* wrongCat = new WrongCat(); + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the getType function" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "wrongAnimal is of type : " << wrongAnimal->getType() << std::endl; + std::cout << "wrongCat is of type : " << wrongCat->getType() << std::endl; + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the makeSound function" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "wrongAnimal sound : "; + wrongAnimal->makeSound(); + std::cout << std::endl; + std::cout << "wrongCat sound : "; + wrongCat->makeSound(); + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the destructor" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "delete wrongAnimal:" << std::endl; + delete wrongAnimal; + std::cout << "delete wrongCat:" << std::endl; + delete wrongCat; + std::cout << "========================================" << std::endl << std::endl; + + return 0; +} \ No newline at end of file diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..3a5004e --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,20 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/04/05 17:53:27 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= WithBrains +SRC= main.cpp \ + Animal.cpp \ + Dog.cpp \ + Cat.cpp \ + Brain.cpp + +-include ../common.mk diff --git a/ex01/inc/Animal.hpp b/ex01/inc/Animal.hpp new file mode 100644 index 0000000..8f3f890 --- /dev/null +++ b/ex01/inc/Animal.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Animal.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 16:53:44 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class Animal +{ +protected: + std::string type; + +public: + Animal(); + Animal(const Animal &animal); + virtual ~Animal(); + Animal &operator=(const Animal &animal); + + virtual void makeSound(void) const; + std::string getType(void) const; +}; \ No newline at end of file diff --git a/ex01/inc/Brain.hpp b/ex01/inc/Brain.hpp new file mode 100644 index 0000000..66e2f93 --- /dev/null +++ b/ex01/inc/Brain.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Brain.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:40:33 by whaffman #+# #+# */ +/* Updated: 2025/04/06 11:29:20 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include + +#define MAX_IDEAS 10 + +class Brain +{ +private: + std::array ideas; + +public: + Brain(); + Brain(const Brain &brain); + ~Brain(); + Brain &operator=(const Brain &brain); + + void setIdea(int index, const std::string &idea); + std::string getIdea(int index) const; + void clearIdeas(); + void fillRandomIdeas(); + void printIdeas() const; + void printIdea(int index) const; +}; \ No newline at end of file diff --git a/ex01/inc/Cat.hpp b/ex01/inc/Cat.hpp new file mode 100644 index 0000000..33400cc --- /dev/null +++ b/ex01/inc/Cat.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Cat.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:02:57 by whaffman #+# #+# */ +/* Updated: 2025/04/05 17:50:55 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Animal.hpp" +#include "Brain.hpp" +#include + +class Cat : public Animal +{ +private: + Brain *brain; + +public: + Cat(); + Cat(const Cat &cat); + ~Cat() override; + Cat &operator=(const Cat &cat); + + void makeSound(void) const override; + +}; \ No newline at end of file diff --git a/ex01/inc/Dog.hpp b/ex01/inc/Dog.hpp new file mode 100644 index 0000000..fa05944 --- /dev/null +++ b/ex01/inc/Dog.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Dog.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:04:03 by whaffman #+# #+# */ +/* Updated: 2025/04/05 17:49:51 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Animal.hpp" +#include "Brain.hpp" +#include + +class Dog : public Animal +{ +private: + Brain *brain; + +public: + Dog(); + Dog(const Dog &Dog); + ~Dog() override; + Dog &operator=(const Dog &Dog); + + void makeSound(void) const override; +}; \ No newline at end of file diff --git a/ex01/src/Animal.cpp b/ex01/src/Animal.cpp new file mode 100644 index 0000000..3e8fe29 --- /dev/null +++ b/ex01/src/Animal.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Animal.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 16:59:26 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include +#include + +Animal::Animal() +{ + this->type = "Animal"; + std::cout << "Animal default constructor called" << std::endl; +} +Animal::Animal(const Animal &animal) : type(animal.type) +{ + std::cout << "Animal copy constructor called" << std::endl; +} +Animal::~Animal() +{ + std::cout << "Animal destructor called" << std::endl; +} +Animal &Animal::operator=(const Animal &animal) +{ + std::cout << "Animal assignment operator called" << std::endl; + if (this == &animal) + return *this; + + type = animal.type; + return *this; +} +void Animal::makeSound(void) const +{ +} +std::string Animal::getType(void) const +{ + return type; +} \ No newline at end of file diff --git a/ex01/src/Brain.cpp b/ex01/src/Brain.cpp new file mode 100644 index 0000000..78bd996 --- /dev/null +++ b/ex01/src/Brain.cpp @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Brain.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:42:41 by whaffman #+# #+# */ +/* Updated: 2025/04/06 11:27:41 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Brain.hpp" +#include +#include +#include + +Brain::Brain() +{ + std::cout << "Brain default constructor called" << std::endl; +} +Brain::Brain(const Brain &brain) +{ + std::cout << "Brain copy constructor called" << std::endl; + *this = brain; +} +Brain::~Brain() +{ + std::cout << "Brain destructor called" << std::endl; +} + +Brain &Brain::operator=(const Brain &brain) +{ + std::cout << "Brain assignment operator called" << std::endl; + if (this == &brain) + return *this; + + for (size_t i = 0; i < ideas.size(); ++i) + ideas[i] = brain.ideas[i]; + return *this; +} + +void Brain::setIdea(int index, const std::string &idea) +{ + if (index >= 0 && index < 100) + ideas[index] = idea; +} +void Brain::fillRandomIdeas() +{ + for (size_t i = 0; i < ideas.size(); ++i) + ideas[i] = "Random idea " + std::to_string(i); +} + +std::string Brain::getIdea(int index) const +{ + if (index >= 0 && index < 100) + return ideas[index]; + return ""; +} + +void Brain::clearIdeas() +{ + for (size_t i = 0; i < ideas.size(); ++i) + ideas[i] = ""; +} +void Brain::printIdeas() const +{ + for (size_t i = 0; i < ideas.size(); ++i) + { + if (!ideas[i].empty()) + printIdea(i); + } +} + +void Brain::printIdea(int index) const +{ + if (index >= 0 && index < 100) + std::cout << "Idea " << index << ": " << ideas[index] << std::endl; +} + + diff --git a/ex01/src/Cat.cpp b/ex01/src/Cat.cpp new file mode 100644 index 0000000..785a4f5 --- /dev/null +++ b/ex01/src/Cat.cpp @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Cat.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:05:34 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Cat.hpp" +#include "Brain.hpp" +#include +#include + +Cat::Cat() : Animal(), brain(new Brain()) +{ + type = "Cat"; + std::cout << "Cat default constructor called" << std::endl; +} +Cat::Cat(const Cat &cat) : Animal(cat), brain(new Brain(*cat.brain)) +{ + std::cout << "Cat copy constructor called" << std::endl; +} + +Cat::~Cat() +{ + delete brain; + std::cout << "Cat destructor called" << std::endl; +} +Cat &Cat::operator=(const Cat &cat) +{ + std::cout << "Cat assignment operator called" << std::endl; + if (this == &cat) + return *this; + + delete brain; + brain = new Brain(*cat.brain); + + type = cat.type; + + return *this; +} + +void Cat::makeSound(void) const +{ + std::cout << "Meow" << std::endl; +} diff --git a/ex01/src/Dog.cpp b/ex01/src/Dog.cpp new file mode 100644 index 0000000..885c3ac --- /dev/null +++ b/ex01/src/Dog.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Dog.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:06:12 by whaffman #+# #+# */ +/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Dog.hpp" +#include +#include + +Dog::Dog() +{ + this->type = "Dog"; + std::cout << "Dog default constructor called" << std::endl; +} +Dog::Dog(const Dog &dog) : Animal(dog) +{ + this->type = dog.type; + std::cout << "Dog copy constructor called" << std::endl; +} +Dog::~Dog() +{ + std::cout << "Dog destructor called" << std::endl; +} +Dog &Dog::operator=(const Dog &dog) +{ + std::cout << "Dog assignment operator called" << std::endl; + if (this == &dog) + return *this; + + type = dog.type; + return *this; +} +void Dog::makeSound(void) const +{ + std::cout << "Woof" << std::endl; +} diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp new file mode 100644 index 0000000..6429340 --- /dev/null +++ b/ex01/src/main.cpp @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/05 17:06:50 by whaffman #+# #+# */ +/* Updated: 2025/04/05 18:01:13 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" + +#include + +int main(void) +{ + std::cout << "Construct Animal, Cat and Dog" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "construct an animal:" << std::endl; + const Animal* animal = new Animal(); + std::cout << "construct a cat:" << std::endl; + const Animal* cat = new Cat(); + std::cout << "construct a dog:" << std::endl; + const Animal* dog = new Dog(); + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the getType function" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "animal is of type : " << animal->getType() << std::endl; + std::cout << "cat is of type : " << cat->getType() << std::endl; + std::cout << "dog is of type : " << dog->getType() << std::endl; + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the makeSound function" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "animal sound : "; + animal->makeSound(); + std::cout << std::endl; + std::cout << "cat sound : "; + cat->makeSound(); + std::cout << "dog sound : "; + dog->makeSound(); + std::cout << "========================================" << std::endl << std::endl; + + std::cout << "test the destructor" << std::endl; + std::cout << "----------------------------------------" << std::endl; + std::cout << "delete animal:" << std::endl; + delete animal; + std::cout << "delete cat:" << std::endl; + delete cat; + std::cout << "delete dog:" << std::endl; + delete dog; + std::cout << "========================================" << std::endl << std::endl; + + return 0; +} \ No newline at end of file diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..9013f1c --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,16 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/04/05 16:49:25 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= +SRC= main.cpp + +-include ../common.mk diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..9013f1c --- /dev/null +++ b/ex03/Makefile @@ -0,0 +1,16 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/04/05 16:49:25 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= +SRC= main.cpp + +-include ../common.mk