- Game Development Patterns and Best Practices
- John P. Doran Matt Casanova
- 459字
- 2021-07-02 23:43:42
Polymorphism refresher
In Hollywood, lots of actors and actresses take on many different roles when filming movies. They can be the hero of a story, a villain, or anything else as they inhabit a role. No matter what role they've gotten, when they are being filmed they are acting even if what they do specifically can be quite different. This kind of behavior acts similarly to the idea of polymorphism.
Polymorphism is one of the three pillars of an object-oriented language (along with encapsulation and inheritance). It comes from the words poly meaning many and morph meaning change.
Polymorphism is a way to call different specific class functions in an inheritance hierarchy, even though our code only uses a single type. That single type, the base class reference, will be changed many ways depending on the derived type. Continuing with the Hollywood example, we can tell an actor to act out a role and, based on what they've been cast in, they will do something different.
By using the virtual keyword on a base class function and overriding that function in a derived class, we can gain the ability to call that derived class function from a base class reference. While it may seem a bit complex at first, this will seem clearer with an example. For instance, if we have the following class:
class Animal
{
public:
virtual void Speak(void) const //virtual in the base class
{
//Using the Mach 5 console print
M5DEBUG_PRINT("...\n");
}
};
I could create a derived class with its own method, without modifying the base class in any way. In addition, we have the ability to replace or override a method within a derived class without affecting the base class. Let's say I wanted to change this function:
class Cat: public Animal
{
public:
void Speak(void) const //overridden in the derived class
{
M5DEBUG_PRINT("Meow\n");
}
void Purr(void) const //unrelated function
{
M5DEBUG_PRINT("*purr*\n");
}
};
class Dog: public Animal
{
public:
void Speak(void) const //overridden in the derived class
{
M5DEBUG_PRINT("Woof\n");
}
};
Since a derived class can be used anywhere a base class is needed, we can refer to derived classes using a base class pointer or an array of pointers and call the correct function at runtime. Let's have a look at the following code:
void SomeFunction(void)
{
const int SIZE = 2;
Cat cat;
Dog dog;
Animal* animals[SIZE] = {&cat, &dog};
for(int i = 0; i < SIZE; ++i)
{
animals[i]->Speak();
}
}
The following is the output of the preceding code:
Meow
Woof
As you can see, even though we have an array of base class pointers, the correct derived class function is called. If the functions weren't marked as virtual, or if the derived classes didn't override the correct functions, polymorphism wouldn't work.
- Android Wearable Programming
- 垃圾回收的算法與實(shí)現(xiàn)
- Visual C++數(shù)字圖像模式識(shí)別技術(shù)詳解
- Android 7編程入門經(jīng)典:使用Android Studio 2(第4版)
- Python高級(jí)編程
- Oracle數(shù)據(jù)庫從入門到運(yùn)維實(shí)戰(zhàn)
- WebRTC技術(shù)詳解:從0到1構(gòu)建多人視頻會(huì)議系統(tǒng)
- Learning OpenStack Networking(Neutron)(Second Edition)
- Android玩家必備
- App Inventor創(chuàng)意趣味編程進(jìn)階
- HTML5開發(fā)精要與實(shí)例詳解
- Emgu CV Essentials
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)教程(Windows 7+Office 2010)
- 后臺(tái)開發(fā):核心技術(shù)與應(yīng)用實(shí)踐
- Machine Learning for Developers