Programming embedded techniques: polymorphism in C++

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++

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.

Read More... Read More