The Word Morphable and Polymorphism in C++ Explained

To understand the word morphable better, I write this article. I introduce the topic mainly to explain the word morphable. And as a extention, after the explanation it is a dedicated C++ based tutorial on Polymorphism.

Basically, Polymorphism means that one object can be referred to by different ways. As I am a male college student, you may call me guy, young man, Fenghe(my name), my parents call me son or more often directly by name. These make sense since as an entity myself, I play different roles in different social circumstances. So that’s where the prefix “poly” (multiple) comes from. As you can see after “poly”, here we come “morphism”, this is really similar to the word morphable in my previous article, just with the different postfix, i.e., they are the corresponding noun and adjectives of the same word. So we may put that morphism simply means a collection of attributes that constructs an entity(object) like me, you or everything in the world we live. But such magician is the nature that the construction process is not like 1+1=2, it shapes the entity to own its unique behavior. So at this stage, we can understand the world morphable easily. With the suffix “-able”, it should be the ability to collect different attributes and reconstruct a new entity or even abstractly, decomposing ideas to to compose new ideas like what we(human being) do.

(When writing the paragraph above, it suddenly stroke me that polymorphism and morphable share the same origin in terms of etymology. So writing is an excellent way to think about things more carefully and precisely.)

To get it straight, in the previous chapter, in the 3rd stage of the robot operating system, the robot should just think and behave like human beings. (More should be put here or the previous article to explain the exact meaning of morphable.

Now go back to polymorphism in CS, I will explain it based on C++.

To get to know Polymorphism better, we must introduce inheritance first.

What is in inheritance?

You may find this word in biology and medicine, which is a basic relationship between parents and their children. Similarly, in C++, inheritance just means that a class (derived class of a base class, a subclass of a superclass, or inherited class of a parent class) “gets” some of the features of a class( the base class, superclass, or inherited class); besides, it has some more attributes that a base class does not possess. You can just imagine the inherited class to be a kid of the parent in human relations.

Following is a C++ Implemented Example:

class Shape()
double area;
double perimeter;

class Circle()
public Shape
double radius;

class Square()
public Shape
double side_length;

As we all know, geometrically, circle and squares are all shapes, so they all have area and perimeter attributes. But visually, they are evidently different, and the difference can be represented by the radius of circle and side_length of square mathematically. In this way, codes above makes much sense. 

But as you may find, we use public before the class Shape in the subclass when performing the inheritance, why? Well, that is public inheritance. To understand what it is and how it works, let’s talk about Type of Inheritance.

Type of Inheritance

Type of Inheritance Visualization

You can get a straight-forward sense of what public, protected and private inheritances are from the illustration above. But to understand these things better, you have to know about the hierarchical structure of public, protected and private members.

private Only the member functions inside class can access the private members, friend class is an exception
protected Besides private, the derived classes can access
public every object in the scope can access

This is page is under construction, more concepts to be covered, including the following and beyond:

A class that declares or inherits a virtual function is called a polymorphic class. What does it mean?

What does polymorphism mean?

What does dynamic binding mean?

Virtual functions

Pure virtual functions

polymorphic class