polymorphism
This commit is contained in:
parent
804adb3b89
commit
0d55cf1273
@ -15,9 +15,9 @@ Lessons:
|
|||||||
- [creating a class](./lessons/Classes.md)
|
- [creating a class](./lessons/Classes.md)
|
||||||
- [constructors](./lessons/Constructors.md)
|
- [constructors](./lessons/Constructors.md)
|
||||||
- [encapsulation](./lessons/Encapsulation.md)
|
- [encapsulation](./lessons/Encapsulation.md)
|
||||||
- [inheritance](./lessons/Inheritance.md) <--
|
- [inheritance](./lessons/Inheritance.md)
|
||||||
- [properties](./lessons/Properties.md)
|
- [properties](./lessons/Properties.md) -
|
||||||
- [polymorphism](./lessons/Polymorphism.md)
|
- [polymorphism](./lessons/Polymorphism.md) -
|
||||||
|
|
||||||
Work in progress:
|
Work in progress:
|
||||||
|
|
||||||
|
5
cpp/Polymorphism.md
Normal file
5
cpp/Polymorphism.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Polymorphism
|
||||||
|
|
||||||
|
Relevant files:
|
||||||
|
|
||||||
|
- [main.cpp](./inheritance/main.cpp)
|
@ -5,8 +5,6 @@ little to complicated for this lesson.
|
|||||||
|
|
||||||
Relevant files:
|
Relevant files:
|
||||||
|
|
||||||
- [main.cpp](./index.cpp)
|
|
||||||
|
|
||||||
- [animal.h](./animal.h)
|
- [animal.h](./animal.h)
|
||||||
- [animal.cpp](./animal.cpp)
|
- [animal.cpp](./animal.cpp)
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ class animal
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::string name;
|
std::string name;
|
||||||
|
// define make_sound as virtual to allow overriding in subclasses
|
||||||
virtual void make_sound();
|
virtual void make_sound();
|
||||||
animal(std::string name);
|
animal(std::string name);
|
||||||
};
|
};
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void cat::make_sound() {
|
void cat::make_sound() {
|
||||||
|
// call the base class function first
|
||||||
animal::make_sound();
|
animal::make_sound();
|
||||||
|
|
||||||
|
// add additional functionality
|
||||||
cout << "meow" << endl;
|
cout << "meow" << endl;
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
// include the animal header
|
||||||
#include "animal.h"
|
#include "animal.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class cat: public animal
|
class cat: public animal
|
||||||
{
|
{
|
||||||
|
// include the animal constructor
|
||||||
using animal::animal;
|
using animal::animal;
|
||||||
public:
|
public:
|
||||||
|
// override the make_sound function
|
||||||
void make_sound() override;
|
void make_sound() override;
|
||||||
};
|
};
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void dog::make_sound() {
|
void dog::make_sound() {
|
||||||
|
// implement own functionality
|
||||||
cout << "doggo " << name << " says:" << endl;
|
cout << "doggo " << name << " says:" << endl;
|
||||||
cout << "woof!" << endl;
|
cout << "woof!" << endl;
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
// include the animal header
|
||||||
#include "animal.h"
|
#include "animal.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class dog: public animal
|
class dog: public animal
|
||||||
{
|
{
|
||||||
|
// include the animal constructor
|
||||||
using animal::animal;
|
using animal::animal;
|
||||||
public:
|
public:
|
||||||
|
// override the make_sound function
|
||||||
void make_sound() override;
|
void make_sound() override;
|
||||||
};
|
};
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
|
#include "animal.h"
|
||||||
#include "cat.h"
|
#include "cat.h"
|
||||||
#include "dog.h"
|
#include "dog.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
dog d("buster");
|
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||||
cat c("mittens");
|
animal * animals[2] = {
|
||||||
|
new dog("buster"),
|
||||||
|
new cat("mittens")
|
||||||
|
};
|
||||||
|
|
||||||
d.make_sound();
|
for (animal * a : animals)
|
||||||
c.make_sound();
|
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||||
|
a->make_sound();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user