Programming embedded techniques: polymorphism in C++

Soon after the earlier lessons on encapsulation and inheritance, you are now prepared for the third elementary OOP idea identified as polymorphism, which has no direct analog in procedural languages like C.

So significantly, in this section about Item-Oriented Programming (OOP), you figured out about encapsulation and inheritance, and you saw how to naturally emulate them in C. Now you are all set for the third essential OOP thought called polymorphism, which has no immediate analog in procedural languages like C. Thus, the approach for today’s video clip lesson #31 is reversed compared to the earlier two in that you will initial see what polymorphism is, and how it is effective at a reduced level in C++ [1]. In the future lesson, you will apply this knowledge to emulate polymorphism in C.

Lesson #31 OOP Aspect-3: Polymorphism in C++ at?v=xHMje9fL1Bk

Polymorphism (Many Varieties)

Polymorphism is the capacity to specify an procedure in a foundation course and then supply several implementations (called approaches) of that operation in the subclasses. For example, the movie displays the draw() operation in the Form foundation class, which is then executed in Shape’s Rectangle and Circle subclasses.

What Is Polymorphism Fantastic For?

Polymorphism will allow you to produce generic, extensible code with less dependencies than you could without it. For illustration, the movie shows a generic drawGraph() operate, dependent only on the Condition foundation class, which can attract graphs consisting of Rectangles, Circles, and any other Condition subclasses but to be invented.

Without polymorphism, the drawGraph() operate would have to look at the specific style of shape to be drawn and then contact the individual draw() implementation (e.g., Rectangle_attract() or Circle_attract()). This kind of a drawGraph() function would rely on Rectangles, Circles, or any other concrete styles and would require modification when a new form is included or eradicated.

Early Binding vs. Late Binding

The connection among the functionality get in touch with and the purpose implementation is referred to as simply call binding. In procedural languages like C, a purpose can have only one implementation, and as a result, there is only just one sort of get in touch with binding established at compile- and connection-time.

Polymorphism allows a perform to have several implementations, which needs a distinct contact mechanism recognized at operate-time dependent on the variety of the object. This connect with system is regarded as late binding, in distinction to the standard early binding system. In C++, late binding is enabled for features explicitly selected as digital.

The video clip looks beneath the hood of the late binding mechanism of C++, where you can see the indirection stages of the “virtual pointer” (VPTR) and the “virtual table” (VTABLE). You can see that the VPTR is inside every single item in RAM and that there is only 1 VTABLE for every class in ROM. Pertaining to the CPU overhead, late binding calls for only two more CPU guidance for each perform phone, similar to passing a single extra parameter. This is both of those deterministic and economical, especially if you think about that a digital phone (like Form ->draw()) commonly replaces a total switch statement dependent on the item variety.

The VPTR and the Constructor

The remaining piece of the puzzle is the initialization of the VPTR residing in every object. The great spot for that is the constructor, the place the C++ compiler secretly inserts code for setting the VPTR. The video also displays that the constructors of all foundation courses are invoked in the particular purchase, the superclass (e.g., Form) usually remaining initialized in advance of the subclass (e.g., Rectangle).

Conclusion Notes

The real benefit of polymorphism lies not basically in cutting down but reversing the dependencies inside your code. This component is the essence of the Open up/Closed Basic principle, which indicates that modules ought to be open for extensions but closed for modifications [2]. The polymorphic drawGraph() is dependent only on the Form superclass and does not need to have to modify (or even recompile) when you insert, clear away, or alter the Condition subclasses.

[1] Miro Samek, “Item-Oriented Programming in C, code examples“, GitHub. 

[2] Bob Martin, “Uncle Bob Reliable concepts: The Open up/Shut Basic principle“, YouTube 2019. 

Dr. Miro M. Samek is the creator of the open up source QP serious-time embedded frameworks and the freeware QM graphical product-based design and style resource. He is also the founder and CEO of Quantum Leaps — the supplier of modern embedded computer software centered on lively objects and hierarchical condition devices as effectively as resources for visual modeling, automated code technology, and unit tests of deeply embedded software program. Miro teaches the well-liked YouTube “Modern Embedded Devices Programming” movie class on which this posting collection is based.

Relevant Contents: