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 0000000000000000000000000000000000000000..f1bb3898f044a574f189533bca90f8e902cb1732 --- /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 0000000000000000000000000000000000000000..44f5d6b4ae335c08416dd285bb4cf1e6bcb8cd57 --- /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 0000000000000000000000000000000000000000..d3d68014169018468861691b250d49d69fbf4957 --- /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 0000000000000000000000000000000000000000..93a11912cff57073b382d8f4db9628050b636dd4 --- /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 0000000000000000000000000000000000000000..aca892f53914f31a40876d41142ce9f53e3bdedd --- /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 0000000000000000000000000000000000000000..39036e26482ac2760843cb9ed0206eb8fdae4d69 --- /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 0000000000000000000000000000000000000000..096b0169f591d08557308388e6a5855df09bc5ea --- /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 0000000000000000000000000000000000000000..9a5c4cbb1202bf7c2f12534d1848ea21780c561a --- /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 0000000000000000000000000000000000000000..fa67f7608dc5fd4b5a189ed3ab6b4926c8bd14ee --- /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_); } +};