Polymorphism in CPP

Understanding Polymorphism in C++ Programming Language

In the realm of programming, the term polymorphism embodies the essence of versatility, encapsulating the concept of having many forms. This fascinating phenomenon bl

ossoms when a hierarchy of classes interlaces through the threads of inheritance, creating a tapestry of relationships.

In the domain of C++, polymorphism unveils its power through a distinctive facet. It signifies that invoking a member function triggers diverse functions to spring to life, contingent upon the object type that beckons the call.

Polymorphism in C++ is a concept that allows objects of different classes to be treated as objects of a common base class, enabling dynamic method binding and flexible behavior in object-oriented programming.

Let’s embark on a journey of understanding with a vivid example:

#include <iostream>
using namespace std;

class Shape {
   protected:
      int width, height;

   public:
      Shape(int a = 0, int b = 0) {
         width = a;
         height = b;
      }

      virtual int area() {
         cout << "Parent class area: " << width * height << endl;
         return width * height;
      }
};

class Rectangle: public Shape {
   public:
      Rectangle(int a = 0, int b = 0):Shape(a, b) { }

      int area() {
         cout << "Rectangle class area: " << width * height << endl;
         return (width * height);
      }
};

class Triangle: public Shape {
   public:
      Triangle(int a = 0, int b = 0):Shape(a, b) { }

      int area() {
         cout << "Triangle class area: " << (width * height)/2 << endl;
         return (width * height / 2);
      }
};

int main() {
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);

   // Storing the address of Rectangle
   shape = &rec;

   // Calling rectangle's area
   shape->area();

   // Storing the address of Triangle
   shape = &tri;

   // Calling triangle's area
   shape->area();

   return 0;
}

In the vivid narrative above, the tale unfolds through classes like Shape, Rectangle, and Triangle. The magic of polymorphism is invoked as different functions are executed based on the object type, transcending the limitations of static linkage.

The pivotal moment arrives with the keyword virtual, adorning the function definition within the Shape class. By bestowing the function with the power of being virtual, we embrace the realm of dynamic linkage, also known as late binding.

A Leap into the Virtual Realm

A virtual function emerges as a cornerstone in this symphony of polymorphism. Declared within a base class with the prefix virtual, it becomes a beacon of dynamic linkage. Unlike its non-virtual brethren, the selection of the function to be executed is deferred to runtime, dictated by the kind of object making the call.

But what if the base class lacks a meaningful definition for the function? Fear not, for the concept of pure virtual functions comes to the rescue. By decorating a virtual function with the = 0 signifier, we signal to the compiler that this function is a blank canvas, awaiting the touch of a derived class to bring it to life.

A glimpse of this enchantment is captured below:

class Shape {
   protected:
      int width, height;

   public:
      Shape(int a = 0, int b = 0) {
         width = a;
         height = b;
      }

      // Pure virtual function
      virtual int area() = 0;
};

The symphony of polymorphism conducts its harmonious melodies, uniting inheritance, virtual functions, and dynamic linkage to orchestrate code that adapts and transforms. With every call to a function, a myriad of possibilities unfolds, demonstrating the elegance of C++’s polymorphic prowess.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading