Copy ex00 to ex01
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 07:44:20 +0000 (09:44 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 07:44:20 +0000 (09:44 +0200)
12 files changed:
ex01/Animal.cpp [new file with mode: 0644]
ex01/Animal.h [new file with mode: 0644]
ex01/Cat.cpp [new file with mode: 0644]
ex01/Cat.h [new file with mode: 0644]
ex01/Dog.cpp [new file with mode: 0644]
ex01/Dog.h [new file with mode: 0644]
ex01/Makefile [new file with mode: 0644]
ex01/WrongAnimal.cpp [new file with mode: 0644]
ex01/WrongAnimal.h [new file with mode: 0644]
ex01/WrongCat.cpp [new file with mode: 0644]
ex01/WrongCat.h [new file with mode: 0644]
ex01/main.cpp [new file with mode: 0644]

diff --git a/ex01/Animal.cpp b/ex01/Animal.cpp
new file mode 100644 (file)
index 0000000..fff5e5e
--- /dev/null
@@ -0,0 +1,39 @@
+#include "Animal.h"
+#include <string>
+#include <iostream>
+
+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/ex01/Animal.h b/ex01/Animal.h
new file mode 100644 (file)
index 0000000..d5cba60
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef ANIMAL_H
+# define ANIMAL_H
+
+# include <string>
+
+class Animal
+{
+       protected:
+               std::string     m_type;
+
+       public:
+               Animal(std::string type = "BASE");
+               Animal(const Animal &other);
+               virtual ~Animal();
+
+               Animal  &operator=(const Animal &other);
+
+               virtual void    makeSound() const;
+               std::string             getType() const;
+};
+
+#endif // ANIMAL_H
diff --git a/ex01/Cat.cpp b/ex01/Cat.cpp
new file mode 100644 (file)
index 0000000..def9eb8
--- /dev/null
@@ -0,0 +1,32 @@
+#include "Cat.h"
+#include <iostream>
+
+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/ex01/Cat.h b/ex01/Cat.h
new file mode 100644 (file)
index 0000000..77a3c09
--- /dev/null
@@ -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/ex01/Dog.cpp b/ex01/Dog.cpp
new file mode 100644 (file)
index 0000000..c81191d
--- /dev/null
@@ -0,0 +1,32 @@
+#include "Dog.h"
+#include <iostream>
+
+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/ex01/Dog.h b/ex01/Dog.h
new file mode 100644 (file)
index 0000000..db3631b
--- /dev/null
@@ -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/ex01/Makefile b/ex01/Makefile
new file mode 100644 (file)
index 0000000..8e04f72
--- /dev/null
@@ -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/ex01/WrongAnimal.cpp b/ex01/WrongAnimal.cpp
new file mode 100644 (file)
index 0000000..097a8a9
--- /dev/null
@@ -0,0 +1,39 @@
+#include "WrongAnimal.h"
+#include <string>
+#include <iostream>
+
+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/ex01/WrongAnimal.h b/ex01/WrongAnimal.h
new file mode 100644 (file)
index 0000000..39a59a3
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef WRONGANIMAL_H
+# define WRONGANIMAL_H
+
+# include <string>
+
+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/ex01/WrongCat.cpp b/ex01/WrongCat.cpp
new file mode 100644 (file)
index 0000000..50d7745
--- /dev/null
@@ -0,0 +1,32 @@
+#include "WrongCat.h"
+#include <iostream>
+
+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/ex01/WrongCat.h b/ex01/WrongCat.h
new file mode 100644 (file)
index 0000000..c2d81e4
--- /dev/null
@@ -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/ex01/main.cpp b/ex01/main.cpp
new file mode 100644 (file)
index 0000000..3a3efa2
--- /dev/null
@@ -0,0 +1,33 @@
+#include "Animal.h"
+#include "Cat.h"
+#include "Dog.h"
+#include "WrongAnimal.h"
+#include "WrongCat.h"
+#include <iostream>
+
+int main()
+{
+       const Animal* base = new Animal();
+       const Animal* cat = new Cat();
+       const Animal* dog = new Dog();
+       const WrongAnimal* wrong_base = new WrongAnimal();
+       const WrongAnimal* wrong_cat = new WrongCat();
+
+       std::cout << '\n' << base->getType() << ": ";
+       base->makeSound();
+       std::cout << cat->getType() << ": ";
+       cat->makeSound();
+       std::cout << dog->getType() << ": ";
+       dog->makeSound();
+       std::cout << '\n' << wrong_base->getType() << ": ";
+       wrong_base->makeSound();
+       std::cout << wrong_cat->getType() << ": ";
+       wrong_cat->makeSound();
+       std::cout << '\n';
+       delete base;
+       delete cat;
+       delete dog;
+       delete wrong_base;
+       delete wrong_cat;
+       return (0);
+}