From 2a3aa83adf2daa629cd216642aa6db34b669f6a3 Mon Sep 17 00:00:00 2001
From: Aleksandar Karakas <aleksandar.karakas@tugraz.at>
Date: Thu, 25 Mar 2021 15:48:23 +0100
Subject: [PATCH] add code for inheritance

---
 .../0_pets_wo_inheritance/main.cpp            | 49 +++++++++++++++++++
 .../1_pets_w_inheritance/cat.hpp              | 12 +++++
 .../1_pets_w_inheritance/main.cpp             | 15 ++++++
 .../1_pets_w_inheritance/pet.hpp              | 24 +++++++++
 .../1_pets_w_inheritance/pig.hpp              | 13 +++++
 .../2_pets_w_polymorphism/cat.hpp             | 12 +++++
 .../2_pets_w_polymorphism/main.cpp            | 15 ++++++
 .../2_pets_w_polymorphism/pet.hpp             | 25 ++++++++++
 .../2_pets_w_polymorphism/pig.hpp             | 14 ++++++
 9 files changed, 179 insertions(+)
 create mode 100644 2021-oop1/vo/stream-04-vererbung/0_pets_wo_inheritance/main.cpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/cat.hpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/main.cpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pet.hpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pig.hpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/cat.hpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/main.cpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pet.hpp
 create mode 100644 2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pig.hpp

diff --git a/2021-oop1/vo/stream-04-vererbung/0_pets_wo_inheritance/main.cpp b/2021-oop1/vo/stream-04-vererbung/0_pets_wo_inheritance/main.cpp
new file mode 100644
index 0000000..f1bb389
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/0_pets_wo_inheritance/main.cpp
@@ -0,0 +1,49 @@
+#include <cstdio>
+#include <cstring>
+
+// TODO: destructor (free memory allocated on the heap (for name_))
+// TODO: copy constructor (implement deep copy)
+
+class Pig {
+  char* name_; // longer names now supported (compared to `char name_[10];`)
+  unsigned age_;
+ 
+  public:
+    Pig(const char* name, unsigned age)
+      : name_{ new char[strlen(name) + 1] }, age_{age}
+    {
+      strcpy(name_, name);
+    }
+    void drink() { printf("%s drinks\n", name_); }
+    void bath() { printf("Bath! :D\n"); }
+    unsigned getAge() { return age_; }
+    void setAge(unsigned age) { age_ = age; }
+};
+
+class Cat { // not a pig
+  char* name_;
+  unsigned age_;
+
+  public:
+    Cat(const char* name, unsigned age)
+      : name_{ new char[strlen(name) + 1] }, age_{age}
+    {
+      strcpy(name_, name);
+    }
+    void drink() { printf("%s drinks\n", name_); }
+    void play() { printf("Play! :D\n"); } // cats play and don't take a bath
+    unsigned getAge() { return age_; }
+    void setAge(unsigned age) { age_ = age; }
+};
+
+int main()
+{
+  Pig pig{ "Babe", 0 };
+  pig.bath();
+  pig.drink();
+  Cat cat{ "Grumpy", 7 };
+  cat.play();
+  // cat.bath(); // <-- error: no member named 'bath' in 'Cat' (Cats don't enjoy taking a bath – only pigs do)
+  cat.drink();
+  return 0;
+}
diff --git a/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/cat.hpp b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/cat.hpp
new file mode 100644
index 0000000..44f5d6b
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/cat.hpp
@@ -0,0 +1,12 @@
+# include "pet.hpp"
+
+// TODO: add include guards (see pet.hpp for an example)
+	
+class Cat : public Pet {
+  public:
+    Cat(const char* name, unsigned age)
+      : Pet{ name, age } {}
+    Cat(const Cat& other) = delete;
+    ~Cat() = default;
+    void play() { printf("%s plays!\n", name_); }
+};
\ No newline at end of file
diff --git a/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/main.cpp b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/main.cpp
new file mode 100644
index 0000000..d3d6801
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/main.cpp
@@ -0,0 +1,15 @@
+#include <cstdio>
+
+#include "pig.hpp"
+#include "cat.hpp"
+
+int main()
+{
+  Pig pig{ "Babe", 0 };
+  pig.drink();
+  pig.bath();
+  Cat cat{ "Grumpy", 0 };
+  cat.drink();
+  cat.play();
+  return 0;
+}
\ No newline at end of file
diff --git a/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pet.hpp b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pet.hpp
new file mode 100644
index 0000000..93a1191
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pet.hpp
@@ -0,0 +1,24 @@
+#ifndef PET_H
+#define PET_H
+
+#include <cstring>
+#include <cstdio>
+
+class Pet {
+  unsigned age_;
+  protected:
+    char* name_;
+  public:
+    Pet(const char* name, unsigned age)
+      : age_{age}, name_{ new char[strlen(name) + 1] }
+    {
+      strcpy(name_, name);
+    }
+    Pet(const Pet& other) = delete;
+    ~Pet() { delete[] name_; }
+    unsigned getAge() { return age_; }
+    void setAge(unsigned age) { age_ = age; }
+    void drink() { printf("%s drinks\n", name_); }
+};
+
+#endif
\ No newline at end of file
diff --git a/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pig.hpp b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pig.hpp
new file mode 100644
index 0000000..aca892f
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/1_pets_w_inheritance/pig.hpp
@@ -0,0 +1,13 @@
+# include "pet.hpp"
+
+// TODO: add include guards (see pet.hpp for an example)
+
+class Pig : public Pet {
+  public:
+    // using Pet::Pet; // possible instead of defining the constructor for Pig. Using this line, Pig inherits Pet's constructor.
+    Pig(const char* name, unsigned age)
+      : Pet{ name, age } {}
+    Pig(const Pig& other) = delete;
+    ~Pig() = default;
+    void bath() { printf("%s enjoys taking a bath!\n", name_); }
+};
\ No newline at end of file
diff --git a/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/cat.hpp b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/cat.hpp
new file mode 100644
index 0000000..39036e2
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/cat.hpp
@@ -0,0 +1,12 @@
+# include "pet.hpp"
+
+// TODO: add include guards (see pet.hpp for an example)
+
+class Cat : public Pet {
+  public:
+    Cat(const char* name, unsigned age)
+      : Pet{ name, age } {}
+    Cat(const Cat& other) = delete;
+    ~Cat() = default;
+    void play() { printf("%s plays!\n", name_); }
+};
diff --git a/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/main.cpp b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/main.cpp
new file mode 100644
index 0000000..096b016
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/main.cpp
@@ -0,0 +1,15 @@
+#include <cstdio>
+
+#include "pig.hpp"
+#include "cat.hpp"
+
+int main() {
+  Pet* pets[2]{
+    new Pig{ "Babe", 0 },
+    new Cat{ "Grumpy", 7}
+  };
+  for (auto pet : pets) {
+    pet->drink();
+  }
+  return 0;
+}
diff --git a/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pet.hpp b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pet.hpp
new file mode 100644
index 0000000..9a5c4cb
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pet.hpp
@@ -0,0 +1,25 @@
+#ifndef PET_H
+#define PET_H
+
+#include <cstring>
+#include <cstdio>
+
+class Pet {
+  unsigned age_;
+  protected:
+    char* name_;
+  public:
+    Pet(const char* name, unsigned age)
+      : age_{age}, name_{ new char[strlen(name) + 1] }
+    {
+      strcpy(name_, name);
+    }
+    Pet(const Pet& other) = delete;
+    ~Pet() { delete[] name_; }
+    unsigned getAge() { return age_; }
+    void setAge(unsigned age) { age_ = age; }
+    // virtual void drink() = 0; // if we use this line instead of the next, Pet becomes an abstract class.
+    virtual void drink() { printf("%s drinks.\n", name_); }
+};
+
+#endif
\ No newline at end of file
diff --git a/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pig.hpp b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pig.hpp
new file mode 100644
index 0000000..fa67f76
--- /dev/null
+++ b/2021-oop1/vo/stream-04-vererbung/2_pets_w_polymorphism/pig.hpp
@@ -0,0 +1,14 @@
+# include "pet.hpp"
+
+// TODO: add include guards (see pet.hpp for an example)
+
+class Pig : public Pet {
+  public:
+    // using Pet::Pet; // possible instead of defining the constructor for Pig. Using this line, Pig inherits Pet's constructor.
+    Pig(const char* name, unsigned age)
+      : Pet{ name, age } {}
+    Pig(const Pig& other) = delete;
+    ~Pig() = default;
+    void bath() { printf("%s enjoys taking a bath!\n", name_); }
+    void drink() override { printf("%s drinks water.\n", name_); }
+};
-- 
GitLab