From 0d55cf12734453cc35753b81a003e15c52da3457 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Sun, 24 May 2020 17:39:59 +0200 Subject: [PATCH] polymorphism --- README.md | 6 +++--- cpp/Polymorphism.md | 5 +++++ cpp/inheritance/README.md | 2 -- cpp/inheritance/animal.h | 1 + cpp/inheritance/cat.cpp | 3 +++ cpp/inheritance/cat.h | 3 +++ cpp/inheritance/dog.cpp | 1 + cpp/inheritance/dog.h | 3 +++ cpp/inheritance/main.cpp | 13 +++++++++---- 9 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 cpp/Polymorphism.md diff --git a/README.md b/README.md index f5c983e..c915dd7 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,9 @@ Lessons: - [creating a class](./lessons/Classes.md) - [constructors](./lessons/Constructors.md) - [encapsulation](./lessons/Encapsulation.md) -- [inheritance](./lessons/Inheritance.md) <-- -- [properties](./lessons/Properties.md) -- [polymorphism](./lessons/Polymorphism.md) +- [inheritance](./lessons/Inheritance.md) +- [properties](./lessons/Properties.md) - +- [polymorphism](./lessons/Polymorphism.md) - Work in progress: diff --git a/cpp/Polymorphism.md b/cpp/Polymorphism.md new file mode 100644 index 0000000..ca0e969 --- /dev/null +++ b/cpp/Polymorphism.md @@ -0,0 +1,5 @@ +# Polymorphism + +Relevant files: + +- [main.cpp](./inheritance/main.cpp) diff --git a/cpp/inheritance/README.md b/cpp/inheritance/README.md index 62d54a2..70ff893 100644 --- a/cpp/inheritance/README.md +++ b/cpp/inheritance/README.md @@ -5,8 +5,6 @@ little to complicated for this lesson. Relevant files: -- [main.cpp](./index.cpp) - - [animal.h](./animal.h) - [animal.cpp](./animal.cpp) diff --git a/cpp/inheritance/animal.h b/cpp/inheritance/animal.h index df2c12f..15b981f 100644 --- a/cpp/inheritance/animal.h +++ b/cpp/inheritance/animal.h @@ -7,6 +7,7 @@ class animal { public: std::string name; + // define make_sound as virtual to allow overriding in subclasses virtual void make_sound(); animal(std::string name); }; diff --git a/cpp/inheritance/cat.cpp b/cpp/inheritance/cat.cpp index f5295a4..9bbf579 100644 --- a/cpp/inheritance/cat.cpp +++ b/cpp/inheritance/cat.cpp @@ -4,6 +4,9 @@ #include void cat::make_sound() { + // call the base class function first animal::make_sound(); + + // add additional functionality cout << "meow" << endl; } diff --git a/cpp/inheritance/cat.h b/cpp/inheritance/cat.h index 142e8f9..79e2098 100644 --- a/cpp/inheritance/cat.h +++ b/cpp/inheritance/cat.h @@ -1,12 +1,15 @@ #pragma once #include +// include the animal header #include "animal.h" using namespace std; class cat: public animal { + // include the animal constructor using animal::animal; public: + // override the make_sound function void make_sound() override; }; diff --git a/cpp/inheritance/dog.cpp b/cpp/inheritance/dog.cpp index f6eb9c4..2e04487 100644 --- a/cpp/inheritance/dog.cpp +++ b/cpp/inheritance/dog.cpp @@ -4,6 +4,7 @@ #include void dog::make_sound() { + // implement own functionality cout << "doggo " << name << " says:" << endl; cout << "woof!" << endl; } diff --git a/cpp/inheritance/dog.h b/cpp/inheritance/dog.h index c771d7e..4a533b1 100644 --- a/cpp/inheritance/dog.h +++ b/cpp/inheritance/dog.h @@ -1,12 +1,15 @@ #pragma once #include +// include the animal header #include "animal.h" using namespace std; class dog: public animal { + // include the animal constructor using animal::animal; public: + // override the make_sound function void make_sound() override; }; diff --git a/cpp/inheritance/main.cpp b/cpp/inheritance/main.cpp index 03ffd1d..073a85a 100644 --- a/cpp/inheritance/main.cpp +++ b/cpp/inheritance/main.cpp @@ -1,12 +1,17 @@ +#include "animal.h" #include "cat.h" #include "dog.h" #include int main() { - dog d("buster"); - cat c("mittens"); + // because cat and dog are subclasses of animal, they can be stored in an array of animals + animal * animals[2] = { + new dog("buster"), + new cat("mittens") + }; - d.make_sound(); - c.make_sound(); + for (animal * a : animals) + // when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog) + a->make_sound(); }